简体   繁体   中英

How to display timestamp in Android Notifications for Honeycomb+ devices?

I'd like to show a timestamp in an Android Notification, just like the Android Design Guidelines suggest. (see the first snapshot, "12:03PM" is what I want!).

I tried many different ways, I thought setWhen would do it, but it only seems to affect the ordering in the Notification tray. Any idea how to achieve that?

See my code below:

  Notification.Builder builder = new Notification.Builder(context);
  builder.setContentIntent(pendingIntent)
    .setSmallIcon(R.drawable.ic_notify)
    .setLargeIcon(largeIcon)
    .setTicker(text)
    .setNumber(unreadCount)
    .setWhen((new Date()).getTime())
    .setAutoCancel(true)
    .setContentTitle(title)
    .setContentText(text)
    .setDefaults(Notification.DEFAULT_VIBRATE);

notificationManager.notify(NOTIFICATION_ID, builder.getNotification());

I don't want to use a custom layout for the notification.

I think I found the answer myself, when seems to be displayed only on Android 4.0. 3 and later. My wife's Nexus S (4.0.3) shows it, and I just upgraded to 4.0. 4 on my Galaxy Nexus and it magically started to work!

It also shows on older versions (2.3 for instance), but not on 4.0. 2 !

Use setContentInfo and pass your date as a string into it.

From this link

public Notification.Builder setContentInfo (CharSequence info) Since: API Level 11

Set the large text at the right-hand side of the notification.

Large Text at the right hand side of the notification should be where you want to put time.

Also from the same link:

public Notification.Builder setWhen (long when) Since: API Level 11

Set the time that the event occurred. Notifications in the panel are sorted by this time.

which means that setWhen only sorts the notifications, and doesn't set any text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM