启动我的应用程序后,它会执行API调用,然后根据结果安排通知.这相当于约10个预定的通知.实际通知上显示的时间戳似乎存在问题.
由于我正在创建这些通知,然后使用a安排警报,AlarmManager因此通知上的默认时间将是创建通知的时间(System.currentTimeMillis()).
我试图使用.setWhen()我的方法Notification.Builder将其设置为我用来安排前面提到的警报的时间.然而,这稍微好一些,因为通知不能保证在指定的确切时间发送,我经常在过去几分钟收到通知.
另外,我尝试手动覆盖when我的通知中的字段BroadcastReceiver,在.notify()实际调用之前:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,在上面的场景中,似乎.when被忽略了.
坦率地说,我只是想找到一种方法,让通知上显示的时间戳是实际显示的时间.
android broadcastreceiver alarmmanager android-notifications android-pendingintent