Home » Android » android – New PendingIntent updates current intent

android – New PendingIntent updates current intent

Posted by: admin June 15, 2020 Leave a comment

Questions:

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

How to&Answers:

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

Answer:

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);

Answer:

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.