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

前端 未结 7 1029
佛祖请我去吃肉
佛祖请我去吃肉 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:51

    C# Server Side Code For Firebase Cloud Messaging

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Web;
    using System.Web.Script.Serialization;
    
    namespace Sch_WCFApplication
    {
        public class PushNotification
        {
            public PushNotification(Plobj obj)
            {
                try
                {    
                    var applicationID = "AIza---------4GcVJj4dI";
    
                    var senderId = "57-------55";
    
                    string deviceId = "euxqdp------ioIdL87abVL";
    
                    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 = obj.Message,
    
                            title = obj.TagMsg,
    
                            icon = "myicon"
    
                        }    
                    };       
    
                    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;
    
                                }    
                            }    
                        }    
                    }    
                }        
    
                catch (Exception ex)
                {
    
                    string str = ex.Message;
    
                }          
    
            }   
    
        }
    }
    

    APIKey and senderId , You get is here---------as follow(Below Images) (go to your firebase App)

提交回复
热议问题