当我的应用程序启动时,它会执行一个API调用,然后根据结果安排通知。这大约涉及到安排 ~10 个通知。实际通知上显示的时间戳似乎有问题。
由于我正在创建这些通知并使用
我尝试在我的
此外,在实际调用
然而,在上述情况下,似乎忽略了
由于我正在创建这些通知并使用
AlarmManager
安排闹钟,因此默认时间将是创建通知的时间 (System.currentTimeMillis()
)。我尝试在我的
Notification.Builder
上使用 .setWhen()
方法将其设置为我用于安排先前提到的闹钟的时间。这样稍微好一些,但是由于不能保证通知能够准确地在指定的时间传递,因此我经常收到过去几分钟的通知。此外,在实际调用
.notify()
之前,我尝试在我的 BroadcastReceiver
中手动覆盖通知中的 when
字段:public class NotificationPublisher extends BroadcastReceiver {
public static String NOTIFICATION_ID = "notification_id";
public static String NOTIFICATION = "notification";
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(NOTIFICATION);
notification.when = System.currentTimeMillis();
int id = intent.getIntExtra(NOTIFICATION_ID, 0);
notificationManager.notify(id, notification);
}
}
然而,在上述情况下,似乎忽略了
.when
。坦白地说,我只是在寻找一种方法,使通知显示的时间戳是实际显示的时间。
.setAutoCancel()
与默认的通知点击行为配合使用。您在构建通知时是否使用了.addAction()
或者有任何会将焦点从默认通知点击中移开的内容?如果是这样,您可能需要调用 Notification Manager 的.cancel(notificationId)
来解除通知。 - Sammy T