Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alarm Notification fires instantly. Android

I am working on a Reminder that sends notification on fixed time to the user.

The alarm is getting off instantly ...

I tried most of the suggestions over stackoverflow, but still having same issue

Please help me sort this problem out.

server data

user_reminder": [
                {
                    "id": "75",
                    "name": "Morning Snacks",
                    "time": "11:00:00",
                    "days": "1,2,3,4,5,6,7",
                    "user_id": "14"
                },
                {
                    "id": "76",
                    "name": "Lunch",
                    "time": "13:00:00",
                    "days": "1,2,3,4,5,6,7",
                    "user_id": "14"
                },
               ......
            ]

My code

for (int i = 0; i < reminderList.size(); i++) 
{
     String time = reminderList.get(i).getTime(); // "time": "11:00:00"

    String strSpit[] = time.split(":");
    String strDays[] = reminderList.get(i).getDays().split(","); //"days": "1,2,3,4,5,6,7"

    Date date = new Date();
    Calendar calNow = Calendar.getInstance();
    calNow.setTime(date);

    Calendar calAlarm = Calendar.getInstance();
    calAlarm.set(Calendar.HOUR_OF_DAY, Integer.parseInt(strSpit[0]));
    calAlarm.set(Calendar.MINUTE, Integer.parseInt(strSpit[1]));

    for (int j = 0; j < strDays.length; j++) 
    {
        calAlarm.set(Calendar.DAY_OF_WEEK, viewFunctions.getDayInt(strDays[j]));

        if (calAlarm.before(calNow)) 
        {
            //if its in the past increment
            calAlarm.add(Calendar.DATE, 1);
        }

        notifyIntent.putExtra(Constants.REMINDER_NAME, reminderList.get(i).getName());
        pendingIntent = PendingIntent.getBroadcast(this, 0, notifyIntent, PendingIntent.FLAG_ONE_SHOT);
        alarmManager.set(AlarmManager.RTC_WAKEUP, calAlarm.getTimeInMillis() , pendingIntent);

        }
    }
}

Get days : This solves the day numbering

public int getDayInt(String strDay) 
{
   int dayNumber = 0;

   if (strDay.equals("1")) 
   {
       dayNumber = Calendar.MONDAY;

   } ......

   return dayNumber;
}

screen shot

enter image description here

like image 691
karthik kolanji Avatar asked Sep 01 '17 12:09

karthik kolanji


2 Answers

Problem

You alarm is going off instantly because Android will fire off any alarms that are scheduled in the past.

Some of your alarms are getting scheduled in the past because the following code is not working as you expect. Sample code from your question:

if (calAlarm.before(calNow)) 
{
   //if [it's] in the past increment
   calAlarm.add(Calendar.DATE, 1);
}

In the above code you are only adding one day to your alarm if the alarm is in the past. So let's say you are running this code on Friday and you read an alarm for Monday. Your code will add one day to the date making it Tuesday, schedule that alarm. The alarm is in the past because Tuesday is still before Friday, so Android will fire off that alarm shortly after being scheduled.

Update

It is unclear from your question what you wish to do with the reminders that are in the past. One possible solution is to schedule them 1 week into the future.

if(calAlarm.before(calNow)) 
{
   // If it's in the past increment by one week.
   calAlarm.add(Calendar.DATE, 7);
}
like image 57
Newtron Labs Avatar answered Oct 30 '22 20:10

Newtron Labs


The main problem appears to be with this line:

calAlarm.set(Calendar.DAY_OF_WEEK, viewFunctions.getDayInt(strDays[j]));

What you need to realise is this is just setting the Day of the week which will be displayed in output - It is not changing the underlying date to match, which I think is what you are expecting.

Try using the following code to alter your dates to set an alarm for each day selected:

String strSpit[] = time.split(":");
String strDays[] = reminderList.get(i).getDays().split(","); //"days": "1,2,3,4,5,6,7"

Calendar todayWithTime = Calendar.getInstance(); //setting current time is redundant
todayWithTime.set(Calendar.HOUR_OF_DAY, Integer.parseInt(strSpit[0]));
todayWithTime.set(Calendar.MINUTE, Integer.parseInt(strSpit[1]));

Calendar alarm;
int today = todayWithTime.get(Calendar.DAY_OF_WEEK);
int offset, target;

for (int j = 0; j < strDays.length; j++) {

    alarm = (Calendar) todayWithTime.clone(); //now you have todays date, but correct time
    target = strDays[j];
    //saturday is biggest day of week
    offset  = (Calendar.SATURDAY - today + target) % 7; //work out how many days in the future the next occurance of this day is
    alarm.add(Calendar.DATE, offset);

    ... // the rest stays the same

}
like image 1
Nick Cardoso Avatar answered Oct 30 '22 20:10

Nick Cardoso