我已成功从服务器向 FireBase Cloud Messaging(FCM)发送推送通知,以便将它们发送到 android 设备。但我不知道为什么 FCM 不会将这些通知发送到设备。
我的代码如下,
从服务器通知 FCM(C#)
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,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:14200031%09c4rrr5787egg"}]}
我假设一旦 FCM 收到新通知,它会将这些通知推送到相应的 android 设备。
但对我来说,它不起作用。