如何使用 Firebase admin SDK 发送消息

How to send messages with Firebase admin SDK

使用的 Nuget:

FireBaseAdmin v1.9.2

我正在尝试使用 firebase admin 向 fcm 发送推送通知。 我阅读了 documentation 但找不到任何其他好的来源来弄清楚如何正确使用它。 我不知道在哪里添加 serverkey 和 senderid。

我做了一个发送推送通知的方法。 有任何我可以遵循的示例或任何其他文档吗?

public override async Task<string> Send(List<String> tokens, string title, string body)
{
   var message = Message()
   {
      Tokens = tokens,
      Notification = new Notification()
      {
         Title = title,
         Body = body
      }
   };

   return await FirebaseMessaging.DefaultInstance.SendAsync(message).ConfigureAwait(false);
}

当我调试此代码时,来自 SendAsync 的响应为空。 这可能是因为我没有给它 serverkey 和 senderId 但我会期待像 serverKeyNotFound 这样的错误。

Admin SDK 中未使用服务器密钥和发件人 ID 参数。您只需要用一些 GoogleCredential 实例化一个 FirebaseApp,如 https://firebase.google.com/docs/admin/setup.

所示

最重要的是,您的代码在语法上似乎不正确。 Message class 中没有 Tokens 属性。为此,您需要 MulticastMessage class。所以我希望上面的代码编译失败。