Firebase cloud messaging in c #

I am trying to send push notifications from a server in C #, I am using the correct registration token and API key, but I still get 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 with C # using FCM (Firebase Cloud Messaging)

I am currently trying to send a notification to one device but also want to send to multiple devices at once, I used to : "/topics/all"

as stated in the url, but it doesn't work. What if I need to send a notification to multiple devices 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;
      }   

      

+3


source to share


1 answer


My problem is resolved. I used a device id which is different from the registration id. So I have to use the registration ID returned by the method FirebaseInstanceId.getInstance().getToken()

. The rest of the code is the same as I wrote.



+1


source







All Articles