[Android] Update the Notification Text

In recent days, I found out my application always shows the new notification in the status bar. This is not right in my scenario. So, I try to solve the problem about how to update the notification text.

Here is a short example to describe how to create the notification.

public void refresh() {
mTicketText = mContext.getString(R.string.service_start_notification);
RemoteViews contentView = new RemoteViews(
mContext.getPackageName(), R.layout.notification);
contentView.setImageViewResource(R.id.notification_icon, mIcon);

mNotification.icon = mIcon;
mNotification.tickerText = mTicketText;
mNotification.flags = Notification.FLAG_NO_CLEAR
| Notification.FLAG_ONLY_ALERT_ONCE
| Notification.FLAG_FOREGROUND_SERVICE;

/* Set title */
final String title = mTicketText;
contentView.setTextViewText(R.id.title, title);
/* Set action summary */
String summary = mContext.getString(R.string.launch_summary);
contentView.setTextViewText(R.id.text, summary);
mNotification.contentView = contentView;

Intent notificationIntent = new Intent(mContext,
FingerTranslateSettings.class);
PendingIntent contentIntent = PendingIntent.getActivity(mContext,
0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mNotification.contentIntent = contentIntent;

mNotificationManager.notify(NOTIFICATION_ID, mNotification);
}

I always use this example to create and to update my notification. But, when I just want to update the notification text, the notification will be re-created. This seems a noise. Therefore, I try to find out the reason about the problem.

Finally, I see the description of Notification in the Android SDK.

public CharSequence tickerText

Since: API Level 1
Text to scroll across the screen when this item is added to the status bar on large and smaller devices.
See Also

It seems the tickerText is scrolled across the screen. So, when you change the tickerText, Android will scroll the new text on the screen. This behavior is not re-created the notification, it just refreshes the newer tickerText. For this reason, we can arrange the initialize the tickerText to the constructor, then the workflow will same as my idea.

One comment

發佈留言