简体   繁体   English

如何更新Android通知的时间

[英]How to update time of an Android notification

I've got an Android notification which is updated every few minutes. 我有一个Android通知,每隔几分钟就会更新一次。

Firstly I create a Builder like that: 首先,我创建一个这样的Builder

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);

And then, for first time when I show it and every time I update it, I use this code: 然后,当我第一次显示它并且每次更新它时,我都使用以下代码:

mBuilder.setContentTitle("Title");
mBuilder.setContentText("Text");
NotificationManager manager = (NotificationManager)
        context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, mBuilder.build());

But in the upper right corner of notification, Android still shows the time of when it was first displayed to the user. 但是在通知的右上角,Android仍会显示首次向用户显示的时间。 I'd like it to show the time of the last update . 我希望它显示上次更新的时间 I know that it's possible because Facebook's Messenger app does that - changes the displayed time when a new message is sent. 我知道这是可能的,因为Facebook的Messenger应用程序可以做到这一点 - 更改发送新消息时的显示时间。

How can I achieve that? 我怎样才能做到这一点?

Currently: 目前: 测试

You're probably looking for NotificationCompat.Builder#setWhen(long) . 您可能正在寻找NotificationCompat.Builder#setWhen(long) Supplying it with System.currentTimeMillis() should update the timestamp to the current time. 使用System.currentTimeMillis()提供它应该将时间戳更新为当前时间。

In addition if you want that timestamp to appear on Android N or higher, you need to call NotificationCompat.Builder#setShowWhen(true) at some point because it defaults to false. 此外,如果您希望时间戳显示在Android N或更高版本上,则需要在某个时刻调用NotificationCompat.Builder#setShowWhen(true) ,因为它默认为false。

Source: https://developer.android.com/reference/android/app/Notification.html#when 资料来源: https//developer.android.com/reference/android/app/Notification.html#when

您可以使用以下代码显示时间

val notificationBuilder = NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle(title) .setContentText(body) .setColor(resources.getColor(R.color.colorPrimary)) .setAutoCancel(true) .setSound(soundUri) .setContentIntent(pendingIntent) .setWhen(System.currentTimeMillis())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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