c# - C# 中的 Firebase 云消息传递

标签 c# android firebase push-notification firebase-cloud-messaging

我正在尝试使用 C# 从服务器发送推送通知,我使用了正确的注册 token 和 API key ,但仍然收到以下响应。

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

我正在关注此 url 以实现此解决方案 Send push to Android by C# using FCM (Firebase Cloud Messaging)

目前我正在尝试将通知发送到单个设备,但也想同时发送到多个设备,我使用 to : "/topics/all" 作为 url 中给出的,但它没有工作。如果我必须同时向多个设备发送通知,我该怎么办?

这是我的代码

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

最佳答案

我的问题已经解决了。我使用的设备 ID 与注册 token ID 不同。所以我必须使用 FirebaseInstanceId.getInstance().getToken() 方法返回的注册 token ID。其余代码与我发布的问题相同。

关于c# - C# 中的 Firebase 云消息传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43941273/

相关文章:

c# - Windows Phone 锁屏下导航

c# - 查找另一个对象的对象的有效解决方案

android - 安装 Android Studio - 未安装 SDK 组件

android - 如何使 LinearLayout 内容滚动?

swift - 使用 caseInSensitive 在 Firebase 中保存和/或查询用户显示名称?

javascript - 无法从 firebase 数据库的时间戳中获取秒数

c# - 如何在不假其顶层属性的情况下将表单作为控件添加到另一个表单中?

c# - 当图片框位于另一个图像上方时如何使它透明

android - 我怎样才能获得 sleep 时间

firebase - AngularFire2 在数组中嵌套 *ngFor 数组...但 Firebase 没有数组...?