NotificationCompat Ticker Text Not Showing When Updating Notification
Asked Answered
L

0

6

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:

Service notification startup on Android 2.3.3

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)

Android 2.2

Android 4.0.3

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.

Android 2.3.3

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?
Legate answered 8/5, 2013 at 16:11 Comment(12)
It should act the same way on Android 2.3.3, I already have checked you code snippets and they are working properly on emulator and device. Actually I was facing some issues with 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 exactly updateNotificationTicker is called?Halftimbered
@andrew_pako I am using android-support-v4.jar revision 12 (Feb 2013). updateNotificationTicker is called via a callback interface, in the example it is called by the BroadcastReceiver 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.Legate
@andrew_pako I also added the callback code to the question. See how the time shows on the content text, but the ticker never flashes on a 2.3.3 emulator or a 2.3.4 device.Legate
I didn't check you code properly. You are right setTicker has some issues on Android 2.3.3Halftimbered
Could it be that the ticker is only updated if the notification shade is not expanded? It's entirely possible they made this decision in 2.3, saw that it was wrong and reverted it in later versions. This is core behaviour and I doubt the SDK/support lib have anything to do with it.Kiloton
From my testing, the ticker will always show when the shade is open or closed on all versions (2.3 included - see 1st screenshot). However, the ticker does not show (on notification updates) on 2.3 whether the shade is open or closed.Legate
Same problem on 2.3.6Mareld
Where you able to workaround the bug?Mareld
@Mareld Unfortunately no.Legate
Some updates for this issue?Unsung
@OllieStrevel nope, however now that 2.3 market share is really dwindling, 4.0+ (14+) is becoming a common minimum API to support, so I haven't had to deal with this lately...Legate
Exactly, however there still many 2.3 devices, i hope won't have any problems in future with this issue, thanks a lot.Unsung

© 2022 - 2024 — McMap. All rights reserved.