android - 使用多个 Firebase 项目终止应用程序时未收到 FCM 通知

标签 android firebase firebase-cloud-messaging

我已经关注了这个 documentation并使用多个在登录时动态切换的 Firebase 项目。因此,我不得不删除 google-services 插件和 google-services.json 并使用 FirebaseApp.initializeApp 以编程方式设置配置.到目前为止一切正常,但是当应用程序完全终止并收到通知时,出现错误并且未收到通知:

E/AndroidRuntime: FATAL EXCEPTION: Firebase-NotificationMessageHandler
    Process: com.example.app, PID: 8005
    java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.app. Make sure to call FirebaseApp.initializeApp(Context) first.
        at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@19.3.0:184)
        at com.google.firebase.messaging.zzo.zza(com.google.firebase:firebase-messaging@@20.1.0:120)
        at com.google.firebase.messaging.zzo.zza(com.google.firebase:firebase-messaging@@20.1.0:1)
        at com.google.firebase.messaging.FirebaseMessagingService.zzc(com.google.firebase:firebase-messaging@@20.1.0:50)
        at com.google.firebase.messaging.zze.run(com.google.firebase:firebase-messaging@@20.1.0:2)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
        at com.google.android.gms.common.util.concurrent.zza.run(Unknown Source:6)
        at java.lang.Thread.run(Thread.java:764)

在所有其他情况下,我会相应地收到通知。使用 google-services 插件和 google-services.json 即使应用程序被终止,我也可以通过 FCM 接收消息,但这不是一个选项,因为我不能使用 google-services 插件动态切换 Firebase 项目。

我总是将 FirebaseApp 初始化为默认应用:FirebaseApp.initializeApp(context, configuration)

关于如何解决这个问题的任何提示?

最佳答案

使用 data messages 而不是 notification messages 解决了这个问题。我现在可以动态切换 Firebase 项目并接收通知,即使应用已终止/关闭也是如此。

更多信息:https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages

关于android - 使用多个 Firebase 项目终止应用程序时未收到 FCM 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59325857/

相关文章:

android - 如何为 retrofit POST 调用设置动态端点?

javascript - TypeError : _this. props.editAp 不是一个函数

swift - 如何使用 displayName 更新用户

KitKat 之前(api 18 之前)的 Android HTML 代码打印

android - SQLITE with Android(设置数据库的最佳方法)

java - 更改 Min Sdk 版本后 "Cannot resolve symbol ' R '"?

javascript - TypeError : Cannot read property 'toString' of undefined at Geolib. isDecimal

firebase - 在 React Native 一段时间后删除通知(@react-native-firebase/messaging)

android - Firebase 消息处理 - kotlin 中的后台消息

java - 点击推送通知时如何做一些逻辑