【问题标题】:Firebase cloud messaging in C#C# 中的 Firebase 云消息传递
【发布时间】:2017-10-11 23:35:00
【问题描述】:

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

{"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)

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

这是我的代码

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

【问题讨论】:

  • 很难确定。它给你的响应似乎意味着服务器密钥是好的,但注册令牌不好:firebase.google.com/docs/cloud-messaging/server#auth 可能是该应用程序被卸载并重新安装在android设备上并生成了一个新令牌?我只是猜测。
  • 另外,您可能希望登录到 Firebase 控制台(如果您有在那里设置此项目的用户的凭据)并从那里直接向该单独的应用注册令牌发送消息.您还可以向所有设备或主题发送消息。 console.firebase.google.com
  • @albertbraun,感谢您的快速回复,您指的是设备 ID 的注册令牌,对吧?
  • @albertbraun,我自己在 firebase 控制台中进行了设置,并且消息从那里发送得很好,我一定会检查它是否有个人注册令牌。我需要知道如何从我的服务器向所有设备发送消息,循环遍历所有设备 ID 是唯一的方法吗?
  • 对于注册令牌,我的意思是此代码返回的值: String instanceIDToken = FirebaseInstanceId.getInstance().getToken();在您的 FirebaseInstanceIdService 实现中从 onTokenRefresh() 内部运行

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


【解决方案1】:

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

【讨论】:

  • 如何在此处设置通知图标..我像这样使用var data = new { to = deviceId, notification = new { body = "Welcome", title = "hey", icon = "https://placeimg.com/250/250/nature" , sound="default" } };,但我只收到默认通知
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-10
相关资源
最近更新 更多