Question

I'm developing a messaging app that potentially creates multiple notifications. I want it such that these notifications can be updated and deleted when stuff happens.

For example, if I receive a message from A, the notification should say "You have 1 message from A". If another message arrives, it should say "You have 2 messages from A".

Then, if I receive a message from B, it should be a consolidated notification that simply says "You have messages" rather than a separate notification from each sender.

Also, when I click on the notification, it should cancel, and if I click on the user A chat window, any notification from user A should also cancel.

Right now I've implemented the code below for generating notifications:

PendingIntent pendingIntent = PendingIntent.getActivity(context, -1, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder noti = new NotificationCompat.Builder(
  context).setContentTitle(context.getResources().getString(R.string.app_name))
  .setContentText(notificationMessage)
  .setSmallIcon(R.drawable.ic_stat_notify)
  setContentIntent(pendingIntent)
  setAutoCancel(true)
  setWhen(System.currentTimeMillis())
  setDefaults(Notification.DEFAULT_ALL);
notificationManager.notify(notifyId, noti.build());

This creates the notification just fine, and cancels it when the user clicks on it. However, I'm unsure how to update notifications, consolidate them, and cancel the appropriate ones. Does anyone know?

Thanks.

Was it helpful?

Solution

You need to use the same Notification ID all the time

 public void notify(int id, Notification notification)

id - 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.

OTHER TIPS

Just post a new notification with the same ID as the old one, that will replace the old notification with your new one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top