【问题标题】:Setting notification intent for multiple notifications为多个通知设置通知意图
【发布时间】:2015-02-03 00:28:19
【问题描述】:

我有这样的方法:

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

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

【问题讨论】:

    标签: android


    【解决方案1】:

    如果 PendingIntent 具有相同的操作、动作、数据、类别, 组件,并标记它将被替换。

    根据情况,我通常通过提供独特的解决方案来解决这个问题 请求代码作为静态值 (0,1,2) 或数据的行 ID 我从数据库接收。

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

    然后我使用与 notify() 相同的唯一值

    mNotificationManager.notify(MY_UNIQUE_VALUE, notification);
    

    回答:Multiple notifications to the same activity

    【讨论】:

      【解决方案2】:

      发生这种情况是因为当您创建 PendingIntent 时,最后一个参数指示是否对同一活动有其他待处理的意图,只是将被更新而不是创建一个新的。您只需将最后一个参数更改为其他一些标志或只输入 0 表示不需要特殊标志。

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

      编辑

      在意图中添加额外的消息。

      resultIntent.putExtra("message_extra", sender);
      

      并且在 Activity 中只恢复新的 extra 以显示消息。

      【讨论】:

      • 现在一切都恢复了。第一个意图设置为所有通知。
      • 然后你需要像额外的一样传递每个意图中的消息并在另一端恢复消息。
      • 你能解释更多吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多