1

I am writing an application and in this application I need to set multiple notification with same intent just like open my application whenever user tap on any notification.

All the notification comes on different time and date without having any data but the problem is, if I set two notification for 03:27 PM and 03:28 PM then the first notification (03:27 PM) is canceled (Overwritten by second) and second is working correctly.

I am currently using this code for achieving this goal:

this method is used to set notification from Activity:

 public static void setNotificationOnDateTime(Context context, long fireTime)
     {
         int requestID = (int) System.currentTimeMillis();

         AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
         Intent i = new Intent(context, NotificationReceiver.class);

         PendingIntent pi = PendingIntent.getBroadcast(context, requestID, i, 0);
         am.set(AlarmManager.RTC_WAKEUP, fireTime, pi);
     }

and my NotificationReceiver class look like this:

 NotificationManager nm;

 @Override
 public void onReceive(Context context, Intent intent) {
     Log.w("TAG", "Notification fired...");

     nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

     PendingIntent contentIntent;

     Bundle bundle = intent.getExtras().getBundle("NotificationBundle");
     if(bundle == null)
     {
         contentIntent = PendingIntent.getActivity(context, 0,
                 new Intent(context, SplashScreen.class), 0);
     }

     else
     {
         contentIntent = PendingIntent.getActivity(context, 0,
                 new Intent(context, MenuScreen.class)
         .putExtra("NotificationBundle", bundle), 0);
     }

     Notification notif = new Notification(R.drawable.ic_launcher,
             "Crazy About Android...", System.currentTimeMillis());
     notif.setLatestEventInfo(context, "Me", "Message test", contentIntent);
     notif.flags |= Notification.FLAG_AUTO_CANCEL;
     nm.notify(1, notif);
 }

I alerady spend a lot time on googling and found some solutions but they didn't work for me here is the link of some of them:

android pending intent notification problem

Multiple notifications to the same activity

If any one knows how to do this please help me.

Community
  • 1
  • 1
Pari
  • 1,705
  • 4
  • 18
  • 19

3 Answers3

1

Quote:

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.

But you're always using 1 for the id parameter. Use a unique ID when you post several notifications.

Update If that doesn't help, you can still create Intents which do not compare as being equal, while having an equal effect.

class stacker
  • 5,357
  • 2
  • 32
  • 65
  • Thanks for your precious information, but how can I change notification id in NotificationReceiver class. onReceive called when receiver receive a notification. am I right? – Pari Feb 22 '13 at 10:58
  • @Pari You're basically already doing such a trick, although in a place where it's wasted: You generate a `requestID` which is, with a relatively high probability, unique, and pass it to `PendingIntent.getBroadcast()`, but according to the documentation, this is "currently not used". You can do the same trick for the `id` parameter of `notify()`. However, this makes only sense if you do not have to be able to identify the notifications based on their ID (in which case you'll need either consecutive IDs or a list of IDs you issued or something similar). But it will solve your problem. – class stacker Feb 22 '13 at 11:07
  • @Pari I have also updated my answer with an additional suggestion. – class stacker Feb 22 '13 at 11:32
0

this may be helpful

notif.flags |= Notification.FLAG_ONGOING_EVENT;

the notification will never close...

Tamilselvan Kalimuthu
  • 1,534
  • 1
  • 13
  • 28
0

Try to set as below :

  contentIntent=PendingIntent.getActivity(p_context, i, new Intent(context, MenuScreen.class),PendingIntent.FLAG_CANCEL_CURRENT);

It might help you.

GrIsHu
  • 29,068
  • 10
  • 64
  • 102