我試圖從C#中的服務器發送推送通知,我使用正確的註冊令牌和API密鑰,但仍獲得以下響應。C中的Firebase雲消息傳遞#
{"multicast_id":7864311304033595507,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
我下面這個網址來實施該解決方案Send push to Android by C# using FCM (Firebase Cloud Messaging)
目前,我嘗試發送通知到一個單一的設備,但也想發送給多個設備一次,我用to : "/topics/all"
在給定的網址,但它不起作用。如果我必須一次向多臺設備發送通知,該怎麼辦?
這裏是我的代碼
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;
}
很難確定。它給你的迴應似乎意味着服務器密鑰是好的,但註冊令牌不好:https://firebase.google.com/docs/cloud-messaging/server#auth也許應用程序已卸載並重新安裝android設備和新的令牌是否生成?我只是猜測。 –
另外,您可能需要登錄到Firebase控制檯(如果您擁有設置此項目的用戶的憑據),並從那裏直接向該單個應用程序註冊令牌發送消息。您還可以向所有設備或主題發送消息。 https://console.firebase.google.com/ –
@albertbraun,thanx爲您的快速回復,註冊令牌您的設備ID,對吧? – yadavr