NotificationCompat notification text that does not appear when updating a notification

Background

This is an application that I am writing to familiarize myself with some of the APIs, it serves no real purpose other than to demonstrate some features in Android. I have a Service that runs in the foreground ( startForeground ), and has a current Notification , which when clicked returns to the application. Service listens for broadcasts and writes them to the database.

Start the service, create a notification

I create my Notification using NotificationCompat.Builder in the onCreate 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 Service starts, the Notification text and ticker are displayed in all versions of Android:

Service notification startup on Android 2.3.3

Next, Update Notification

I also added an interface callback to my Service , which would Notification by changing the text and text of the ticker 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()); } } 

In order to test the Notification update, I had a BroadcastReceiver listen to the timeout 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 fine (almost) in every version of Android, when the ticker text blinks and the content text is updated to 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

Miscellaneous behavior on Android 2.3.3

When running on an emulator or Android 2.3.3 device (API 10), the text of the ticker is first displayed at the first start of the Service (see the 1st screenshot: "Starting the monitoring service"). However, the text of the ticker is never displayed when updating Notification , even if the text of the content is being updated.

Android 2.3.3

Questions

  • Am I setting up my NotificationCompat.Builder so that the ticker text does not display on Android 2.3.3?
  • Or does Android 2.3.3 behave differently than other versions when the text of the ticker is displayed when updating the current Notification ?
  • Or is this a defect with NotificationCompat.Builder not working properly on Android 2.3.3?
+6
source share

Source: https://habr.com/ru/post/944545/


All Articles