如何在ASP.Net C#中实现FCM?

时间:2016-09-07 06:18:11

标签: c# firebase-cloud-messaging

我正在使用GCM在Android上使用c#进行通知,但现在我想将GCM更改为FCM。

我需要改变什么?或者我如何在C#中实现FCM?任何帮助表示赞赏。我目前正在使用此代码用于FCM,我刚刚更改了URL,但它无效。

         try { 
            msg = message;

            var applicationID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
            var SENDER_ID = "xxxxxxxxxxx";

            WebRequest tRequest;
            //tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
            tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
            tRequest.Method = "post";

            tRequest.ContentType = "application/x-www-form-urlencoded";
            tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));

            tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));

            string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + message + "&data.time=" + System.DateTime.Now.ToString() +                               "&registration_id=" + notificationtoken + "";

            Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            tRequest.ContentLength = byteArray.Length;

            Stream dataStream = tRequest.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();

            WebResponse tResponse = tRequest.GetResponse();

            dataStream = tResponse.GetResponseStream();

            StreamReader tReader = new StreamReader(dataStream);

            String sResponseFromServer = tReader.ReadToEnd();

            notification = sResponseFromServer;
            tReader.Close();
            dataStream.Close();
            tResponse.Close();
       return sResponseFromServer;
                     }
        catch (Exception ex)
        {
            notification.Status = false;
            notification = "ERROR DESCRIPTION : " + ex.Message;
        }

1 个答案:

答案 0 :(得分:4)

试试这个希望这对你有帮助..........

try
        {
            var applicationID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
            var senderId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
            string deviceId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
            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 = "This is the message Body",
                    title = "This is the title of Message",
                    icon = "myicon"
                },
                priority = "high"

            };

            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();
                            Response.Write(sResponseFromServer);
                            Label3.Text = sResponseFromServer;
                        }
                    }
                }
            }
        }

        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }


    }