fcm push C# 서버단 추가
Posted by Albert 2810Day 11Hour 44Min 35Sec ago [2017-08-10]
public class FcmHelper
{
public static String SendNotificationFromFirebaseCloud(string googleApiKey,string to,string message)
{
//Test일때 사용
string googleKey = "구글의 apikey";
string toData = "/topics/news"; // 보낼 디바이스 토큰아이디 혹은 토픽 이름
string sendMessage = "This is a Firebase Cloud Messaging Topic Message" ; // 보낼내용
var result = "-1";
var webAddr = "https://fcm.googleapis.com/fcm/send";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Headers.Add("Authorization:key=" + googleApiKey);
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"to\": \""+to+"\",\"data\": {\"message\": \""+message+"\",}}";
streamWriter.Write(json);
streamWriter.Flush();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}
return result;
}
}
보내는 json type은 data 와 notification 두가지가 있는데 저는 notification으로 하니 잘된다.
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon",
"sound" : "mySound"
}
}