在后台应用中未收到Firebase云消息 [英] Firebase cloud messaging not received when app in background

查看:345
本文介绍了在后台应用中未收到Firebase云消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近更新了应用程序,通过 FCM 发送消息,当应用程序在Jelly Bean上运行时,它非常有效。问题是,它不是,如果它是棒棒糖和应用程序在背景



我读文档,并声明当有效负载是数据消息,它将由 onMessageReceived()处理。我正在发送一个数据有效负载,不是通知,只要是JellyBean就可以正确处理。对于棒棒糖,它只处理消息,如果应用程序在前台。如果没有的话,什么都不会发生。



这就是我的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:
android:label =@ string / app_name
android:supportsRtl =true
android:labelButton =true
android:icon =@ mipmap / ic_launcher_shadow主题= @机器人:风格/ 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:我的FCM注册ID

$ / code>

通过POST发送到 https://fcm.googleapis.com/fcm/send 与授权和内容类型标头。



我可能读过所有与此相关的SO线程,但是我找不到答案,Android监视器中也没有日志记录。有没有人有这个提示?

好的,原来华为有一些节电导致某些应用程序无法接收通知的功能,因此与Android版本无关。



我无法找到以编程方式修复此问题的方法,但如果有人感兴趣,请转到<设置/受保护的应用程序,然后选择要允许在后台运行的应用程序,以便他们收到通知。

更多信息这里


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.

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.

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();
        .....
    }
}

The manifest:

<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>

And the payload:

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

Which is sent via POST to https://fcm.googleapis.com/fcm/send with the Authorization and Content-type headers.

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.

More info here.

这篇关于在后台应用中未收到Firebase云消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆