3

I am trying to send push notifications from server in C#, i am using the correct registration token and API key but still getting the following response.

{"multicast_id":7864311304033595507,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}

I am following this url to implement this solution Send push to Android by C# using FCM (Firebase Cloud Messaging)

Currently i am trying to send notification to a single device but also want to send to multiple device at once, I used to : "/topics/all" as given in the url but it doesn't work. What should I do if I have to send notification to multiple device at once?

Here is my code

try
      {

          string applicationID = "SERVER_KEY ";

          string senderId = "SENDER_ID";

          string deviceId = "ba92be2da78e7285";

          WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
          tRequest.Method = "post";
          tRequest.ContentType = "application/json";
          var data = new
          {
              //to = deviceId,
              to = deviceId,
              notification = new
              {
                  body = "Bring your existing apps and games to the Windows Store with the Desktop Bridge",
                  title = "Bridge",
                  sound = "Enabled"

              }
          };
          var serializer = new JavaScriptSerializer();
          var json = serializer.Serialize(data);
          Byte[] byteArray = Encoding.UTF8.GetBytes(json);
          tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
          tRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
          tRequest.ContentLength = byteArray.Length;
          using (Stream dataStream = tRequest.GetRequestStream())
          {
              dataStream.Write(byteArray, 0, byteArray.Length);
              using (WebResponse tResponse = tRequest.GetResponse())
              {
                  using (Stream dataStreamResponse = tResponse.GetResponseStream())
                  {
                      using (StreamReader tReader = new StreamReader(dataStreamResponse))
                      {
                          String sResponseFromServer = tReader.ReadToEnd();
                          string str = sResponseFromServer;
                      }
                  }
              }
          }
      }
      catch (Exception ex)
      {
          string str = ex.Message;
      }   
Community
  • 1
  • 1
yadavr
  • 1,388
  • 3
  • 22
  • 49
  • It's hard to be sure. The response it gave you seems to mean that the server key is good, but the registration token is not good: https://firebase.google.com/docs/cloud-messaging/server#auth Maybe the app was uninstalled and reinstalled on the android device and a new token was generated? I'm just guessing. – albert c braun May 12 '17 at 15:41
  • Also, you might want to log on to the firebase console (if you have the credentials for the user that set up this project in there) and send a message directly to that individual app registration token from there. You can also send a message to all devices, or to a topic. https://console.firebase.google.com/ – albert c braun May 12 '17 at 15:51
  • @albertbraun, thanx for your quick reply, registration token you meant with device id, right? – yadavr May 12 '17 at 16:03
  • @albertbraun, i myself did the settings in firebase console and messages are delivering fine from there, I will definitely check it for individual registration token. I need to know how can i send message to all devices from my server, is the looping through all the device id is the only way? – yadavr May 12 '17 at 16:06
  • i believe that topics is the right approach. i have had success with that approach. The firebase system can take up to 24 hours to become aware of topics which were created by the apps using a call like FirebaseMessaging.getInstance().subscribeToTopic("all"); Also you can try sending to a topic using curl from a command line as another way to test the code you are putting in your server: http://stackoverflow.com/questions/37371990/how-can-i-send-a-firebase-cloud-messaging-notification-without-use-the-firebase – albert c braun May 12 '17 at 16:18
  • 1
    for the registration token, i mean the value that is returned by this code: String instanceIDToken = FirebaseInstanceId.getInstance().getToken(); which is running from inside onTokenRefresh() in your implementation of FirebaseInstanceIdService – albert c braun May 12 '17 at 17:09
  • Is the deviceId in your code snippet the actual registration token you were sending the message to? If so, I'm fairly sure that's not a Registration token, hence the `InvalidRegistration` error. – AL. May 13 '17 at 04:02
  • @albertbraun, thnx for clarification, my problem resolved. I was using device id which is different then registration token id. So I have to use registration token id returned by FirebaseInstanceId.getInstance().getToken() method. – yadavr May 13 '17 at 06:47
  • How to set notification icon here..i used like this var data = new { to = deviceId, notification = new { body = "Welcome", title = "hey", icon = "https://placeimg.com/250/250/nature" , sound="default" } }; but i am receiving default notification only – Nagaraj S Feb 06 '18 at 12:18

1 Answers1

1

My problem got resolved. I was using device id which is different then registration token id. So I have to use registration token id returned by FirebaseInstanceId.getInstance().getToken() method. Rest of the code is same what I posted in question.

yadavr
  • 1,388
  • 3
  • 22
  • 49
  • How to set notification icon here..i used like this `var data = new { to = deviceId, notification = new { body = "Welcome", title = "hey", icon = "https://placeimg.com/250/250/nature" , sound="default" } };` but i am receiving default notification only – Nagaraj S Feb 06 '18 at 12:16