When my application is launched, it performs an API call and then schedules notifications based on the results. This amounts to around ~10 notifications being scheduled. There seems to be an issue with the timestamp displayed on the actual notification being incorrect.
Since I am creating these notifications and then scheduling an alarm with an AlarmManager
, the default time present on the notification will be the time at which the notification is created (System.currentTimeMillis()
).
I've tried to use the .setWhen()
method on my Notification.Builder
to set it to the time I am using to schedule the previously mentioned alarm. This is a little better, however, because notifications are not guaranteed to be delivered at the exact time specified, I often get notifications a few minutes in the past.
Additionally, I tried to manually override the when
field on the notification in my BroadcastReceiver
, right before .notify()
is actually called:
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);
}
}
However, in the above scenario, it seems that .when
is ignored.
Frankly, I am simply looking for a way to have the timestamp displayed on the notification be the time at which it is actually displayed.
Your NotificationPublisher's
onReceive()
method will beinvoked
only when scheduledalarm
triggers as specifiedtime
. When you crate a notification fromonReceive()
method, it will definitely show the current time. No need to require to use.when
or.setWhen()
method.Try this one:
If you want to redirect to an
activity
when click onNotification
, then you can usePendingIntent
and set it to yourNotification
.Hope this will help~
I would suggest passing in your notification's information as extras then building the notification inside of the BroadcastReceiver. This will build the notification just before it is issued, so it will have the same time your AlarmManager triggers the BroadcastReceiver.
From wherever you're scheduling the notification:
Then, inside your BroadcastReceiver's onReceive: