Firebase onMessageReceived not called when app in background

問題:

I'm working with Firebase and testing sending notifications to my app from my server while the app is in the background.我正在使用 Firebase 並測試在應用程序處於後臺時從我的服務器向我的應用程序發送通知。 The notification is sent successfully, it even appears on the notification centre of the device, but when the notification appears or even if I click on it, the onMessageReceived method inside my FCMessagingService is never called.通知發送成功,它甚至出現在設備的通知中心,但是當通知出現時或者即使我點擊它,我的 FCMessagingService 中的 onMessageReceived 方法也不會被調用。

When I tested this while my app was in the foreground, the onMessageReceived method was called and everything worked fine.當我在我的應用程序處於前臺時對此進行測試時,調用了 onMessageReceived 方法並且一切正常。 The problem occurs when the app is running in the background.當應用程序在後臺運行時會出現問題。

Is this intended behaviour, or is there a way I can fix this?這是預期的行爲,還是有辦法解決這個問題?

Here is my FBMessagingService:這是我的 FBMessagingService:

import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class FBMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.i("PVL", "MESSAGE RECEIVED!!");
        if (remoteMessage.getNotification().getBody() != null) {
            Log.i("PVL", "RECEIVED MESSAGE: " + remoteMessage.getNotification().getBody());
        } else {
            Log.i("PVL", "RECEIVED MESSAGE: " + remoteMessage.getData().get("message"));
        }
    }
}

解決方案:

參考一: https://en.stackoom.com/question/2Wkdq
參考二: https://stackoom.com/question/2Wkdq
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章