【问题标题】:Is there a confilict between firebase admin node sdk and firebase client sdk when it comes to subscribing to fcm topics?在订阅 fcm 主题时,firebase 管理节点 sdk 和 firebase 客户端 sdk 之间是否存在冲突?
【发布时间】:2020-01-31 08:34:13
【问题描述】:

使用 firebase 客户端 sdk 订阅 firebase 控制台消息主题时:

FirebaseMessaging.getInstance().subscribeToTopic("/topics/sometopic")

我收到警告:

'/topics/sometopic' 已弃用。 subscribeToTopic 中只能使用“sometopic”。

但是,当使用 firebase admin sdk 服务器端订阅主题时:

admin.messaging().subscribeToTopic(user.messaging_token, 'sometopic')

我在云服务器上收到警告

'提供给 subscribeToTopic() 的主题必须是匹配的字符串 格式为“/topics/[a-zA-Z0-9-_.~%]+”。'

如果我希望能够同时从客户端和服务器端订阅用户,我该如何解决这个问题?是否存在冲突或我缺少什么?

【问题讨论】:

    标签: android firebase firebase-cloud-messaging firebase-cli


    【解决方案1】:

    在客户端和服务器端都会为用户订阅一个主题,不同的是在服务器端你可以为多个用户订阅同一个主题:

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

    您需要使用一个不包含/的主题:

    FirebaseMessaging.getInstance().subscribeToTopic("news")
    

    admin.messaging().subscribeToTopic(user.messaging_token, 'news')
    

    【讨论】:

    • 谢谢彼得,但我的问题是,当我使用 admin sdk 时(如您评论的最后一行),它希望令牌具有正斜杠。而来自客户端的命令不需要正斜杠。
    • @thebutcher_254 我认为在 admin sdk 中你需要使用 subscribetotopic("/topics/news") 而在客户端你需要使用 subscribetotopic("news") 但在这两个主题中都是news。您应该尝试一下,看看它是同一个主题还是会创建一个新主题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    • 2019-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多