我使用以下代码成功地通过firebase将推送通知发送到服务器(.net C#)到Android设备的所有设备:
var result = "-1";
var webAddr = "https://fcm.googleapis.com/fcm/send";
var serverKey = "xxxxxxx";
var senderId = "xxxxxxx";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, "key="+ serverKey);
httpWebRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string strNJson = @"{
""to"": ""/topics/ServiceNow"",
""data"": {
""ShortDesc"": ""Some short desc"",
""IncidentNo"": ""any number"",
""Description"": ""detail desc""
},
""notification"": {
""title"": "": Incident No. number"",
""text"": ""This is Notification"",
""sound"":""default""
}
}";
streamWriter.Write(strNJson);
streamWriter.Flush();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}
return result;
现在,我想向特定或单个Android设备发送推送通知。请帮助我。
感谢。
答案 0 :(得分:0)
这是您要填充的JSON字符串中的'to'字段。
您需要为要将消息发送到的每个设备都拥有FCM令牌。我只是在服务器端工作,但有更多信息:
https://firebase.google.com/docs/cloud-messaging/android/first-message
答案 1 :(得分:0)
要向单个设备发送消息,您需要从中收集设备令牌(Android,iOS)。然后,该令牌可用于它们对FCM API的JSON有效负载。显示了许多示例here in the documentation。还有关于HTTP v1 API here的更全面的文档。该令牌应添加到"令牌"建立有效载荷时的关键:
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data":{
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
}
}
}
您似乎正在使用较旧的API(使用"到"字段),所以我建议您转到新的。