8

i am trying to show different notifications after some time interval but what happens is it updates the current PendingIntent by the new one as a result i get only single notification even if i fire 4 - 5 pending intents requests

On button click i do the following thing

 try{
                 adapter.OpenDB();
             int id = adapter.getMaxId("reminder")+1;
             adapter.CloseDB();
             Intent intent = new Intent(MilestonesActivity.this, AlarmReceiver.class);
             intent.putExtra("message", ""+editMsg.getText()+" "+editDate.getText());               
             intent.putExtra("id", id);
              PendingIntent pendingIntent = PendingIntent.getBroadcast(MilestonesActivity.this, 0,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
                am.set(AlarmManager.RTC_WAKEUP,
                reminddate.getTimeInMillis(), pendingIntent);

             adapter.CloseDB();
             }catch (Exception e) {
                // TODO: handle exception
            }

AlarmReceiver.java

 @Override
 public void onReceive(Context context, Intent intent) {

     Intent i =new Intent(context, NotificationService.class);

     try{

     int id = intent.getExtras().getInt("id");
        i.putExtra("id", id);
     CharSequence message = intent.getExtras().getString("message");
        i.putExtra("message", message);
     }catch (Exception e) {
        // TODO: handle exception
    }
     context.startService(i);

 }

NotificationService.java

 public void  show(Intent intent) {
 try{
        NotificationManager nm;
         Log.e("recieve", "ok");
      nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      CharSequence from = "Lawyer app Reminder";
      CharSequence message = intent.getExtras().getString("message");
      PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(), 
                PendingIntent.FLAG_ONE_SHOT);// also tried FLAG_UPDATE_CURRENT
      int id =intent.getExtras().getInt("id");
      Log.e("I", ""+id);
      Notification notif = new Notification(R.drawable.message_active,
        "Lawyer app Reminder.", System.currentTimeMillis());
      notif.defaults = Notification.DEFAULT_ALL;
      notif.flags = Notification.FLAG_AUTO_CANCEL;

      notif.setLatestEventInfo(this, from, message, contentIntent);
      nm.notify(id, notif);

 }catch (Exception e) {         
     e.printStackTrace();            
}

plaese help me out with this .thanks in advance

Mahesh
  • 1,257
  • 1
  • 14
  • 24

3 Answers3

38

i found it

the second parameter of pendingintent constructor should not be same (which was 0 hard coded)

i changed it to (int) System.currentTimeMillis() so that it should not be the same :)

Intent ni =new Intent(NotificationService.this,ModifyDatabaseService.class);
        ni.putExtra("id", ""+id);
        PendingIntent contentIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), ni, 
                PendingIntent.FLAG_ONE_SHOT);
Mahesh
  • 1,257
  • 1
  • 14
  • 24
  • 15
    Mother-uckers! I had exactly the same problem. The -ing documentation even says "currently not used". Currently not used my arse. – Timmmm Oct 31 '12 at 16:52
  • *Update*: Upon further testing, it does actually work with just having 0 as the second parameter as long as you use `FLAG_ONE_SHOT`. You can do either/or basically. `FLAG_ONE_SHOT` seems more correct though. – Timmmm Oct 31 '12 at 17:09
  • 2
    *Update 2*: Disregard me previous comment. `FLAG_ONE_SHOT` makes the previous `Intent` do nothing. I.e. once the `FLAG_ONE_SHOT` `Intent` is used it turns into a nop `Intent()`. Therefore you should always use unique request codes. – Timmmm Dec 10 '12 at 11:13
  • It's not only the requestCode (second parameter). It's this or intent data (type, category, url ..etc). if either of those (or both) is different, the PendingIntent is considered different, and hence does not override a previous one. – kdehairy Jan 12 '16 at 17:50
  • I can't tell you how much this helped me, Save my day. – TapanHP Apr 30 '18 at 11:01
  • @Timmmm comment even wasn't the specific answer, but was a life savior! if launching multiple notifications with same intent but different bundles, even with different notification id, the intents bundle will be overwritten. The request code must be unique, in a way to prevent that. Thanks Mate! – Houssem Chlegou May 17 '19 at 23:10
3

In your case if second PendingIntent is "equal" to the previous one, it replaces it.

PendingIntents are equal if all of the following are the same:

  1. The requestCode (second parameter passed to the factory method)
  2. All of the Intent's data (type, uri, category..etc)

So, if you don't want the second PendingIntent to override the previous, change one of the list above.

kdehairy
  • 2,630
  • 22
  • 27
3

If the value of id in nm.notify(id, notif); is same then the Notification will be overwritten.

So you need to make sure that the id is different for different Notification

Vivek Khandelwal
  • 7,829
  • 3
  • 25
  • 40
  • 1
    yes the id is different every time and when i fire the first request to be fired after 2 mins and second after 3 mins then it displays single notification after 3 mins not after 2 min like this it updates. – Mahesh Mar 14 '12 at 11:17