Niranjan Godbole
Niranjan Godbole

Reputation: 2175

Android notification using fire base

Hi I am developing push notification in webapi. Recently I have upgraded to fire base and using the below code.

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";
        }

When i run the above piece of code i get error "{\"multicast_id\":6423299842549772135,\"success\":0,\"failure\":1,\"canonical_ids\":0,\"results\":[{\"error\":\"MismatchSenderId\"}]}". In https://console.firebase.google.com/project/androidnotification-17721223233/settings/general/ in setting option there is a General tab. Inside general tab there is Web API Key. Under cloud messaging tab i can see Server key,Legacy server key and sender id. In the above code for api key i used Server key,Legacy server key and webapi key but all fails. May i know which is the correct one to use? Any help would be appreciated. Thank you.

    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"));

Upvotes: 0

Views: 387

Answers (2)

Akram
Akram

Reputation: 2198

The error massage is MismatchSenderId. If you run this code on android, you should put google-services.json inside the app folder of your application and it should be the one that is created for your application in firebase console. If you have this file in your android project, open it and check that value of senderID is the same as the senderId of this server key that you are using.

Upvotes: 0

Younghwa Park
Younghwa Park

Reputation: 392

You have to set sender ID in http-header

Under cloud messaging tab, server key is sender ID.

In http-header :

'Content-type: application/json'

'Authorization: key=AAAA0...' <= your key here.

Send POST request to this address: https://fcm.googleapis.com/fcm/send

Upvotes: 1

Related Questions