Background
This is an app I am writing to familiarize myself with some of the APIs, it serves no real purpose other than to demonstrate some functionality in Android. I have a Service
that is running in the foreground (startForeground
), and has an ongoing Notification
that when tapped returns to the application. The Service
listens for broadcasts and logs them to a DB.
Start Service, Create Notification
I create my Notification
using NotificationCompat.Builder
in onCreate
of the Service
:
@Override
public void onCreate() {
super.onCreate();
Log.v(TAG, "onCreate");
// Get the notification manager to update notifications
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent openAppIntent = new Intent(this, MainActivity.class);
openAppIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent selectNotifPendingIntent = PendingIntent.getActivity(this, 0, openAppIntent, 0);
// Build the notification to show
mNotificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle("Monitor Service")
.setContentText("Logging system events")
.setTicker("Starting monitor service")
.setSmallIcon(R.drawable.ic_stat_cloud)
.setContentIntent(selectNotifPendingIntent)
.setOnlyAlertOnce(false)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setUsesChronometer(true);
// Start service in foreground
startForeground(MonitorService.NOTIFICATION_ID_FOREGROUND, mNotificationBuilder.build());
// more code....
}
When the Service
starts, the Notification
and ticker text are both shown on all versions of Android:
Next, Update Notification
I also added a callback interface
to my Service
, that would update the Notification
by changing the ticker text and content text:
@Override
public void updateNotificationTicker(String newTickerText) {
Log.d(TAG, "updateNotificationTicker");
if (mNotificationBuilder != null && mNotificationManager != null) {
mNotificationBuilder.setTicker(newTickerText);
mNotificationBuilder.setContentText(newTickerText);
mNotificationManager.notify(NOTIFICATION_ID_FOREGROUND, mNotificationBuilder.build());
}
}
To test updating the Notification
, I had my BroadcastReceiver
listen for time tick broadcasts, and call updateNotificationTicker
with the current time:
else if (action.equals(android.content.Intent.ACTION_TIME_TICK)) {
SimpleDateFormat sdf = new SimpleDateFormat("h:mm aa", Locale.US);
String message = "The time is now " + sdf.format(new Date());
newLogEntry.setMessage(message);
// Update the ticker notification with the time
if (mNotificationCallback != null) {
mNotificationCallback.updateNotificationTicker(message);
}
}
This works perfectly on (almost) every version of Android, as the ticker text flashes and the content text is updated on the Notification
:
- Android 2.2 (API 8)
- Android 4.0.3 (API 15)
- Android 4.1.2 (API 16)
- Android 4.2.2 (API 17)
Different Behavior on Android 2.3.3
When run on an Android 2.3.3 (API 10) emulator or device, the ticker text is initially shown when first starting the Service
(seen in the 1st screenshot: "Starting monitor service"). However, the ticker text is never shown when updating the Notification
, even though the content text does update.
Questions
- Am I setting up my
NotificationCompat.Builder
in a way that is causing the ticker text to not show on Android 2.3.3? - Or, does Android 2.3.3 behave differently from the other versions when showing the ticker text while updating an ongoing
Notification
? - Or, is this a defect with
NotificationCompat.Builder
not working properly on Android 2.3.3?
startForeground
notification previously, can't remember what exactly, but it was due to bug in support library and it was fixed in newer revision. So what is the revision of your support library and how exactlyupdateNotificationTicker
is called? – Halftimberedandroid-support-v4.jar
revision 12 (Feb 2013).updateNotificationTicker
is called via a callback interface, in the example it is called by theBroadcastReceiver
when it receives a time tick broadcast. However this info is moot, since I have tested updating the ticker and content text with a random string and still see the same results. On Android 2.3.3, the content text will update, but the ticker won't show anything, while all other versions show the content text and the ticker. – LegatesetTicker
has some issues on Android 2.3.3 – Halftimbered