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.
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:
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
:
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.
NotificationCompat.Builder
in a way that is causing the ticker text to not show on Android 2.3.3?Notification
?NotificationCompat.Builder
not working properly on Android 2.3.3?