我已成功从服务器向FireBase Cloud Messaging(FCM)发送推送通知,以便将它们发送到Android设备。但我不知道为什么FCM不会将这些通知发送到设备。
我的代码如下,
从服务器(C#)向FCM发送通知
public static void SendPushNotification()
{
string serverKey = "AAAA...z";
try
{
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=" + serverKey);
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"to\": \"f..A:..p_G\",\"data\": {\"message\": \"This is a Firebase Cloud Messaging Topic Message!\",}}";
streamWriter.Write(json);
streamWriter.Flush();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
我对上述请求的回复
{ “multicast_id”:5002368547300, “成功”:1, “失败”:0, “canonical_ids”:0 “结果”:[{ “MESSAGE_ID”: “0:14200031%09c4rrr5787egg”}]}
我假设一旦FCM收到新通知,它会将这些通知推送到相应的Android设备。
但对我来说,它无效。
答案 0 :(得分:0)
我通过参考以下链接获得了解决方案
Firebase push notification not showing on phone despite successful response
我改变了'json'如上面链接中提到的字符串,它正在工作。