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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章