简体   繁体   中英

Firebase cloud messaging in C#

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM