0

I am trying to use FCM for pushing notifications to Android device but it seems that I am missing something. These are steps that I've followed

  1. Create new project in https://console.firebase.google.com
  2. From Settings -> Cloud Messaging tab, I got Server key and Sender Id
  3. I used this code

        var regId = "device id";
        string SERVER_API_KEY = "Firebase Server key";
        var SENDER_ID = "Sender ID";
        var value = "Test Notification";
        WebRequest tRequest;
        tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
        tRequest.Method = "post";
        tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";
        tRequest.Headers.Add(string.Format("Authorization: key={0}", SERVER_API_KEY));
    
        tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));
    
        string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + value + "&data.time=" + System.DateTime.Now.ToString() + "&registration_id=" + regId + "";
        Console.WriteLine(postData);
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        tRequest.ContentLength = byteArray.Length;
    
        Stream dataStream = tRequest.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();
    
        WebResponse tResponse = tRequest.GetResponse();
    
        dataStream = tResponse.GetResponseStream();
    
        StreamReader tReader = new StreamReader(dataStream);
    
        string sResponseFromServer = tReader.ReadToEnd();
    
    
        tReader.Close();
        dataStream.Close();
        tResponse.Close();
    

I always get Mismatch Sender Id error. Am I missing any steps? or do I have to do something on mobile app?

AL.
  • 36,815
  • 10
  • 142
  • 281
ahmed
  • 21
  • 1
  • 10
  • Hi. [`MismatchSenderId`](https://firebase.google.com/docs/cloud-messaging/http-server-ref#error-codes) error may indicate that the Sender ID you're using to send the notification is not associated with the specified registration token. Are you positive that you are using the correct Sender ID (seen in Firebase Console > Project Settings > Cloud Messaging Tab)? – AL. Jan 18 '17 at 05:54
  • Yes in cloud messaging tab it is written clearly Sender ID – ahmed Jan 18 '17 at 05:56
  • A few points with regards to your payload: 1. `delay_while_idle` is already deprecated, so you can just remove that. 2. The `registration_id` parameter is wrong, it should be `registration_ids`. BUT, if you're only going to send to a single user, as advised in the docs, please use `to` instead. Can you try editing it now and see if you can send the message? – AL. Jan 18 '17 at 05:59
  • 1
    Still getting this error Error=MismatchSenderId. I changed it to "string postData = "collapse_key=score_update&time_to_live=108&data.message=" + value + "&data.time=" + DateTime.Now.ToString() + "&to=" + regId + "";" – ahmed Jan 18 '17 at 06:03
  • Hmm. I'm not too familiar with C#. But can you follow the syntax mentioned in this [post](http://stackoverflow.com/a/39778368/4625829)? For example, the Content type should be `application/json`. – AL. Jan 18 '17 at 06:29

1 Answers1

0

I found the error. As a back-end developer I worked on C# part only. There is one more step to be done from mobile application side. Mobile app should allow receiving messages from list of Sender Ids so the one that I use in my C# code must be allowed from mobile app side. Once I did that it worked

ahmed
  • 21
  • 1
  • 10
  • Hi ahmed. Can you provide more details on how you *allow receiving messages from list of Sender Ids*? Did you add it in your google-services.json? Cheers! – AL. Jan 18 '17 at 08:12