如何在多个通知单击上显示相应的消息

时间:2014-01-20 07:34:04

标签: android notifications push-notification android-notifications

我的应用程序处理推送通知,并使用

在状态栏中显示多个通知
notificationmanager.notify(id, notification);

其中id是动态int。现在我想获得状态栏中点击通知的“id”。我通过

传递“id”
notificationIntent.putExtra("notifyId", id);

但它总是返回最新收到的通知的“id”。如何获得相应点击通知的“id”?

用于显示通知的代码:

    int icon = R.drawable.icon;
    long when = System.currentTimeMillis();
    Random generator = new Random();
    int i = generator.nextInt(10) + 1;

    // Set Notification Title
    String strtitle = getString(R.string.notificationtitle);
    // Set Notification Text
    String strtext = getString(R.string.notificationtext);

    Notification notification = new Notification(icon, strtext, when);

    // Open NotificationView Class on Notification Click
    Intent notificationIntent = new Intent(this, NotificationView.class);
    // Send data to NotificationView Class
    notificationIntent.putExtra("title", title);
    notificationIntent.putExtra("text", message);
    notificationIntent.putExtra("notifyId", i);

    PendingIntent intent = PendingIntent.getActivity(MyActivity.this, 0,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setLatestEventInfo(MyActivity.this, "Title",
            message, intent);

    // Create Notification Manager
    NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    // Build Notification with Notification Manager
    notificationmanager.notify(i, notification);

1 个答案:

答案 0 :(得分:0)

  1. 将标记从PendingIntent.FLAG_UPDATE_CURRENT更改为PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT
  2. 创建PendingIntent时使用不同的第二个参数