David
David

Reputation: 7456

How do I redisplay Ticker Text?

For my app, I use one notification ID as to not clutter the Notifications Menu of a user. Each of my notifications have Ticker Text. When there are no notifications from my app, and the user gets notified, the ticker text displays. When a notification already exists, and is merely updated, the ticker text does not get displayed.

I made a work around which is very hacky, where I cancel the notification before I notify, but this ultimately causes a very obvious lag with vibrations.

Currently how I have notifications happening:

    mBuilder.setSmallIcon(R.drawable.actionbar_logo)
            .setContentTitle(extras.getString("title"))
            .setContentText(extras.getString("summary"))
            .setAutoCancel(true)
            .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(extras.getString("extended_text"))
            )
            .setLights(Color.WHITE, NOTIF_LIGHT_INTERVAL, NOTIF_LIGHT_INTERVAL)
            .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);

    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("mainActivityNotification", true);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 424242, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    mBuilder.setContentIntent(contentIntent);

    final Notification notification = mBuilder.build();
    notification.tickerText = extras.getString("title") + "\n" +
                              extras.getString("summary") + "\n" +
                              extras.getString("post_body");

    mNotificationManager.notify(GATE_NOTIFICATION_ID, notification);

Upvotes: 1

Views: 2603

Answers (1)

AADProgramming
AADProgramming

Reputation: 6345

Android does not display the ticker text again if it is the same as before.

You may use setTicker API of Notification.Builder class instead of constructing ticker text using Notification object, as Notification.Builder has been added to make it easier to construct Notifications.

And if your intentions is to re-display the same ticker text(from previously posted notification), then just add this Line :

mNotificationManager.notify(GATE_NOTIFICATION_ID, mBuilder.build());

After you posted your first notification.

So, your complete code looks like as below:

mBuilder.setSmallIcon(R.drawable.actionbar_logo)
            .setContentTitle(extras.getString("title"))
            .setContentText(extras.getString("summary"))
            .setAutoCancel(true)
            .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(extras.getString("extended_text"))
            )     //consider using setTicker here
            .setLights(Color.WHITE, NOTIF_LIGHT_INTERVAL, NOTIF_LIGHT_INTERVAL)
            .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);

    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("mainActivityNotification", true);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 424242, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    mBuilder.setContentIntent(contentIntent);

    final Notification notification = mBuilder.build();

    //consider using setTicker of Notification.Builder
    notification.tickerText = extras.getString("title") + "\n" +
                              extras.getString("summary") + "\n" +
                              extras.getString("post_body");

    mNotificationManager.notify(GATE_NOTIFICATION_ID, notification);
    //second time onward, add your changed content like setContentText, setTicker etc.
    mNotificationManager.notify(GATE_NOTIFICATION_ID, mBuilder.build());

Upvotes: 3

Related Questions