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
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.
Inside btn_add_task listener, set the alarm as below -
Intent notifyMessage = new Intent(getApplicationContext(),NotificationMessage.class);
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0, notifyMessage, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (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.