【问题标题】:How to tell PendingIntent whether to create new or update existing broadcast如何告诉 PendingIntent 是创建新广播还是更新现有广播
【发布时间】:2016-02-18 16:18:09
【问题描述】:

想象一下我正在制作一个日历。所以人们可以安排约会,人们可以更新约会。人们也可以取消约会。

我想在约会前 15 分钟向人们发送通知。所以我有以下方法

public void scheduleNotification( Notification notification, long futureTimeInMillis, int id){
        Context context =MyApplication.getContext();
        Intent notificationIntent = new Intent(context,NotificationPublisher.class);
        notificationIntent.putExtra(NOTIFICATION_ID,id);
        notificationIntent.putExtra(NOTIFICATION,notification);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,futureTimeInMillis,pendingIntent);
    }

我的问题是PendingIntent.getBroadcast(context,0,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT)

  • 如果用户更新了现有约会,我希望新通知替换该约会的旧通知
  • 如果用户创建新约会,我不希望新通知替换任何现有通知。

那么我如何告诉 PendingIntent 是替换还是创建新的东西?显然,NotificationPublisher 将成为所有人的同一个班级。

【问题讨论】:

  • 只需为不同的按钮点击添加两种不同的方法,例如更新和新建
  • 问题的重点是PendingIntent 如何知道我是添加新通知还是更新现有通知
  • 有时使用不同的 ID 创建意图
  • 我是通知意图

标签: android push-notification android-notifications android-pendingintent


【解决方案1】:

第二个参数

PendingIntent.getBroadcast(context, 0 ,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

可以用来区分不同的PendingIntents。您应该为每个独特的警报使用不同的值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-21
    • 2015-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多