【问题标题】:How to restart / refresh contents of activity on notification click如何在通知单击时重新启动/刷新活动内容
【发布时间】:2014-11-21 03:02:51
【问题描述】:

我正在尝试在单击通知时刷新活动的内容。当我在其他活动中并单击通知时,我可以导航到该活动。我想要实现的是, 我在显示一些内容的活动 A 中。我收到一条新通知,单击它应该重新启动 Activity A,或者根据我在通知的 PendingIntent 中传递的内容刷新 Activity 中的内容。

我所做的一切,

  1. 尝试设置PendingIntent.FLAG_CANCEL_CURRENTPendingIntent.FLAG_UPDATE_CURRENT

  2. 尝试在我传递的意图中设置 Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP 以及挂起的意图。

  3. 检查了onNewIntent() 中的数据,它没有被刷新。我得到的数据与我在旧意图中传递的数据相同。

  4. 同时传递了一个唯一的 requestCode 以及 PendingIntent,仍然相同。

还有其他建议吗?

【问题讨论】:

  • 试试FLAG_ACTIVITY_REORDER_TO_FRONT。检查this definition link
  • 也用过。没有运气。
  • 请发布您用于将数据放入通知中的代码以及您在onNewIntent() 中用于从Intent 获取数据的代码
  • @iZBasit 如果您有任何解决方案,请发布您的答案。因为我也遇到了同样的问题。
  • @Srinivasan 我开始在项目中使用 Greenrobot 的事件总线,所以它自动为我解决了这个问题。

标签: android android-intent android-activity push-notification android-pendingintent


【解决方案1】:

如果您使用Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP,如果该活动在堆栈中,则应该重新创建它,或者如果它不在堆栈中,则启动一个新活动。如果您不想重新创建活动(如果它已经在堆栈中),您可以使用Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP

【讨论】:

  • 你的答案应该是正确的答案。实际上,如果它已经在堆栈中,则仅使用 Intent.FLAG_ACTIVITY_CLEAR_TOP 来重新创建活动就足够了。该标志的文档指出:“如果它(目标活动)已将其启动模式声明为“多重”(默认(更改为“标准”))并且您没有以相同的意图设置 FLAG_ACTIVITY_SINGLE_TOP,那么它(活动)将完成并重新创建;”。我测试了它,它确实是真的! :) 如果我对创建其他任务不感兴趣,我还应该有 Intent.FLAG_ACTIVITY_NEW_TASK 的任何特殊原因吗?
  • 从技术上讲,如果您从非活动 Context 启动 Activity,您应该拥有 Intent.FLAG_ACTIVITY_NEW_TASK。在所描述的情况下,OP 正在创建一个Notification,因此Activity 将从非活动Context 启动。 Android 对此有点宽容,但是如果您查看 logcat,如果您没有在 Notification 中指定此标志,您应该会看到警告。如果已经存在包含此应用程序的任务,则以这种方式使用此标志实际上不会创建新任务。它实际上将Activity 启动到现有任务中(如果有的话)。
【解决方案2】:

正如 aldoran 所说,使用 LocalBroadcastManager。在您的 Activity 类中:

@Override
protected void onResume() {
    super.onResume();
    LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter(FILTER_STRING));
}

@Override
protected void onPause() {
    super.onPause();
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
}

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
    Log.d("receiver", "Got message");
  }
};

然后在 GSM 广播中将您的数据放入意图中:

Intent intent = new Intent(FILTER_STRING);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

【讨论】:

  • 为什么我应该/我应该使用广播接收器并发送我不明白的广播!
  • 对不起,我不专心地阅读了这个问题。 Activity A 的 launchMode 是什么?尝试在 Manifest 中设置 singleTop。
  • 现在是 SingleInstance。已尝试与 SingleTop 相同。没用。
【解决方案3】:

试试这个代码:

           Intent notificationIntent = new Intent(context, DetailActivity.class);
           notificationIntent.putExtra(CommonConstants.EXTRA_NOTE_ID, note.get_id());
           notificationIntent.setAction(CommonConstants.NOTE_NOTIFICATION_REMINDER);

           notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
           PendingIntent contentIntent = PendingIntent.getActivity(context, note.get_id(), notificationIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
           NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle(context.getString(R.string.app_name)).setContentText(note.getMessage());
           mBuilder.setContentIntent(contentIntent);
           mBuilder.setDefaults(Notification.DEFAULT_SOUND);
           mBuilder.setAutoCancel(true);
           NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
           mNotificationManager.notify(note.get_id(), mBuilder.build());

最重要的是你必须打电话

 PendingIntent contentIntent = PendingIntent.getActivity(context, note.get_id(), notificationIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

PendingIntent.FLAG_CANCEL_CURRENT

【讨论】:

    【解决方案4】:

    您也可以使用LocalBroadcastManager 或编写自己的BroadcastReceiver,然后从您的Notification 发送Broadcast

    【讨论】:

    • 请问?你在说什么?我已经从 GCM 获得广播,并在服务中处理它并在通知中显示消息。我想要的是处理通知点击的活动周期!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-28
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    • 1970-01-01
    相关资源
    最近更新 更多