我最近更新了我的应用程序以通过 FCM 发送消息,当该应用程序在 Jelly Bean 上运行时效果很好。问题是如果它是 Lollipop 并且该应用程序在后台。
我读了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();
.....
}
}
list :
<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 带有授权和内容类型 header 。
我可能已经阅读了与此相关的所有其他 SO 线程,但我找不到答案,而且 Android 监视器中也没有日志跟踪。有人对此有提示吗?
最佳答案
好的,原来是华为有一些省电功能,导致部分应用收不到通知,所以跟安卓版本没有关系。
我找不到以编程方式解决此问题的方法,但如果有人感兴趣,请转至设置/ protected 应用并选择您要允许在后台运行的应用,这样它们接收通知。
更多信息 here .
关于android - 应用程序在后台时未收到 Firebase 云消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38016196/