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。单击其余部分会导致通知窗口最小化。
额外信息:函数 displayNotification()
位于名为 UploadManager
的类中。 Context
从实例化的 activity
传递到 UploadManager
。函数 displayNotification()
从一个函数中多次调用,同样在 UploadManager 中,该函数在 AsyncTask
中运行。
编辑 1:我忘了提到我将字符串响应作为 extra
传递给 Intent intent
。
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);
这有很大的不同,因为我需要额外的“响应”来反射(reflect)创建通知时的字符串响应。相反,使用 PendingIntent.FLAG_UPDATE_CURRENT
,额外的“响应”反射(reflect)了上次调用 displayNotification()
时的字符串响应。
我知道为什么这是通过阅读 FLAG_UPDATE_CURRENT
上的文档。但是,我目前不确定如何解决它。
最佳答案
不要将 Intent.FLAG_ACTIVITY_NEW_TASK
用于 PendingIntent.getActivity,使用 FLAG_ONE_SHOT而是
从评论中复制:
然后在 Intent 上设置一些虚拟 Action ,否则会丢弃额外内容。例如
intent.setAction(Long.toString(System.currentTimeMillis()))
关于android - PendingIntent 对第一个通知正常工作,但对其余通知不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3168484/