【问题标题】:Sending topic notifications using firebase-admin使用 firebase-admin 发送主题通知
【发布时间】:2021-04-20 07:05:56
【问题描述】:

我需要通过 fcm 中的主题来实现广播通知。我正在使用 firebase-admin 发送这些。任何人都可以发布代码的 sn-p 以通过 node.js 发送这些通知吗?

【问题讨论】:

    标签: node.js firebase firebase-cloud-messaging backend


    【解决方案1】:
        const topic = "test";
        const payload = {
          notification: {
            title: "New news",
            body: "2:45",
          },
        };
        admin
          .messaging()
          .sendToTopic(topic, payload)
          .then((response2) => {
            // Response2 is a message ID string.
            console.log("Successfully sent message:", response2);
          })
          .catch((error) => {
            console.log("Error sending message:", error);
          });
    

    【讨论】:

      【解决方案2】:

      首先您需要为用户订阅给定的主题

      // These registration tokens come from the client FCM SDKs.
      var registrationTokens = [
         'YOUR_REGISTRATION_TOKEN_1',
        // ...
         'YOUR_REGISTRATION_TOKEN_n'
      ];
      
      // Subscribe the devices corresponding to the registration tokens to the
      // topic.
      admin.messaging().subscribeToTopic(registrationTokens, topic)
        .then(function(response) {
          // See the MessagingTopicManagementResponse reference documentation
          // for the contents of response.
          console.log('Successfully subscribed to topic:', response);
        })
        .catch(function(error) {
          console.log('Error subscribing to topic:', error);
        });
      

      然后您可以使用

      广播通知
      // The topic name can be optionally prefixed with "/topics/".
      var topic = 'highScores';
      
      var message = {
        data: {
          score: '850',
          time: '2:45'
        },
        topic: topic
      };
      
      // Send a message to devices subscribed to the provided topic.
      admin.messaging().send(message)
        .then((response) => {
          // Response is a message ID string.
          console.log('Successfully sent message:', response);
        })
        .catch((error) => {
          console.log('Error sending message:', error);
        });
      

      您可以阅读更多关于它的信息here

      【讨论】:

      • 已经浏览了他们的文档。我想知道我们应该在哪里通过 firebase admin 中的服务器密钥。在初始化应用程序时,我们应该在哪里传递服务器密钥。
      • 哦,好的,我明白了,只需到 Firebase 控制台,然后选择您的项目,然后选择其项目设置,然后选择服务帐户选项卡,然后复制初始化代码,然后通过以下方式下载服务帐户密钥生成私钥
      • 在初始化应用中我们有 admin.initializeApp({ credential: admin.credential.applicationDefault(), })
      • 这是什么 admin.credential.applicationDefault() 。我们是否需要在 applicationDefault() 函数中传递 serverKey。
      • admin.credential.applicationDefault() 返回从 Google 应用程序默认凭据创建的凭据,该凭据授予管理员对 Firebase 服务的访问权限。此凭据可用于调用 admin.initializeApp()。 Google 应用程序默认凭据可用于任何 Google 基础架构,例如 Google App Engine 和 Google Compute Engine。
      【解决方案3】:

      你需要 Firebase 项目 设备注册令牌 firebase 云功能

      firebase 通知的工作: 设备到设备通知: 从设备 1 - firebase 数据库(函数在写入数据库时​​触发) -firebase 云功能(云功能将向 dev 2 发送通知)- device2

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-12
        • 1970-01-01
        • 2018-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-28
        相关资源
        最近更新 更多