FCM onMessageReceived() 永远不会被调用

Posted

技术标签:

【中文标题】FCM onMessageReceived() 永远不会被调用【英文标题】:FCM onMessageReceived() is never called 【发布时间】:2016-10-29 18:45:26 【问题描述】:

我正在将 FireBase 推送通知集成到我的应用程序中。 虽然我正在接收推送通知(当应用程序在后台时),但我的 onMessageReceived(RemoteMessage remoteMessage) 从未被调用,无论应用程序是在前台还是在后台。 如果是 FCM 的问题,或者我做错了什么。 我在我的应用 build.gradle 中使用“com.google.firebase:firebase-messaging:9.0.0”。 我已经在我的应用程序清单中声明了这两个服务

 <service
    android:name="app.pnd.camscanner.MyFirebaseMessagingService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>

<service
    android:name="app.pnd.camscanner.MyFirebaseInstanceIDService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    </intent-filter>
</service>

【问题讨论】:

如果 onMessageReceived 从未被调用,你怎么知道你收到了推送消息? MyFirebaseMessagingService 是否扩展 FirebaseMessagingService? @ArthurThompson - 是的,我已经在 FirebaseMessagingService 服务类中扩展了 FirebaseMessagingService 要明确一点,如果您的应用程序在前台并且您向它发送消息,那么您什么也得不到,但如果您的应用程序在后台您会收到通知? @ArthurThompson 是的 【参考方案1】:

终于,问题解决了。我创建了一个新项目并再次集成了 firebase。现在,当应用程序在后台时,我会自动获得推送,当应用程序在前台时,我的 MyFirebaseMessagingService 类的 onMessageReceived(remoteMessage) 扩展了 FirebaseMessagingService 正在被调用,我正在自己生成推送通知。

【讨论】:

【参考方案2】:

对我来说问题是我在清单的应用程序标签中有tools:node="replace

【讨论】:

以上是关于FCM onMessageReceived() 永远不会被调用的主要内容,如果未能解决你的问题,请参考以下文章

如何将数据从 FCM 的 onMessageReceived() 发送到 Fragment?

FCM android onmessagereceived not called

解析从 android onMessageReceived 上的 fcm 响应获得的 json

android O中的onMessageReceived在后台应用时未调用

如何禁用或启用onMessageReceived的firebase推送通知?

当应用程序处于前台并调用 onMessageReceived 时不显示通知