我正在尝试使用 C# 从服务器发送推送通知,我使用了正确的注册 token 和 API key ,但仍然收到以下响应。
{"multicast_id":7864311304033595507,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
我正在关注此 url 以实现此解决方案 Send push to Android by C# using FCM (Firebase Cloud Messaging)
目前我正在尝试将通知发送到单个设备,但也想同时发送到多个设备,我使用 to : "/topics/all"
作为 url 中给出的,但它没有工作。如果我必须同时向多个设备发送通知,我该怎么办?
这是我的代码
try
{
string applicationID = "SERVER_KEY ";
string senderId = "SENDER_ID";
string deviceId = "ba92be2da78e7285";
WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "post";
tRequest.ContentType = "application/json";
var data = new
{
//to = deviceId,
to = deviceId,
notification = new
{
body = "Bring your existing apps and games to the Windows Store with the Desktop Bridge",
title = "Bridge",
sound = "Enabled"
}
};
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(data);
Byte[] byteArray = Encoding.UTF8.GetBytes(json);
tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
tRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
tRequest.ContentLength = byteArray.Length;
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();
string str = sResponseFromServer;
}
}
}
}
}
catch (Exception ex)
{
string str = ex.Message;
}
最佳答案
我的问题已经解决了。我使用的设备 ID 与注册 token ID 不同。所以我必须使用 FirebaseInstanceId.getInstance().getToken()
方法返回的注册 token ID。其余代码与我发布的问题相同。
关于c# - C# 中的 Firebase 云消息传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43941273/