C#构建服务器端ios推送通知

时间:2017-03-06 07:59:54

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

我是.net开发人员使用此代码使用FCM firebase云消息传递使服务器端库推送 IOS客户端应用程序上的通知。

    {
        WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
        tRequest.Method = "post";
        tRequest.ContentType = "application/json";
        var objNotification = new
        {
            **to = "Registration token ",**
            notification = new
            {
                title = "hi",
                body = "FCM correct token tab closed"
            }
        };
        string jsonNotificationFormat = Newtonsoft.Json.JsonConvert.SerializeObject(objNotification);

        Byte[] byteArray = Encoding.UTF8.GetBytes(jsonNotificationFormat);
        tRequest.Headers.Add(string.Format("Authorization: key={0}", "server-key"));
        tRequest.Headers.Add(string.Format("Sender: id={0}", "sender id"));
        tRequest.ContentLength = byteArray.Length;
        tRequest.ContentType = "application/json";
        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 responseFromFirebaseServer = tReader.ReadToEnd();

                        FCMResponse response = Newtonsoft.Json.JsonConvert.DeserializeObject<FCMResponse>(responseFromFirebaseServer);
                        if (response.success == 1)
                        {
                            Console.WriteLine("Message was sent successfully");

                        }
                        else if (response.failure == 1)
                        {
                            Console.WriteLine(string.Format("Error sent from FCM server, after sending request : {0} , for following device info: {1}", responseFromFirebaseServer, jsonNotificationFormat));

                        }

                    }
                }

            }
        }
    }

如何在发送通知之前从FCM检索注册令牌并使用它来推送通知?

谢谢..

0 个答案:

没有答案