代码之家  ›  专栏  ›  技术社区  ›  Niranjan Godbole

使用fire base的Android通知

  •  0
  • Niranjan Godbole  · 技术社区  · 7 年前

    您好,我正在webapi中开发推送通知。最近我升级到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"));
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Younghwa Park    7 年前

    您必须在http标头中设置发件人ID

    在“云消息”选项卡下,服务器密钥是发件人ID。

    在http头中:

    '内容类型:应用程序/json'

    '授权:密钥=AAAA0…'<=这里是你的钥匙。

    将邮寄请求发送到此地址: https://fcm.googleapis.com/fcm/send

        2
  •  0
  •   Akram    7 年前

    错误消息是 MismatchSenderId . 如果你在android上运行这段代码,你应该把 google-services.json 内部 app firebase console . 如果你的android项目中有这个文件,请打开它并检查 senderID 与此的senderId相同 server key 您正在使用的。