1

In my android app I need to show multiple local notifications on a particular day at different time intervals,I used alarm manager and broadcast receiver to do for one notification but when I was trying to implement multiple notifications only the second one is been displayed.

Here is my MainActivity

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
    notificationIntent.addCategory("android.intent.category.DEFAULT");

    PendingIntent broadcast = PendingIntent.getBroadcast(this, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Calendar cal = Calendar.getInstance();
    cal.set(2015, 10, 23, 15, 03);
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);

Here is BroadcastReceiver

Intent notificationIntent = new Intent(context, MainActivity.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(notificationIntent);

    PendingIntent pendingIntent1 = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

    Notification notification = builder.setContentTitle("TRR - 2016")
            .setContentText("Station - 1 closed grace period only 10min")
            .setTicker("New Message Alert!")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setDefaults(Notification.DEFAULT_SOUND)
            .setContentIntent(pendingIntent1).build();

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(mCounter, notification);

Notification notification1 = builder.setContentTitle("TRR - 2016")
            .setContentText("Station - 2 closed grace period only 10min")
            .setTicker("New Message Alert!")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setDefaults(Notification.DEFAULT_SOUND)
            .setContentIntent(pendingIntent1).build();

    NotificationManager notificationManager1 = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager1.notify(++mCounter, notification1);
ShivLeela
  • 117
  • 1
  • 3
  • 12

1 Answers1

4

Each Notification must have its own Notification ID.

Your problem is here:

notificationManager.notify(0, notification);

Specifically, the "0" is the ID of the Notification. If you do not provide a different ID, Android will think you are simply updating the Notification that already exists.

Documentation.

public void notify (int id, Notification notification)

Post a notification to be shown in the status bar. If a notification with the same id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.

You could try something like this:

private int mCounter = 0;

...

notificationManager1.notify(++mCounter, notification1);
Knossos
  • 15,802
  • 10
  • 54
  • 91
  • I tried by keeping that,any way each notification has different kind of information – ShivLeela Nov 23 '15 at 10:37
  • should I use different intents and different builders for multiple notifications..? – ShivLeela Nov 23 '15 at 10:40
  • You can use all the same builders, just for each new type of Notification, you need a different ID. You should work out a way of tracking this information though, so that you can later cancel these Notifications (if that is at all relevant). – Knossos Nov 23 '15 at 10:49
  • can you please edit my post and show me where I should use different notification id,I tried it but it didn't work – ShivLeela Nov 23 '15 at 10:51
  • I cannot make it any clearer. For each notification you make, you need another ID. For example, each time you create a Notification, you could increment the ID. – Knossos Nov 23 '15 at 10:56
  • plz check my updated question.Is this what you are telling me to do? – ShivLeela Nov 23 '15 at 11:04
  • No, that is still a static ID. You need to change the ID for /every/ Notification. – Knossos Nov 23 '15 at 11:05
  • could you please explain with an example by editing the post. – ShivLeela Nov 23 '15 at 11:09
  • Try the example. First part is a member variable for your notification class. The second part is for calling the notification. – Knossos Nov 23 '15 at 11:12
  • tried as you said still I'm receiving 2 notifications at a time (latest time) – ShivLeela Nov 23 '15 at 11:24
  • Use LogCat to determine what is being put into the NotificationManager. `Log.i("TEST", "Counter: "+mCounter);` – Knossos Nov 23 '15 at 11:30
  • That means that mCounter is not incrementing. That means either you haven't put the variable in the class, or you are creating the class each time for each Notification. This will be my last comment here. – Knossos Nov 23 '15 at 11:41