简体   繁体   中英

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

I have followed this documentation and use multiple Firebase projects which are dynamically switched on login. As a result, I had to remove the google-services plugin and the google-services.json and set the configuration programmatically with FirebaseApp.initializeApp . Everything works great so far, but when the app is killed completely and receives a notification, there is an error and the notification is not received:

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)

In all other situations I receive the notifications accordingly. With the google-services plugin and the google-services.json I can receive messages by FCM even when the app is killed, but that's not an option, because I can't switch Firebase projects dynamically using the google-services plugin.

I always initialize the FirebaseApp as the default app: FirebaseApp.initializeApp(context, configuration)

Any hints on how to solve this?

Using data messages instead of notification messages fixed the issue. I can now dynamically switch Firebase projects and receive notifications, even when the app was killed / closed.

More information: 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.

Here is the full article for Test FCM Notification with POSTMAN!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM