嗨我正在开发webapi推送通知。最近我已升级到消防基地并使用下面的代码。使用消防基地的Android通知
string deviceId = "dmsGj47_Ulk:APA91bEMkevJzP2_mV2ALCSc_kSTZw57gMBEP2TWtHkrPl1VGTPJYvb0Be_F0zrzsttk78wopecHT_Af3ShAU39sMku0Ht09Pz22YevWkk6hkHjjl87DEvz_7mUJ3vGc05j4n0wjfKR7";
string message = "Demo Notification";
string tickerText = "Patient Registration";
string contentTitle = "Hi";
string postData =
"{ \"registration_ids\": [ \"" + deviceId + "\" ], " +
"\"data\": {\"tickerText\":\"" + tickerText + "\", " +
"\"contentTitle\":\"" + contentTitle + "\", " +
"\"message\": \"" + message + "\"}}";
string apiKey = "";
string response = SendGCMNotification(apiKey, postData);
return Request.CreateResponse(HttpStatusCode.OK, "notification sent");
private string SendGCMNotification(string apiKey, string postData, string postDataContentType = "application/json")
{
ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateServerCertificate);
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://android.googleapis.com/gcm/send");
Request.Method = "POST";
Request.ContentType = postDataContentType;
Request.Headers.Add(string.Format("Authorization: key={0}", apiKey));
Request.ContentLength = byteArray.Length;
Stream dataStream = Request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
try
{
WebResponse Response = Request.GetResponse();
HttpStatusCode ResponseCode = ((HttpWebResponse)Response).StatusCode;
if (ResponseCode.Equals(HttpStatusCode.Unauthorized) || ResponseCode.Equals(HttpStatusCode.Forbidden))
{
var text = "Unauthorized - need new token";
}
else if (!ResponseCode.Equals(HttpStatusCode.OK))
{
var text = "Response from web service isn't OK";
}
StreamReader Reader = new StreamReader(Response.GetResponseStream());
string responseLine = Reader.ReadToEnd();
Reader.Close();
return responseLine;
}
catch (Exception e)
{
}
return "error";
}
当我运行上面的代码,我得到错误“{\"multicast_id\":6423299842549772135,\"success\":0,\"failure\":1,\"canonical_ids\":0,\"results\":[{\"error\":\"MismatchSenderId\"}]}".
在https://console.firebase.google.com/project/androidnotification-17721223233/settings/general/在设置选项有一个常规选项卡,里面一般选项卡上有网页API密钥。在云消息选项卡,我可以看到服务器密钥,Legacy服务器密钥和发件人ID在上面的API密钥代码中,我使用了服务器密钥,传统服务器密钥和WebAPI密钥,但都失败了,我可以知道哪一个是正确的,可以使用任何帮助,谢谢。
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://fcm.googleapis.com/fcm/send");
Request.Method = "POST";
Request.ContentType = postDataContentType;
Request.Headers.Add(string.Format("Authorization: key={0}", apiKey));
Request.Headers.Add(string.Format("Sender: id={1}", "507022575461"));
谢谢您的回复。我可以知道这是在头文件中设置senderid的方法吗? Request.Headers.Add(string.Format(“SenderID:key = {1}”,“32323233”)); –
key:'Authorization',value ='key = AAAA6pm ...' –
谢谢。我更新我的代码,并得到senderid头错误为索引(基于零)必须大于或等于零,并小于参数列表的大小..我可以知道什么是正确的方式来添加它? –