Question

I am using Notification Manager for content downloading and show the percent of completed download, but each time i call displaymessage function with new percent it creates new notification. How can i just update the notification without creating new one each time?

public void displaymessage(String string) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.icon;
CharSequence tickerText = "Shamir Download Service";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "Downloading Content:";
notification.setLatestEventInfo(context, contentTitle, string, null);
final int HELLO_ID = 2;
mNotificationManager.notify(HELLO_ID, notification);

}
Was it helpful?

Solution

What I did was store the notification in a class level variable.. Maybe change your function to createDownloadNotification and use what you have above, except make notification a variable accessible to the whole class.

Then have another function (something like updateDownloadNotification) which will call setLatestEventInfo on the notification with the updated information.

Also note that you need to call mNotificationManager.notify(HELLO_ID, notification); after each update or nothing will change.

--- Update --- Actually you could just have 1 function and check if notification is null (if not,create it) otherwise use what you already have.

Example:

public class YourClass extends Service { //or it may extend Activity

private Notification mNotification = null;

public void displaymessage(String string) {
  String ns = Context.NOTIFICATION_SERVICE;

  NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
  int icon = R.drawable.icon;
  CharSequence tickerText = "Shamir Download Service";
  long when = System.currentTimeMillis();
  if (mNotification == null) {
    mNotification = new Notification(icon, tickerText, when);
  }
 //mNotification.when = when;//not sure if you need to update this try it both ways
  Context context = getApplicationContext();
  CharSequence contentTitle = "Downloading Content:";
  notification.setLatestEventInfo(context, contentTitle, string, null);
  final int HELLO_ID = 2;
  mNotificationManager.notify(HELLO_ID, mNotification);

}

My code is actually a bit different in that all I'm updating is the notification's iconLevel on each update, so I'm not sure if with each change if you need to update mNotification.when

Try it and see and report back.

Also I would make some variables out of this function.. Usually you name a variable mSomething if it is a private instance variable of the class. Here is what I would recommend:

private Notification mNotification = null;
private NotificationManager mNotificationManager = null;
private static final int HELLO_ID = 2;

public void displaymessage(String string) {
  String ns = Context.NOTIFICATION_SERVICE;
  if (mNotificationmanager == null) {
      mNotificationManager = (NotificationManager) getSystemService(ns);
  }
  int icon = R.drawable.icon;
  CharSequence tickerText = "Shamir Download Service";
  long when = System.currentTimeMillis();
  if (mNotification == null) {
    mNotification = new Notification(icon, tickerText, when);
  }
  //mNotification.when = when;//not sure if you need to update this try it both ways
  Context context = getApplicationContext();      
  notification.setLatestEventInfo(context, contentTitle, string, null);      
  mNotificationManager.notify(HELLO_ID, mNotification);

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