Updating an existing notification

Now that the method has been setLatestEventInfo

deprecated in the latest Android SDK, is there a way we should 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 content of the notifications.

The notification guide doesn't seem to have been updated to use the suggested one Notification.Builder

.

+3


source to share


2 answers


The Notification class has many public fields that you can modify directly by simply maintaining a reference to it. Its other characteristics are clearly not intended to be changed (this may confuse the user if the Notice changes significantly).

You will 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

+2


source


Yes, you can update the content of an existing notification. But you rearranged it in a similar way and with the same notification ID. Also remove all relevant flags and set the default priority.

The same notification ID ensures that no new one is created. Flags like vibrations and lights may be inappropriate as the notification has already been triggered. The priority is set to the default so that the position of the notification in the notification tray does not change. If priority is set to high, it moves to the top of the tray.

This is how I have implemented. * I 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);
}

      

Thus, the content of an existing notification can be changed.

0


source







All Articles