簡體   English   中英

當應用被多個 Firebase 項目殺死時未收到 FCM 通知

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

我遵循了這個文檔並使用了多個在登錄時動態切換的 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)

根據文檔,

通知消息 - FCM 代表客戶端應用程序自動向最終用戶設備顯示消息。 通知消息具有一組預定義的用戶可見鍵和自定義鍵值對的可選數據負載。

數據消息 - 客戶端應用程序負責處理數據消息。 數據消息只有自定義鍵值對。

這是使用 POSTMAN 測試 FCM 通知的完整文章

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM