PendingIntent 对第一个通知正常工作,但对其余通知不正确 [英] PendingIntent works correctly for the first notification but incorrectly for the rest

查看:25
本文介绍了PendingIntent 对第一个通知正常工作,但对其余通知不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  protected void displayNotification(String response) {
    Intent intent = new Intent(context, testActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);

    Notification notification = new Notification(R.drawable.icon, "Upload Started", System.currentTimeMillis());
    notification.setLatestEventInfo(context, "Upload", response, pendingIntent);

    nManager.notify((int)System.currentTimeMillis(), notification);
}

这个函数会被多次调用.我希望每个 notification 在点击时启动 testActivity.不幸的是,只有第一个通知会启动 testActivity.点击其余部分会导致通知窗口最小化.

This function will be called multiple times. I would like for each notification to launch testActivity when clicked. Unfortunately, only the first notification launches testActivity. Clicking on the rest cause the notification window to minimize.

额外信息:函数displayNotification() 位于名为UploadManager 的类中.Context 从实例化的 activity 传递到 UploadManager.函数 displayNotification() 在一个函数中被多次调用,同样在 UploadManager 中,该函数在 AsyncTask 中运行.

Extra information: Function displayNotification() is in a class called UploadManager. Context is passed into UploadManager from the activity that instantiates. Function displayNotification() is called multiple times from a function, also in UploadManager, that is running in an AsyncTask.

编辑 1:我忘了提到我将字符串响应作为 extra 传递到 Intent Intent.

Edit 1: I forgot to mention that I am passing String response into Intent intent as an extra.

  protected void displayNotification(String response) {
    Intent intent = new Intent(context, testActivity.class);
    intent.putExtra("response", response);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

这有很大的不同,因为我需要额外的响应"来反映创建通知时的字符串响应.相反,使用 PendingIntent.FLAG_UPDATE_CURRENT,额外的响应"反映了最后一次调用 displayNotification() 时的 String 响应.

This makes a big difference because I need the extra "response" to reflect what String response was when the notification was created. Instead, using PendingIntent.FLAG_UPDATE_CURRENT, the extra "response" reflects what String response was on the last call to displayNotification().

我从阅读关于 FLAG_UPDATE_CURRENT 的文档中知道这是为什么.但是,我目前不确定如何解决它.

I know why this is from reading the documentation on FLAG_UPDATE_CURRENT. However, I am not sure how to work around it at the moment.

推荐答案

不要将 Intent.FLAG_ACTIVITY_NEW_TASK 用于 PendingIntent.getActivity,使用 FLAG_ONE_SHOT 代替

Don't use Intent.FLAG_ACTIVITY_NEW_TASK for PendingIntent.getActivity, use FLAG_ONE_SHOT instead

从评论中复制:

然后在 Intent 上设置一些虚拟操作,否则会丢弃额外的操作.例如

Then set some dummy action on the Intent, otherwise extras are dropped. For example

intent.setAction(Long.toString(System.currentTimeMillis()))

这篇关于PendingIntent 对第一个通知正常工作,但对其余通知不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆