6

我尝试创建多个通知。如果单击通知,它将链接到另一个活动。在下面的代码之后,它创建了两行通知。但是当我点击通知的第一行时,它不起作用。只有第二个作品。

for (int i = 0; i < missionName.size(); i++) {
    mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    final Notification notifyDetails = new Notification(R.drawable.icon,
            "Mission Completed, Click Me!", System.currentTimeMillis());
    notifyDetails.defaults |= Notification.DEFAULT_SOUND;
    notifyDetails.defaults |= Notification.DEFAULT_VIBRATE;
    Context context = getApplicationContext();
    CharSequence contentTitle = missionName.get(i) + " is completed";
    CharSequence contentText = "Please click to view the mission";
    Intent notifyIntent = new Intent(getApplicationContext(),MissionMap.class);
    notifyIntent.putExtra("missionName", missionName.get(i));
    PendingIntent intent = PendingIntent.getActivity(ApplicationMenus.this,
            0, notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);

    notifyDetails.setLatestEventInfo(context, contentTitle, contentText,
            intent);
    mNotificationManager.notify(i, notifyDetails);
}

所以,请帮我找出我的错误。非常感谢。

4

2 回答 2

25

您应该使用不同的请求代码创建 PendingIntent。改变这个:

PendingIntent intent = PendingIntent.getActivity(ApplicationMenus.this,
        0, notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);

对此:

PendingIntent intent = PendingIntent.getActivity(ApplicationMenus.this,
        i, notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);

请注意,我已将第二个参数更改为循环索引 (i) 而不是 0。如果您使用相同的参数,则不会创建意图,而是使用具有相同参数的先前意图。

这应该可以解决您的问题。

于 2011-09-04T10:47:54.753 回答
2
    Random random = new Random();
    int randomNumber = random.nextInt(9999 - 1000) + 1000;       

    Intent resultIntent = new Intent(this, MainActivity.class);
    TaskStackBuilder TSB = TaskStackBuilder.create(this);
    TSB.addParentStack(MainActivity.class);

    TSB.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            TSB.getPendingIntent(
                    randomNumber,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );

    nb.setContentIntent(resultPendingIntent);
于 2017-07-25T11:43:16.820 回答