未来通知的时间戳不正确 [英] Incorrect timestamp on future notifications

查看:102
本文介绍了未来通知的时间戳不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

启动我的应用程序时,它会执行API调用,然后根据结果安排通知。总计约有10条通知正在计划中。实际的通知上显示的时间戳似乎不正确。

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.

由于我正在创建这些通知,然后安排一个 AlarmManager ,通知上显示的默认时间将是创建通知的时间( System.currentTimeMillis())。

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()).

我尝试在 Notification.Builder <上使用 .setWhen()方法/ code>将其设置为我用来安排前面提到的警报的时间。但是,这样做要好一些,因为不能保证一定在指定的时间发送通知,所以我常常在过去几分钟收到通知。

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.

此外,我尝试在之前的 BroadcastReceiver 中手动覆盖通知中的 when 字段。 notify()实际上被称为:

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);

    }
}

但是,在上述情况下,似乎 .when 会被忽略。

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.

推荐答案

我建议您将通知的信息作为附加信息传递,然后构建BroadcastReceiver内部的通知。

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.

这将在发出通知之前立即构建通知,从而使AlarmManager在同一时间触发广播接收器。 p>

From wherever you're scheduling the notification:

private void scheduleNotification(){

    // Create an intent to the broadcast receiver you will send the notification from
    Intent notificationIntent = new Intent(this, SendNotification.class);

    // Pass your extra information in
    notificationIntent.putExtra("notification_extra", "any extra information to pass in");

    int requestCode = 1;

    // Create a pending intent to handle the broadcast intent
    PendingIntent alarmIntent = PendingIntent
                .getBroadcast(this, requestCode, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    // Set your notification's trigger time
    Calendar alarmStart = Calendar.getInstance();
    alarmStart.setTimeInMillis(System.currentTimeMillis());
    alarmStart.set(Calendar.HOUR_OF_DAY, 6); // This example is set to approximately 6am

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    // Set the alarm with the pending intent
    // be sure to use set, setExact, setRepeating, & setInexactRepeating 
    // as well as RTC_WAKEUP, ELAPSED_REALTIME_WAKEUP, etc. 
    // where appropriate
    alarmManager.set(AlarmManager.RTC_WAKEUP, alarmStart.getTimeInMillis(), alarmIntent);
}

然后,在BroadcastReceiver的onReceive内部:

Then, inside your BroadcastReceiver's onReceive:

String notificationExtra = null;

// Retrieve your extra data
if(intent.hasExtra("notification_extra")){
    notificationExtra = intent.getStringExtra("notification_extra");
}

//Build the notification
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setSmallIcon(notificationIcon)
        .setContentTitle(notificationTitle)
        .setContentText(notificationMessage)
        .setAutoCancel(true); // Use AutoCancel true to dismiss the notification when selected

// Check if notificationExtra has a value
if(notificationExtra != null){
    // Use the value to build onto the notification
}

//Define the notification's action
Intent resultIntent = new Intent(context, MainActivity.class); // This example opens MainActivity when clicked

int requestCode = 0;

PendingIntent resultPendingIntent =
        PendingIntent.getActivity(
                context,
                requestCode,
                resultIntent,
                PendingIntent.FLAG_UPDATE_CURRENT
        );

//Set notification's click behavior
mBuilder.setContentIntent(resultPendingIntent);

// Sets an ID for the notification
int mNotificationId = 1;

// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());

这篇关于未来通知的时间戳不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆