Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android OnMessageReceived在后台应用程序中未被解雇_Android_Firebase_Push Notification_Xamarin.android_Firebase Cloud Messaging - Fatal编程技术网

Android OnMessageReceived在后台应用程序中未被解雇

Android OnMessageReceived在后台应用程序中未被解雇,android,firebase,push-notification,xamarin.android,firebase-cloud-messaging,Android,Firebase,Push Notification,Xamarin.android,Firebase Cloud Messaging,我试图在单击“差异通知”时重定向到不同的页面。 当应用程序在后台运行时,OnMessagedRecieved不会被触发,只有当它在前台运行时才会被触发 根据文档,触发的是系统托盘 所以我查了一下如何让它在后台工作,我发现了这个 根据本教程: 如下所述,通过使用FCM控制台,您只能发送通知消息。通知消息可以在前台应用程序中通过onMessageReceived方法处理,并在后台应用程序中传递到设备的系统托盘。用户点击通知,默认应用程序启动器将打开。若要在应用程序的每个状态下处理通知,必须使用dat

我试图在单击“差异通知”时重定向到不同的页面。 当应用程序在后台运行时,OnMessagedRecieved不会被触发,只有当它在前台运行时才会被触发

根据文档,触发的是系统托盘

所以我查了一下如何让它在后台工作,我发现了这个

根据本教程:

如下所述,通过使用FCM控制台,您只能发送通知消息。通知消息可以在前台应用程序中通过onMessageReceived方法处理,并在后台应用程序中传递到设备的系统托盘。用户点击通知,默认应用程序启动器将打开。若要在应用程序的每个状态下处理通知,必须使用data message和onMessageReceived方法

所以我查找了数据消息的确切含义以及通知消息的含义

我阅读了教程,但它对我不起作用。 这是我的代码:

public async Task<bool> WakeUp(string[] tokens)
    {
        var message = new Message()
        {
            RegistrationID= tokens,
            Notification = new Notification()
            {
                Title = "testing",
                Body = "test"
            },
            Android = new AndroidConfig()
            {
                Notification = new AndroidNotification()
                {
                    Color = "#FF0000",
                    ClickAction = "MyActivity"
                }
            },
            Data = new Dictionary<string, string>()
            {
                {
                    "notificationType", NotificationType.WakeUp.ToString()
                }
            }
        };

        return await SendAsyncMessage(message).ConfigureAwait(false);
    }


public async Task<bool> SendAsyncMessage(Message message)
    {
        var jsonMessage = JsonConvert.SerializeObject(message);
        var request = new HttpRequestMessage(HttpMethod, FIREBASE_PUSH_NOTIFICATION_URL)
        {
            Content = new StringContent(jsonMessage, Encoding.UTF8, "application/json")
        };
        request.Headers.TryAddWithoutValidation("Authorization", $"key={ConfigurationManager.AppSettings["FirebaseNotificationServerKey"]}");
        request.Headers.TryAddWithoutValidation("Sender", $"id={ConfigurationManager.AppSettings["FirebaseNotificationSenderID"]}");
        HttpResponseMessage result;
        using (var client = new HttpClient())
        {
            result = await client.SendAsync(request).ConfigureAwait(false);
        }

        return result.IsSuccessStatusCode;
    }
原始Json

{
"registration_ids":["myToken"],
"condition":null,
"data":
{
    "notificationType":"WakeUp"
},
"notification":
{
    "title":"testing",
    "body":"test",
    "image":null
},
"android":
{
    "collapse_key":null,
    "restricted_package_name":null,
    "data":null,
    "notification":
    {
        "title":null,
        "body":null,
        "icon":null,
        "color":"#FF0000",
        "sound":null,
        "tag":null,
        "image":null,
        "click_action":"mActivity",
        "title_loc_key":null,
        "title_loc_args":null,
        "body_loc_key":null,
        "body_loc_args":null,
        "channel_id":null
    },
    "fcm_options":null,
    "priority":"high",
    "ttl":null
},
"webpush":null,
"apns":null,
"fcm_options":null,
"topic":null
}

已收到通知,但未触发OnMessageRecieved。我认为通知中心负责显示通知

有什么问题吗?

1。为什么会这样? FCM(Firebase云消息传递)中有两种类型的消息:

  • 显示消息:这些消息仅在应用程序位于前台时触发
    onMessageReceived()
    回调
  • 数据消息:如果您的应用程序位于前台/后台/死机中,这些消息会触发
    onMessageReceived()
    回调
  • 注意:Firebase团队尚未开发一个用户界面,用于向用户发送
    数据消息
    你的设备,还没有。您应该使用您的服务器发送此类型



    2.如何? 要实现这一点,您必须对以下URL执行
    POST
    请求:

    POST

    标题
    • 键:
      内容类型
      值:
      应用程序/json
    • 键:
      授权
      值:
      键=
    身体使用主题 或者如果您想将其发送到特定设备

    注意:确保您没有添加
    JSON密钥
    通知

    注意:要获取您的服务器密钥,您可以在firebase控制台中找到它:
    您的项目->设置->项目设置->云消息->服务器密钥

    3.如何处理推送通知消息? 以下是您处理收到的消息的方式:

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) { 
         Map<String, String> data = remoteMessage.getData();
         String myCustomKey = data.get("my_custom_key");
    
         // Manage data
    }
    
    @覆盖
    收到的消息(RemoteMessage RemoteMessage)上的公共无效{
    Map data=remoteMessage.getData();
    字符串myCustomKey=data.get(“my_custom_key”);
    //管理数据
    }
    

    这是指原始json消息的外观like@tyczj我编辑了这一页。忘了这一点吧,就像你看到的
    “data”:null,
    是null不,它永远不会被发送到background@tyczj该数据为空,但上面的数据不是。标题和正文也在一般通知中,作为我填写的数据,所以我假设android也可以从那里读取。我可能是misstaken@tyczj我将代码更改为也将数据放入空数据属性中。但它仍然不会触发
    {
        "to": "/topics/my_topic",
        "data": {
            "my_custom_key": "my_custom_value",
            "my_custom_key2": true
         }
    }
    
    {
        "data": {
            "my_custom_key": "my_custom_value",
            "my_custom_key2": true
         },
        "registration_ids": ["{device-token}","{device2-token}","{device3-token}"]
    }
    
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) { 
         Map<String, String> data = remoteMessage.getData();
         String myCustomKey = data.get("my_custom_key");
    
         // Manage data
    }