设置多个通知的通知意图

时间:2015-02-03 00:28:20

标签: android

我有这样的方法:

private void createNotification(String sender) {
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
        .setContentTitle(sender+" sent you a message")
        .setAutoCancel(true)
        .setSmallIcon(R.drawable.ribony_top_icon)
        .setContentText("Touch for reply")
        Intent resultIntent = new Intent(this, LoginActivity.class);
        resultIntent.putExtra("gcm_username",sender);
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent,  PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(NotificationID.getID(sender), mBuilder.build());
}

我正在创建3个这样的三个通知:

createNotification("test1");
createNotification("test2");
createNotification("test3");

通知创建没有任何问题。但是当我触摸通知test1test2时,它正在调用test3我的意思是所有通知意图设置为最后一个意图。我如何解决它?

2 个答案:

答案 0 :(得分:2)

  

如果PendingIntent具有相同的操作,操作,数据,类别,   组件和标志将被替换。

     

根据具体情况,我通常会提供一个独特的解决方案   请求代码作为静态值(0,1,2)或数据的行ID   我收到了DB。

PendingIntent.getActivity(context, MY_UNIQUE_VALUE , notificationIntent, PendingIntent.FLAG_ONE_SHOT);
  

然后我将notify()的相同唯一值用作

mNotificationManager.notify(MY_UNIQUE_VALUE, notification);

答案:Multiple notifications to the same activity

答案 1 :(得分:0)

这是因为当您创建PendingIntent时,最后一个参数指示是否对同一活动有其他待定意图,只是要更新而不是创建一个新的。您只需将最后一个参数更改为某些其他标志,或仅将0设置为表示不需要特殊标志。

PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, 0);

修改

在意图中添加额外消息。

resultIntent.putExtra("message_extra", sender);

并且在活动中仅恢复新的额外信息以显示消息。