简体   繁体   English

当应用程序从通知启动时,活动不会从通知启动

[英]Activity won't launch from notification when the app is started from notification

I have an app that sends notifications, and when clicked, opens an activity from my app.我有一个发送通知的应用程序,单击时会从我的应用程序中打开一个活动。 If you launch the app and keep the app open and click notifications, it works properly.如果您启动应用程序并保持应用程序打开并单击通知,则它可以正常工作。 However, when the app is closed, it doesn't.但是,当应用程序关闭时,它不会。 This is what happens:这就是发生的事情:

Close app > receive notification > click notification > opens the correct activity > receive another notification > nothing happens when clicked关闭应用程序>接收通知>单击通知>打开正确的活动>接收另一个通知>单击时没有任何反应

The intended functionality is for the notification to open the activity regardless if it is already open, and just add it onto the task stack.预期的功能是通知打开活动,无论它是否已经打开,并将其添加到任务堆栈中。 So if you clicked 3 notifications, the stack would be ActivityA > ActivityA > ActivityA.因此,如果您单击 3 个通知,则堆栈将为 ActivityA > ActivityA > ActivityA。 Here is the code for the notification:这是通知的代码:

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(ID, new NotificationCompat.Builder(context, CHANNEL_ID)
  .setOnlyAlertOnce(false)
  .setAutoCancel(true)
  .setCustomContentView(helper.createSmallView(context))
  .setCustomBigContentView(helper.createBigView(context))
  .setContentIntent(PendingIntent.getActivity(context, 1,
    new Intent(context, ActivityA.class),
    PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE)
  )
  .build());

I also tried using a broadcast PendingIntent .我还尝试使用广播PendingIntent The BroadcastReceiver would be called but the call to Context#startActivity did nothing. BroadcastReceiver将被调用,但对Context#startActivity的调用什么也没做。

Intent intent = new Intent(YourActivity.class, NewActivity.class); Intent intent = new Intent(YourActivity.class, NewActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);

PendingIntent待定意向
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); notificationManager.notify(ID, new NotificationCompat.Builder(context, CHANNEL_ID).setOnlyAlertOnce(false).setAutoCancel(true).setCustomContentView(helper.createSmallView(context)).setCustomBigContentView(helper.createBigView(context)).setContentIntent(pendingIntent).build()); notificationManager.notify(ID, new NotificationCompat.Builder(context, CHANNEL_ID).setOnlyAlertOnce(false).setAutoCancel(true).setCustomContentView(helper.createSmallView(context)).setCustomBigContentView(helper.createBigView(context)).setContentIntent(pendingIntent) 。建造());

You have to use pending intent.您必须使用待处理的意图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM