【问题标题】:Incorrect timestamp on future notifications未来通知的时间戳不正确
【发布时间】:2017-01-26 20:08:30
【问题描述】:

当我的应用程序启动时,它会执行 API 调用,然后根据结果安排通知。这相当于安排了大约 10 个通知。实际通知上显示的时间戳似乎存在问题。

由于我正在创建这些通知,然后使用 AlarmManager 安排警报,因此通知上的默认时间将是创建通知的时间 (System.currentTimeMillis())。

我尝试在Notification.Builder 上使用.setWhen() 方法将其设置为我用来安排前面提到的警报的时间。不过这样会好一些,因为不能保证通知会在指定的确切时间送达,我经常会在过去几分钟收到通知。

此外,我尝试在BroadcastReceiver 中的通知上手动覆盖when 字段,就在实际调用.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);

    }
}

但是,在上述场景中,.when 似乎被忽略了。

坦率地说,我只是在寻找一种方法,让通知上显示的时间戳成为实际显示的时间。

【问题讨论】:

    标签: android broadcastreceiver alarmmanager android-notifications android-pendingintent


    【解决方案1】:

    我建议将您的通知信息作为附加信息传递,然后在 BroadcastReceiver 中构建通知。这将在发出通知之前构建通知,因此它与您的 AlarmManager 触发 BroadcastReceiver 的时间相同。

    从您安排通知的任何位置:

    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 中:

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

    【讨论】:

    • 你能告诉我如何创建多个通知吗?可以通过提供 mNotificationId 来完成吗?那么我需要取消相同的!
    • 还有一个问题。如果我点击通知。我移动到新的活动,但通知仍然存在。如何清除? setAutoCancel(true) 不起作用。而已。提前致谢。
    • @xbadal 是的,要创建多个通知,您必须为每个通知提供不同的 ID。据我了解,.setAutoCancel() 适用于默认的通知点击行为。您是否在构建通知时使用了.addAction(),或者您是否有任何可以将焦点从默认通知单击中移开的东西?如果是这样,您可能需要致电通知管理器的.cancel(notificationId) 来关闭通知。
    • 取消我使用的标签和ID与用于创建通知的标签和ID相同。它现在工作。是的,我使用 .addAction() 关闭点击我使用了 notification.flags |= Notification.FLAG_AUTO_CANCEL;所有问题都已解决。谢谢@sammy
    【解决方案2】:

    您的 NotificationPublisher 的 onReceive() 方法将是 invoked 仅当预定 alarm 触发指定 time。当您从onReceive() 方法创建通知时,它肯定会显示当前时间。无需使用.when 或.setWhen() 方法。

    试试这个:

    public class NotificationPublisher extends BroadcastReceiver {
    
        public static String NOTIFICATION_ID = "notification_id";
        public static String NOTIFICATION = "notification";
    
        public void onReceive(Context context, Intent intent) {
    
            int id = intent.getIntExtra(NOTIFICATION_ID, 0);
    
            // Notification
            Notification notification = new Notification.Builder(context)       
                .setContentTitle("This is notification title")
                .setContentText("This is notification text")
                .setSmallIcon(R.mipmap.ic_launcher).build();
    
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
    
            // Notification Manager
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    
            notificationManager .notify(id, notification);
        }
    }
    

    如果您想在单击Notification 时重定向到activity,则可以使用PendingIntent 并将其设置为您的Notification。

    public class NotificationPublisher extends BroadcastReceiver {
    
        public static String NOTIFICATION_ID = "notification_id";
        public static String NOTIFICATION = "notification";
    
        public void onReceive(Context context, Intent intent) {
    
            int id = intent.getIntExtra(NOTIFICATION_ID, 0);
    
            Intent intent = new Intent(context, YourTargetActivity.class);
            intent.putExtra("KEY_ID", id); // Pass extra values if needed
    
            PendingIntent pI = PendingIntent.getActivity(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    
            // Notification
            Notification notification = new Notification.Builder(context)       
                .setContentTitle("This is notification title")
                .setContentText("This is notification text")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pI).build();
    
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
    
            // Notification Manager
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    
            notificationManager .notify(id, notification);
        }
    }
    

    希望对你有帮助~

    【讨论】:

      【解决方案3】:

      我也为此苦苦挣扎了一段时间,但是您的问题实际上使我得到了最佳答案。我检查了 setWhen() ,现在看起来效果很好(使用 API lvl 30 和 31 检查)。由于这篇文章已经有几年了,也许这个问题在此期间已经解决了。下面是我在 Kotlin 中的做法:

      class NotificationPublisher : BroadcastReceiver() {
      override fun onReceive(context: Context, intent: Intent) {
          val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
          val notification = intent.getParcelableExtra<Notification>(NOTIFICATION)
          notification?.`when` = System.currentTimeMillis()                               // overwriting the creation time to show the current trigger time when the notification is shown
          val postId = intent.getIntExtra(NOTIFICATION_ID, 0)
      
          notificationManager.notify(postId, notification)
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-01-25
        • 1970-01-01
        • 2019-07-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-20
        相关资源
        最近更新 更多