Search code examples
androidnotificationsticker

Hide ticker text in notification using Notification.Builder


I'm trying to display an ongoing notification without the initial ticker text display. I was able to get this working with the older style Notification by setting ticker text to null in the constructor:

mNotification = new Notification(R.drawable.ic_stat_playing, null, System.currentTimeMillis());

However, I noticed that instantiating a Notification this way is now deprecated, and the use of Notification.Builder is recommended instead. However, I cannot get the notification now to display without the ticker text, even when I set ticker text to null:

Notification.Builder builder = new Notification.Builder(this);

CharSequence contentText = "contentText here";

Intent launchIntent = new Intent(this, MainActivity.class);

// The PendingIntent to launch our activity if the user selects this
// notification
PendingIntent contentIntent = PendingIntent.getActivity(this, -1,
                launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);

builder.setContentIntent(contentIntent)
    .setSmallIcon(R.drawable.ic_stat_playing)
    .setLargeIcon(null)
    .setTicker(null)
    .setOnlyAlertOnce(true)                 
    .setWhen(System.currentTimeMillis())
    .setContentTitle(contentTitle)
    .setOngoing(true)
    .setContentText(contentText);

mNotification = builder.getNotification();

startForeground(NOTIFICATION_ID, mNotification);

Is it just not possible to turn off the ticker display with the new Notification.Builder? Is so, that's unfortunate, since I won't be able to update from the deprecated code.

Edit - The code that finally worked:

mNotification = builder.getNotification();

mNotification.tickerView = null;

startForeground(NOTIFICATION_ID, mNotification);

Solution

  • try to set the tickerView to null after builder. that works fine for me Code like:

    Notification notif = builder.build();
    notif.tickerView = null;
    mNotificationManager.notify(id, notif);