FCM (Firebase Cloud Messaging) Push Notification with Asp.Net

前端 未结 7 1010
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-28 22:01

I have already push the GCM message to google server using asp .net in following method,

GCM Push Notification with Asp.Net

<
7条回答
  •  温柔的废话
    2020-11-28 22:53

     public class Notification
    {
        private string serverKey = "kkkkk";
        private string senderId = "iiffffffffd";
        private string webAddr = "https://fcm.googleapis.com/fcm/send";
    
        public string SendNotification(string DeviceToken, string title ,string msg )
        {
            var result = "-1";
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Headers.Add(string.Format("Authorization: key={0}", serverKey));
            httpWebRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
            httpWebRequest.Method = "POST";
    
            var payload = new
            {
                to = DeviceToken,
                priority = "high",
                content_available = true,
                notification = new
                {
                    body = msg,
                    title = title
                },
            };
            var serializer = new JavaScriptSerializer();
            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = serializer.Serialize(payload);
                streamWriter.Write(json);
                streamWriter.Flush();
            }
    
            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                result = streamReader.ReadToEnd();
            }
            return result;
        }
    }
    

提交回复
热议问题