Android - 使用标记活动单击推送通知后打开或重新启动应用程序

Dij*_*ark 11 java android push-notification google-cloud-messaging

我在Android中使用推送通知.当我收到推送通知时,我想打开应用程序,如果它仍在运行,否则它应该打开它的新实例.

我在用

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 
    Intent.FLAG_ACTIVITY_CLEAR_TOP | 
    Intent.FLAG_ACTIVITY_SINGLE_TOP | 
    Intent.FLAG_ACTIVITY_NEW_TASK);
Run Code Online (Sandbox Code Playgroud)

但是当我现在收到推送通知并点击它时,没有任何反应.

我怎样才能通过使用flagIntents实现这一目标?

Dav*_*ser 15

你需要在上面设置Intent标志Intent.您在调用中指定了它们以获取a PendingIntent.试试这个:

notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
    Intent.FLAG_ACTIVITY_SINGLE_TOP | 
    Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
    notificationIntent, 0);
Run Code Online (Sandbox Code Playgroud)

  • 如果你没有设置标志`Intent.FLAG_ACTIVITY_SINGLE_TOP`,那么活动的当前实例将被销毁,并且将创建一个新实例(从而调用`onCreate()`). (3认同)
  • 看起来我解决了它.如果我同时设置Intent.Intent.FLAG_ACTIVITY_SINGLE_TOP或launchMode ="singleTop"甚至btoh,则不会调用我的onNewIntent()方法.我补充说:Intent intent = new Intent(this,MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 在清单中我设置了launchMode ="singleTask".现在它按预期工作 (2认同)