我最近更新了我的应用程序以通过 FCM 发送消息,当应用程序在 Jelly Bean 上运行时效果很好.问题是,如果它是 Lollipop 并且应用程序位于 背景 中,则它不会.
I recently updated my app to send messages through FCM and it works great when the app is run on Jelly Bean. The problem is it doesn't in case it's Lollipop and the app is in the background.
我阅读了 文档,并指出当有效载荷是一条数据消息,它将由 onMessageReceived() 处理.我正在发送一个数据负载,不是通知,只要它是 JellyBean,它就会被正确处理.对于 Lollipop,它仅在应用程序位于前台时处理消息.如果没有,什么都不会发生.
I read the documentation and it's stated that when the payload is a data message, it will be handled by onMessageReceived(). I am sending a data payload, not a notification, and it's handled correctly as long as it's JellyBean. For Lollipop it only handles the message if the app is in the foreground. If not, nothing happens.
这就是我的 FirebaseMessagingService 开头的样子:
This is how the beginning of my FirebaseMessagingService looks like:
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/使用 Authorization 和 Content-type 标头发送.
Which is sent via POST to https://fcm.googleapis.com/fcm/send with the Authorization and Content-type headers.
我可能已经阅读了与此相关的所有其他 SO 线程,但我找不到答案,而且 Android 监视器中也没有日志跟踪.有人对此有提示吗?
I might have read all other SO threads related to this but I couldn't find an answer to it and there are also no log traces in the Android Monitor. Does anyone have a hint on this?
好吧,原来华为有一些省电功能,导致某些应用无法接收通知,所以与安卓版本无关.
Ok, turns out Huawei has some power-saving features which cause that some apps won't be able to receive notifications, so it's not related to the Android version.
我找不到以编程方式解决此问题的方法,但如果有人感兴趣,请转到设置/受保护的应用程序并选择您希望允许在后台运行的应用程序,以便他们接收通知.
I couldn't find a way to fix this programmatically, but if anyone's interested, go to Settings / Protected apps and select the apps you want to allow to be run on the background, so they receive notifications.
更多信息这里.
这篇关于应用程序在后台时未收到 Firebase 云消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!