Reputation: 171
Now that the setLatestEventInfo
method is deprecated in the latest Android SDK, is there a way that we're supposed to update the content of an existing notification? I am skeptical that they expect us to create a new notification every time we want to update the notification content.
The Notification Guide doesn't appear to have been updated to make use of the suggested Notification.Builder
class either.
Upvotes: 3
Views: 1514
Reputation: 10929
Yes you can update the content of an existing notification. But you have rebuild it similarly and with the same notification id. Also remove all the relevant flags and set priority to default.
The same notification id ensures that a new one is not created. The flags like vibrate, and lights may not be appropriate as the notification has already been launched. The priority is set to default so that the position of the notification in the notification tray does not change. If the priority is set to high then it will move to the top of the tray.
Here is how I implemented. *I have just added the relevant code
public void showNotification(String action){
RemoteViews views = new RemoteViews(getPackageName(),
R.layout.status_bar);
Notification status;
if(action.equals("play")){
views.setImageViewResource(R.id.status_bar_play, R.drawable.play);
status = notificationBuilder
.setSmallIcon(R.drawable.ic_launcher)
.setPriority(Notification.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.setContent(views)
.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS)
.build();
} else {
views.setImageViewResource(R.id.status_bar_play, R.drawable.pause);
status = notificationBuilder
.setSmallIcon(R.drawable.ic_launcher)
.setPriority(Notification.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setContent(views)
.build();
}
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Constants.NOTIFICATION_ID, status);
}
This way the contents of the existing notification can be modified.
Upvotes: 0
Reputation: 28509
There are many public fields on the Notification class that you can alter directly, just by keeping a reference to it. The other characteristics of it, are clearly not designed to be altered (it could confuse the user if a Notification changes substantially).
You'll need to either use the deprecated method, or rebuild the Notification and display a new one.
http://developer.android.com/reference/android/app/Notification.html
Upvotes: 2