首页 > 解决方案 > 在订阅 fcm 主题时,firebase 管理节点 sdk 和 firebase 客户端 sdk 之间是否存在冲突?

问题描述

使用 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-_.~%]+”匹配的字符串。'

如果我希望能够从客户端和服务器端订阅用户,我该如何解决这个问题?有冲突还是我错过了什么?

标签: androidfirebasefirebase-cloud-messagingfirebase-cli

解决方案


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

// 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')

推荐阅读