【问题标题】:Unable to launch activity from FCM onMessageReceived when app is killed当应用程序被杀死时,无法从 FCM onMessageReceived 启动活动
【发布时间】:2020-06-07 22:33:47
【问题描述】:

我想在收到从 FCM 到我的 Android Oreo 的推送通知时打开一个活动。但它不起作用。我尝试在 onMessageReceived 方法中添加一个 HTTP 调用,并且按预期工作。但它不会启动一个活动,如下面的代码:

@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    //http call which succeeds

    Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

清单有这个服务定义:

    <service
        android:name=".services.CheckerService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

期待一些专家的建议。

【问题讨论】:

  • 当您的应用程序被杀死时,onMessageRecieved 将不会被调用,而是将消息传递到通知托盘。因此,如果您将 click_action 添加到您的消息中,您将能够启动所需的活动。
  • @Jason,onMessageReceived 确实被调用,即使在应用程序从最近的应用程序中向左滑动后,正如我从 HTTP 调用中看到的那样。
  • 在您从最近刷掉后,您是否检查过您的应用是否正在运行?
  • @Jason,我在onMessageReceived 中所做的是启动一个 websocket 连接。所以当我从最近刷掉时,我可以看到这个 websocket 是关闭的,当一个新的通知到达时,它会重新建立 websocket。所以我知道它被杀了。有没有其他方法可以检查应用被刷掉后是否还在运行?
  • @Jason 我不同意你的观点,如你所见,skype、messenger 可以在接听电话时直接显示通话屏幕,即使应用程序关闭或屏幕关闭

标签: android android-activity firebase-cloud-messaging android-8.0-oreo launch


【解决方案1】:

检查android:showOnLockScreenandroid:showWhenLockedandroid:turnScreenOn 参数是否在您的AndroidManifest.xml 中设置为"true"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-10
    • 1970-01-01
    • 2019-05-21
    • 1970-01-01
    • 2020-07-17
    • 2018-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多