【问题标题】:Firebase can not send push notification to android appFirebase 无法向 Android 应用发送推送通知
【发布时间】:2018-11-10 17:12:37
【问题描述】:

我已成功从服务器向 FireBase Cloud Messaging(FCM) 发送推送通知,以便将它们发送到 Android 设备。但我不知道为什么 FCM 不会将这些通知发送到设备。

我的代码如下,

服务器向 FCM 发送通知 (C#)

public static void SendPushNotification()
        {
            string serverKey = "AAAA...z";

            try
            {
                var result = "-1";
                var webAddr = "https://fcm.googleapis.com/fcm/send";

                var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Headers.Add("Authorization:key=" + serverKey);
                httpWebRequest.Method = "POST";

                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    string json = "{\"to\": \"f..A:..p_G\",\"data\": {\"message\": \"This is a Firebase Cloud Messaging Topic Message!\",}}";
                    streamWriter.Write(json);
                    streamWriter.Flush();
                }

                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    result = streamReader.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }    
        }

我对上述请求的回应

{"multicast_id":5002368547300,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:14200031%09c4rrr5787egg"}]}

我假设一旦 FCM 收到新通知,它会将这些通知推送到相应的 android 设备。

但对我来说,它不起作用。

【问题讨论】:

  • 我不知道有什么必须在移动应用端完成才能接收通知。
  • 您似乎正在向设备发送数据消息。数据消息不会自动显示通知。您必须实现 FirebaseMessagingService 并在 onMessageReceived 回调中处理数据消息
  • 是的。我得到它是为了通知消息,它​​正在工作。但我不知道如何处理数据消息。可以发一些示例代码吗?
  • 查看演示此功能的 FCM 示例应用程序:github.com/firebase/quickstart-android/tree/master/messaging/…
  • 没有可用的 getData() 方法。同时我从forums.xamarin.com/discussion/62903/…得到了解决方案。

标签: firebase-cloud-messaging android-push-notification


【解决方案1】:

我通过参考以下链接得到了解决方案

Firebase push notification not showing on phone despite successful response

一旦我更改了上面链接中提到的“json”字符串,它就可以工作了。

【讨论】:

猜你喜欢
  • 2020-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-18
  • 2020-06-30
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
相关资源
最近更新 更多