应用程序在后台时未收到 Firebase 云消息

Firebase cloud messaging not received when app in background

我最近更新了我的应用程序以通过 FCM 发送消息,当该应用程序在 Jelly Bean 上 运行 时效果很好。问题是它不会万一它是 Lollipop 并且应用程序在 background.

我看了documentation,它说当有效负载是数据消息时,它将由onMessageReceived()处理。我发送的是数据负载,不是通知,只要是 JellyBean,它就会得到正确处理。对于 Lollipop,它仅在应用程序位于前台时才处理消息。如果不是,什么也不会发生。

我的 FirebaseMessagingService 的开头是这样的:

public class FirebaseBroadcastReceiverService extends FirebaseMessagingService {
    private final String TAG = FirebaseBroadcastReceiverService.class.getName();

    @Override
    public void onMessageReceived(RemoteMessage message){
        Log.i(TAG, "A message is received");
        String from = message.getFrom();
        Map<String, String> data = message.getData();
        .....
    }
}

清单:

<application
    android:name=".controller.application.AppResources"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher_shadow"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@android:style/Theme.Black.NoTitleBar">

   <service
        android:name=".model.firebase.FirebaseListenerService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>
    <service android:name=".model.firebase.FirebaseBroadcastReceiverService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
    .......
</application>

有效负载:

{ 
    "data": {
        "test": "test"
    },
    "to" : "MY FCM REGISTRATION ID"
}

通过 POST 发送到 https://fcm.googleapis.com/fcm/send 并获得授权和 Content-type headers.

我可能已经阅读了与此相关的所有其他 SO 线程,但我找不到答案,而且 Android 监视器中也没有日志跟踪。有人对此有提示吗?

听起来您的清单文件中缺少唤醒锁定权限

好的,原来华为有一些省电功能,导致部分应用收不到通知,所以跟Android版本。

我找不到以编程方式解决此问题的方法,但如果有人感兴趣,请转到 设置/受保护的应用程序 和 select 您要允许的应用程序运行 在后台,所以他们会收到通知。

更多信息here

转到设置应用选择您的应用,然后确保勾选显示通知并取消勾选限制启动