我使用以下 C# Web 方法来 webRequest Firebase Cloud Messaging
public void messgaeFCM(String cToken) {
try
{
//send message to clientToken through FCM server
WebRequest wr = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
wr.Method = "POST";
wr.ContentType = "application/json";
wr.Headers.Add("Authorization", "Key='" + FIREBASE_PROJECT_ID +"'");
String notification = "{ 'data': " +
"{" +
"'title': 'Firebase notification'," +
"'detail': 'I am firebase notification. you can customise me. enjoy'," +
"}," +
"'to' : '" + cToken + "'" +
"}";
byte[] byteArray = Encoding.UTF8.GetBytes(notification);
Stream dataStream = wr.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = wr.GetResponse();
}
catch (Exception e)
{
new general().logError(e.Message);
}
}
FIREBASE_PROJECT_ID值设置为 FCM 项目 ID。
为有效客户端令牌设置的 cToken 值。
上面的代码总是返回以下错误:
远程服务器返回错误:(401( 未经授权。
不知道出了什么问题...
wr.Headers.Add("Authorization", "Key='" + FIREBASE_PROJECT_ID +"'");
尝试删除单引号:
wr.Headers.Add("Authorization", "Key=" + FIREBASE_PROJECT_ID);