如何告诉PendingIntent是创建新广播还是更新现有广播

时间:2016-02-18 16:18:10

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

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

我想在约会前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将成为所有人的完全相同的类。

1 个答案:

答案 0 :(得分:0)

的第二个参数
PendingIntent.getBroadcast(context, 0 ,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

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

相关问题