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=", ServerKey])); tRequest.Headers.Add(string.Format("Sender: id=", 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 정보가 있고 이값을 넣어주면 됩니다.

Android 에서 Firebase와 Appcenter 이용해 Push Notification 을 처리하는 방법을 설명합니다.
이전에도 설명을 했지만 조금 다른 부분이 있어서 다시 포스팅 하게되었습니다.^^
Firebase 사이트로 들어가서 (https://console.firebase.google.com)
새프로젝트를 만듭니다.

아래와 같이 프로젝트 명을 입력하고 애널리틱스위치, 클라우드 Firebase 위치를 선택하고
약관 동의 후 프로젝트 만들기를 합니다.

프로젝트 만들기가 진행됩니다.
프로젝트 만들기기 완료되면 계속을 클릭합니다.

아래처럼 프로젝트가 생성된걸 확인할 수 있습니다.

여기서 오른쪽 화면의 안드로이드 아이콘을 클릭합니다.

안드로이드 앱을 등록합니다.

여기서 구성파일을 다운로드하는 화면이 나타나는데 구성파일을 다운로드 받습니다.
