How to update notification text for foreground service in Android?

I have a foreground setting in Android. I would like to update the notification text. I am creating a service as shown below.

How do I update the notification text that is configured in this front-end service? What is the best practice for updating notification? Any sample code would be appreciated.

public class NotificationService extends Service { private static final int ONGOING_NOTIFICATION = 1; private Notification notification; @Override public void onCreate() { super.onCreate(); this.notification = new Notification(R.drawable.statusbar, getText(R.string.app_name), System.currentTimeMillis()); Intent notificationIntent = new Intent(this, AbList.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); this.notification.setLatestEventInfo(this, getText(R.string.app_name), "Update This Text", pendingIntent); startForeground(ONGOING_NOTIFICATION, this.notification); } 

I create a service in my main action, as shown below:

  // Start Notification Service Intent serviceIntent = new Intent(this, NotificationService.class); startService(serviceIntent); 
+99
android service notifications foreground
Apr 03 2018-11-11T00:
source share
4 answers

I think that startForeground() calling startForeground() with the same unique identifier and Notification with the new information will work, although I have not tried this script.

Update: Based on comments, you should use NotifcationManager to update notifications, and your service continues to be in foreground mode. Take a look at the answer below.

+47
Apr 03 2018-11-11T00:
source share

If you want to update the notification set using startForeground (), simply create a new notification and then use the NotificationManager to notify it.

The key point is to use the same notification identifier.

I have not tested the startForeground () callback script to update Notification, but I think it's better to use NotificationManager.notify.

Updating Notifications will NOT remove the Service from the foreground status (this can only be done by calling stopForground);

Example:

 private static final int NOTIF_ID=1; @Override public void onCreate (){ this.startForeground(); } private void startForeground() { startForeground(NOTIF_ID, getMyActivityNotification("")); } private Notification getMyActivityNotification(String text){ // The PendingIntent to launch our activity if the user selects // this notification CharSequence title = getText(R.string.title_activity); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MyActivity.class), 0); return new Notification.Builder(this) .setContentTitle(title) .setContentText(text) .setSmallIcon(R.drawable.ic_launcher_b3) .setContentIntent(contentIntent).getNotification(); } /** * This is the method that can be called to update the Notification */ private void updateNotification() { String text = "Some text that will update the notification"; Notification notification = getMyActivityNotification(text); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(NOTIF_ID, notification); } 

The documentation reads

To configure a notification so that it can be updated, enter it with the notification ID by calling NotificationManager.notify() . To update this notification after issuing it, update or create a NotificationCompat.Builder object, create a Notification object from it, and run a Notification with the same identifier that you used earlier. If the previous notification is still displayed, the system updates it from the contents of the Notification object. If the previous notification was rejected, a new notification is created instead.

+161
Nov 22 '13 at
source share

Improving Luca Manzo's response in android 8. 0+ when updating a notification, it will sound and show as Heads-up.
to prevent this you need to add setOnlyAlertOnce(true)

so the code is:

 private static final int NOTIF_ID=1; @Override public void onCreate(){ this.startForeground(); } private void startForeground(){ startForeground(NOTIF_ID,getMyActivityNotification("")); } private Notification getMyActivityNotification(String text){ if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){ ((NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel( NotificationChannel("timer_notification","Timer Notification",NotificationManager.IMPORTANCE_HIGH)) } // The PendingIntent to launch our activity if the user selects // this notification PendingIntent contentIntent=PendingIntent.getActivity(this, 0,new Intent(this,MyActivity.class),0); return new NotificationCompat.Builder(this,"my_channel_01") .setContentTitle("some title") .setContentText(text) .setOnlyAlertOnce(true) // so when data is updated don't make sound and alert in android 8.0+ .setOngoing(true) .setSmallIcon(R.drawable.ic_launcher_b3) .setContentIntent(contentIntent) .build(); } /** * This is the method that can be called to update the Notification */ private void updateNotification(){ String text="Some text that will update the notification"; Notification notification=getMyActivityNotification(text); NotificationManager mNotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(NOTIF_ID,notification); } 
+5
Mar 12 '18 at 14:48
source share

here is the code for in your service . Create a new notification, but ask the notification manager to notify the same notification identifier that you used in startForeground.

 Notification notify = createNotification(); final NotificationManager notificationManager = (NotificationManager) getApplicationContext() .getSystemService(getApplicationContext().NOTIFICATION_SERVICE); notificationManager.notify(ONGOING_NOTIFICATION, notify); 

For complete code samples, you can check here:

https://github.com/plateaukao/AutoScreenOnOff/blob/master/src/com/danielkao/autoscreenonoff/SensorMonitorService.java

+4
Jun 06 '13 at 16:53
source share



All Articles