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

FCM notification not received when app is killed with multiple Firebase projects

我已遵循此 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

您应该在 Application

的 onCreate() 中调用 FirebaseApp.initializeApp(context, configuration)

As per documentation,

Notification Message - FCM automatically displays the message to end-user devices on behalf of the client app. Notification messages have a predefined set of user-visible keys and an optional data payload of custom key-value pairs.

Data Message - Client app is responsible for processing data messages. Data messages have only custom key-value pairs.

这是 Test FCM Notification with POSTMAN!

的完整文章