Skip to content Skip to sidebar Skip to footer

Alarm And Notification Not Triggered At Specified Time

I am trying to trigger a notification and an alarm at a specified time. I have put log information to console to see if the correct time is being set and it is fine. However, still

Solution 1:

Take a look at the documentation of the setRepeating method:

Note: as of API 19, all repeating alarms are inexact. If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above. Legacy applications whose targetSdkVersion is earlier than API 19 will continue to have all of their alarms, including repeating alarms, treated as exact.

Your alarms will be inexact always using setRepeating, try using setExact and reprograming the alarm when it's triggered.

Also, try extending WakefulBroadcastReceiver instead of BroadcastReceiver in NotificationMessage in order to be able to wake the service when your app isn't running.

Solution 2:

Inside btn_add_task listener, set the alarm as below -

IntentnotifyMessage=newIntent(getApplicationContext(),NotificationMessage.class);
PendingIntentpi= PendingIntent.getBroadcast(getApplicationContext(), 0, notifyMessage, PendingIntent.FLAG_UPDATE_CURRENT);    
AlarmManageram= (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pi);

Now, put a log inside onReceive of NotificationMessage class to be sure that the alarm is fired on time. Hope, now your program will run.

Post a Comment for "Alarm And Notification Not Triggered At Specified Time"