简体   繁体   English

先前从通知启动应用程序时,Android上的FCM不会触发`onNewIntent`

[英]FCM on Android does not trigger `onNewIntent` when app was previously launched from notification

I want to open a proper screen when my app is launched from notification. 从通知启动我的应用程序时,我想打开一个适当的屏幕。 I found a scenario when it doesn't work as I thought it should. 我发现了一个场景,它无法正常运行。

So here is first scenario when it works as it should work: 因此,这里是第一种情况,它可以正常工作:

  1. App is launched from launcher icon 应用从启动器图标启动
  2. App is put into background 应用程序进入后台
  3. FCM notification shows up 显示FCM通知
  4. Upon clicking it onNewIntent of default activity is called. 单击后,将onNewIntent默认活动的onNewIntent

But when app is first launched from FCM notification then step 4. never happens: 但是,当第一次从FCM通知启动应用程序时,则不会执行步骤4:

  1. FCM notification shows up 显示FCM通知
  2. App is launched from FCM notification 应用从FCM通知启动
  3. App is put into background 应用程序进入后台
  4. Another FCM notification shows up 出现另一个FCM通知
  5. Upon clicking it onNewIntent of default activity IS NOT CALLED 当点击它onNewIntent默认的活动不叫

I found it really problematic to work with and to code against. 我发现使用和编写代码确实很麻烦。 Is this a bug or I am just missing something? 这是一个错误还是我只是缺少一些东西?

EDIT : Default activity launchMode is set to singleTask in manifest file. 编辑 :预设活动launchMode设置为singleTask清单文件。

Referring to the previous answer, onNewIntent is called for activities that set launchMode to "singleTop". 参考上一个答案,对于将launchMode设置为“ singleTop”的活动调用onNewIntent。

So first thing you should set up is the manifest file. 因此,您应该设置的第一件事是清单文件。 Set the activity's launchMode to singleTop . 将活动的launchMode设置为singleTop Another thing is to add an intent filter with an action tag. 另一件事是添加带有动作标签的意图过滤器。

sample: 样品:

<activity android:name="youPackage.YourActivity"
          android:launchMode="singleTop">
    <intent-filter>
        <action android:name="youPackage.ACTION">
    </intent-filter>
</activity>

And, in your service, you can call new intent provided the action you define in the intent-filter. 而且,在您的服务中,您可以调用新的意图,只要您在意图过滤器中定义了操作即可。

sample: 样品:

Intent i = new Intent("youPackage.ACTION");

Reference: 参考:

Make Sure to override onNewIntent in the launcher activity . 确保在启动器活动中覆盖onNewIntent。 In my case, it was the splash screen. 就我而言,这是启动屏幕。

@Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent.getExtras() != null) {
            for (String key : intent.getExtras().keySet()) {
                Object value = intent.getExtras().get(key);
                Log.d("data ", "Key: " + key + " Value: " + value);
            }
        }
    }

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

相关问题 从后台启动应用程序时会调用onNewIntent - onNewIntent is called when app is launched from background 应用程序未启动时处理fcm通知 - Handling fcm notification when app not launched android-从通知启动应用程序时提供向后导航 - android - provide back navigation when app launched from notification Android FCM:当应用程序在后台时 FCM 显示(通知)消息登录 - Android FCM: FCM display(notification) message loggin when app is in background 使用FCM将通知从PHP推送到Android应用 - Push notification from PHP to an android app with FCM 单击通知后在后台运行应用程序时,Android推送不会触发 - Android push does not trigger when app in background after notification clicked 从通知中调用onNewIntent() - Calling onNewIntent() from a notification FCM:当应用程序处于前台状态时处理其应用程序的不同数据通知 - FCM : Handling different data notification of an android app when app is in foreground 从任务管理器滑动应用程序时,fcm通知如何工作 - How does fcm notification works when app is swiped from task manager 为什么单击任务栏中的FCM通知未启动我的应用程序? - Why click on FCM notification in tray not launched my app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM