728x90
반응형

firebase 를 이용해 token 기준으로 notification 을 보내는 방법입니다.

 

 

        private void SendNotification()

        {

            try

            {

                string url = @"https://fcm.googleapis.com/fcm/send";

                WebRequest tRequest = WebRequest.Create(url);

                tRequest.Method = "post";

                tRequest.ContentType = "application/json";

 

                // 디바이스 하나

                //string deviceId = "device1-token";

                //var data = new

                //{

                //    to = deviceId,

                //    notification = new

                //    {

                //        body = "This is the message",

                //        title = "This is the title"

 

                //    }

                //};

 

                // 디바이스 여러개

                List<string> diviceList = new List<string>() {

                    "device1-token", "device2-token"

                };

 

                var data = new

                {

                    registration_ids = diviceList,

                    notification = new

                    {

                        body = "This is the message",

                        title = "This is the title"

 

                    }

                };

 

                string jsonss = Newtonsoft.Json.JsonConvert.SerializeObject(data);

 

                Byte[] byteArray = Encoding.UTF8.GetBytes(jsonss);

                tRequest.Headers.Add(string.Format("Authorization: key={0}", ServerKey]));

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

                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 sResponseFromServer = tReader.ReadToEnd();

                                Console.Write(sResponseFromServer);

                            }

                        }

                    }

                }

            }

 

firebase 의 프로젝트 설정의 클라우드 메시징에 ServerKey, SenderID 정보가 있고 이값을 넣어주면 됩니다.

 

 

 

728x90
반응형

+ Recent posts