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从实例化的UploadManager传递到activity中。函数displayNotification()从也在AsyncTask中运行的UploadManager中的函数多次调用。

编辑1:我忘了提到我将String响应作为Intent intent传递到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);

这有很大的不同,因为我需要额外的“响应”来反射(reflect)创建通知时String响应是什么。相反,使用PendingIntent.FLAG_UPDATE_CURRENT,额外的“响应”反射(reflect)了上次调用displayNotification()时String响应是什么。

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

最佳答案

不要对PendingIntent.getActivity使用Intent.FLAG_ACTIVITY_NEW_TASK,而应使用FLAG_ONE_SHOT

从评论中复制:

然后在Intent上设置一些虚拟操作,否则多余的内容将被丢弃。例如

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

07-26 09:42
查看更多