【问题标题】:sending push notification using FCM on android device在 android 设备上使用 FCM 发送推送通知
【发布时间】:2017-04-27 00:53:31
【问题描述】:

我可以使用控制台在我的 android 应用程序上发送推送通知。但是使用服务器端代码,我收到了成功的消息发送通知,但实际上通知无法在设备端接收。请告诉我我的代码有什么问题:

public static string SendPushNotification() {
        try {
            string applicationID = "AAAA4GkXVHA:....-qRw";

            string senderId = "963..28";

            string deviceId = "APA91bHLV...IC4s";

            WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
            tRequest.Method = "post";
            tRequest.ContentType = "application/json";
            var data = new {
                to = deviceId,
                notification = new {
                    body = "hema",
                    title = "hem",
                    //priority = "normal",
                    //sound = "Enabled"
                },
            };

            var serializer = new JavaScriptSerializer();
            var json = serializer.Serialize(data);
            Byte[] byteArray = Encoding.UTF8.GetBytes(json);
            tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
            tRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
            tRequest.ContentLength = byteArray.Length;
            using (Stream dataStream = tRequest.GetRequestStream()) {
                dataStream.Write(byteArray, 0, byteArray.Length);
                using (WebResponse tResponse = tRequest.GetResponse()) {
                    using (Stream dataStreamResponse = tResponse.GetResponseStream()) {
                        using (StreamReader tReader = new StreamReader(dataStreamResponse)) {
                            String sResponseFromServer = tReader.ReadToEnd();
                            string str = sResponseFromServer;
                            return str;
                        }
                    }
                }
            }
        }
        catch (Exception ex) {
            string str = ex.Message;
            return str;
        }
    }

我得到响应的地方如下: {"multicast_id":8288766196764532656,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1481612825945796%6ad79a87f9fd7ecd"}]}

【问题讨论】:

    标签: c# asp.net firebase firebase-cloud-messaging


    【解决方案1】:

    以正确的格式发送 json:

    {
      "to" : "APA91bHLV__P6Qer8U70j82blZt0VdDgc2zo_4DtAD4_MtE-......",
      "notification" : {
        "body" : "Success!",
        "title" : "Hema",
        "icon" : "myicon"
      }
    }
    

    要正确检查,您还可以使用邮递员:

    【讨论】:

      【解决方案2】:

      即使我也遇到了同样的问题。完成从客户端连接 FCM 的所有步骤。如果您从客户端实现 GetMessage() 方法,则只有您的设备才能从服务器端获取通知

      【讨论】:

        【解决方案3】:

        我为此写了一个小教程:https://www.kendar.org/?p=/tutorials/notifications 带有一个 Android 应用程序和一个 .Net 核心服务器。这里总结的是“主要”服务器代码

            public NotificationResult Send(NotificationModel messageData)
            {
                var result = "-1";
                var webAddr = "https://fcm.googleapis.com/fcm/send";
                var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, "key=PROJECTSETTINGS->Cloud Messagings->Server Key");
                httpWebRequest.Method = "POST";
                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    string strNJson = JsonConvert.SerializeObject(new NotificationMessage
                    {
                        To = "/topics/ServiceNow",
                        Data = new NotificationData
                        {
                            Description = messageData.Description,
                            IncidentNo = messageData.IncidentNo,
                            ShortDesc = messageData.ShortDesc
                        },
                        Notification = new Notification
                        {
                            Title = "ServiceNow: Incident No." + messageData.IncidentNo,
                            Text = "Notification"
                        }
                    });
                    streamWriter.Write(strNJson);
                    streamWriter.Flush();
                }
        
                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    result = streamReader.ReadToEnd();
                }
        
                return new NotificationResult
                {
                    Result = result
                };
            }
        

        以及将消息包装到 FCM 的类(使用 Json 注释来纠正 .Net 和 Json 命名标准之间的不匹配)

            public class NotificationData
        {
            public string ShortDesc { get; set; }
            public long IncidentNo { get; set; }
            public string Description { get; set; }
        }
        public class Notification
        {
            public Notification()
            {
                Sound = "default";
            }
            [JsonProperty("title")]
            public string Title { get; set; }
            [JsonProperty("text")]
            public string Text { get; set; }
            [JsonProperty("sound")]
            public string Sound { get; set; }
        }
        public class NotificationMessage
        {
            [JsonProperty("to")]
            public string To { get; set; }
            [JsonProperty("data")]
            public NotificationData Data { get; set; }
            [JsonProperty("notification")]
            public Notification Notification { get; set; }
        
        }
        

        【讨论】:

          猜你喜欢
          • 2021-01-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-03-29
          相关资源
          最近更新 更多