7

I have developing an App where the user can create event and set notification for that very event. So I want to add multiple notification. I am using the following code.

final Notification notifyDetails = new Notification(R.drawable.icon, "Myapp",calendar.getTimeInMillis());
Context context = getApplicationContext();
Intent notifyIntent = new Intent(context, ViewDoughnut.class);
PendingIntent pendingIntent = PendingIntent.getActivity(ViewCal.this, 0, notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
notifyDetails.setLatestEventInfo(context, contentTitle, contentText, pendingIntent);
notifyDetails.flags = Notification.FLAG_ONGOING_EVENT;
mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);

When I add an event and creating a notification using the above code, it is working fine. But if I add another event, no new notification created, the old one is just updated. I want to add one more notification. How to do it? Moreover, I want to delete any specific notification if the user delete the event corresponding to it. How it is possible?

dev_android
  • 7,874
  • 21
  • 84
  • 143

2 Answers2

12

I assume that SIMPLE_NOTIFICATION_ID is a constant? To have separate notifications you need to use a different ID for each one.

Dan Dyer
  • 51,823
  • 16
  • 125
  • 165
  • but how to cancel any notification when the user is deleting the corresponding event? If i have tried with mNotificationManager.cancel(editEventid), it is not working at all. – dev_android Mar 08 '11 at 12:20
  • @user525004 If you pass the same ID to the cancel method as you used when you created the notification then it should work (works for me in my app). If your events already have their own unique IDs just use these for the notification IDs. – Dan Dyer Mar 08 '11 at 16:21
  • how do I make the ID be different? to +1 each time a notification creates by it self, can you show me some code? – baTimá Oct 15 '12 at 17:07
  • new Random(System.currentTimeMillis()).nextInt() – wutzebaer Jul 31 '13 at 21:27
0

Below is the code for pass unique notification id:

//"CommonUtilities.getValudeFromOreference" is the method created by me to get value from savedPreferences.
String notificationId = CommonUtilities.getValueFromPreference(context, Global.NOTIFICATION_ID, "0");
int notificationIdinInt = Integer.parseInt(notificationId);

notificationManager.notify(notificationIdinInt, notification);

// will increment notification id for uniqueness
notificationIdinInt = notificationIdinInt + 1;
CommonUtilities.saveValueToPreference(context, Global.NOTIFICATION_ID, notificationIdinInt + "");
//Above "CommonUtilities.saveValueToPreference" is the method created by me to save new value in savePreferences.

Let me know if you need more detail information or any query. :)

Gaurav Darji
  • 488
  • 5
  • 12