I'm trying to develop an app which requires scheduled notifications everyday at 7am, 11am and 7pm. And when user taps on the notification it takes the user to MainActivity.
I have a working code but it's showing random behavior. I have the following problems.
1) Every time I open MainActivity, it triggers the alarm manager and shows a notification even though it's only supposed to be triggered at 7am, 11am and 7pm.
2) Also, I think I'm somehow duplicating alarms every time I open the main activity. Everytime I setup the alarm, I cancel the pending intent and then setup again. Is this the correct way to do it?
Manifest.xml
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="android.media.action.DISPLAY_NOTIFICATION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
MainActivity.java
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
notificationIntent.addCategory("android.intent.category.DEFAULT");
PendingIntent broadcast = PendingIntent.getBroadcast(this, 100, notificationIntent, 0);
PendingIntent broadcast1 = PendingIntent.getBroadcast(this, 101, notificationIntent, 0);
PendingIntent broadcast2 = PendingIntent.getBroadcast(this, 102, notificationIntent, 0);
cal.set(Calendar.HOUR_OF_DAY, 7);
cal.set(Calendar.MINUTE, 0);
alarmManager.cancel(broadcast);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, broadcast);
cal1.set(Calendar.HOUR_OF_DAY, 11);
cal1.set(Calendar.MINUTE, 0);
alarmManager.cancel(broadcast1);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, broadcast1);
cal2.set(Calendar.HOUR_OF_DAY, 19);
cal2.set(Calendar.MINUTE, 0);
alarmManager.cancel(broadcast2);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, broadcast2);
AlarmReceiver.java
@Override
public void onReceive(Context context, Intent intent) {
String Text = getText();
Resources res = context.getResources();
Intent notificationIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Notification notification = builder.setSmallIcon(R.drawable.ic_menu_notify)
.setTicker("Ticker")
.setLargeIcon(BitmapFactory.decodeResource(res, R.mipmap.ic_launcher))
.setContentTitle("Title")
.setContentText(Text)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)
.setContentIntent(pendingIntent).build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}