第二个通知不起作用

发布于 2024-12-03 00:34:07 字数 1167 浏览 0 评论 0原文

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

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

所以,请帮我找出我的错误。太感谢了。

I try to create multiple notifications. If click on the notification, it will link
to another Activity. After following code, it's create two lines of notification.
But when I click to the first line of notification, it's not working.
Only second works.

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

So, please help me to find my mistake. Thank you so much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吻泪 2024-12-10 00:34:07

您应该使用不同的请求代码创建 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。
如果您使用相同的参数,则不会创建意图,而是使用具有相同参数的先前意图。

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

You should create the PendingIntent with different request codes.
Change this:

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

To this:

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

Note that I've changed the 2nd argument to your loop index (i) instead of 0.
Intents are not created if you use the same arguments but rather they use a previous intent with the same arguments.

This should solve your problem.

镜花水月 2024-12-10 00:34:07
    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);
    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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文