当应用处于后台时,即使传递了数据负载,也不会收到Firebase通知 [英] Firebase Notifications not being received when app is in background even if data payload is passed

查看:56
本文介绍了当应用处于后台时,即使传递了数据负载,也不会收到Firebase通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我知道,如果我发送数据消息,即使该应用程序在后台,通知也会到来.但是即使发送数据消息后,我也没有收到任何通知.

Now I know that if I send data messages, the notifications will come even if the app is in the background. But I am receiving no notifications whatsoever even after sending data messages.

我正在使用Postman进行测试,这是我的Postman请求的正文:

I'm using Postman for testing purposes and here's my body of my Postman Request:

{
 "to" : (device token),

 "data" : {
     "body" : "great match!",
     "title" : "Portugal vs. Denmark",
     "content_available" : true,
     "priority" : "high",
     "sound": "default",
     "time_to_live":"2419200"
  } 
}

这是我的 onMessageReceived()功能:

public void onMessageReceived(RemoteMessage remoteMessage) {

    Map<String,String> data=remoteMessage.getData();
    String title=data.get("title");
    String body=data.get("body");

    if (remoteMessage.getData().size() > 0) {
        Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
        try {
            JSONObject json = new JSONObject(remoteMessage.getData().toString());
            sendPushNotification(title,body);

        } catch (Exception e) {
            Log.e(TAG, "Exception: " + e.getMessage());
        }
    }
}

这是我的 sendPushNotification()函数的样子:

private void sendPushNotification(String title,String message) {

    try {

        String imageUrl="";
        MyNotificationManager mNotificationManager = new MyNotificationManager(getApplicationContext());
        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        if(imageUrl.equals("null")){
            mNotificationManager.showSmallNotification(title, message, intent);
            mNotificationManager.playNotificationSound();
        }else{
            mNotificationManager.showBigNotification(title, message, imageUrl, intent);
            mNotificationManager.playNotificationSound();
        }

    } catch (Exception e) {
        Log.e(TAG, "Json Exception: " + e.getMessage());
    }
}

我在堆栈溢出时关注此问题:在Firebase中后台运行应用程序时如何处理通知

I was following this question on stack overflow : How to handle notification when app in background in Firebase

P.S .:当应用程序位于前台时,可以很好地接收通知.

P.S.: When app is in the foreground, notifications are being received just fine.

推荐答案

如您在我的帖子中所见,对于特定ID,您应使用"registration_ids":["{device-token}","{device2-token}," {device3-token}] 键和值,而不是"to" .那是为了主题

As you could see on my post, for specific ids you should use "registration_ids": ["{device-token}","{device2-token}","{device3-token}"] key and values instead of "to". That’s for topics

这篇关于当应用处于后台时,即使传递了数据负载,也不会收到Firebase通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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