Android 使用 firebase 的通知
Android notification using fire base
您好,我正在开发网络推送通知api。最近我已经升级到 fire base 并使用下面的代码。
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/ 中有一个常规选项卡。在常规选项卡中有 Web API 键。在云消息选项卡下,我可以看到服务器密钥、旧版服务器密钥和发件人 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"));
您必须在 http-header
中设置发件人 ID
在云消息选项卡下,服务器密钥是发件人 ID。
在http-header中:
'Content-type: application/json'
'Authorization: key=AAAA0...' <= 你的钥匙在这里。
向此地址发送 POST 请求:
https://fcm.googleapis.com/fcm/send
错误消息是MismatchSenderId
。如果您 运行 android 上的此代码,您应该将 google-services.json
放入应用程序的 app
文件夹中,并且它应该是在 [=] 中为您的应用程序创建的那个13=]。如果您的 android 项目中有此文件,请打开它并检查 senderID
的值是否与您正在使用的 server key
的 senderId 相同。
您好,我正在开发网络推送通知api。最近我已经升级到 fire base 并使用下面的代码。
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/ 中有一个常规选项卡。在常规选项卡中有 Web API 键。在云消息选项卡下,我可以看到服务器密钥、旧版服务器密钥和发件人 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"));
您必须在 http-header
中设置发件人 ID在云消息选项卡下,服务器密钥是发件人 ID。
在http-header中:
'Content-type: application/json'
'Authorization: key=AAAA0...' <= 你的钥匙在这里。
向此地址发送 POST 请求: https://fcm.googleapis.com/fcm/send
错误消息是MismatchSenderId
。如果您 运行 android 上的此代码,您应该将 google-services.json
放入应用程序的 app
文件夹中,并且它应该是在 [=] 中为您的应用程序创建的那个13=]。如果您的 android 项目中有此文件,请打开它并检查 senderID
的值是否与您正在使用的 server key
的 senderId 相同。