android - 在订阅 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-_.~%]+”匹配的字符串。'
如果我希望能够从客户端和服务器端订阅用户,我该如何解决这个问题?有冲突还是我错过了什么?
解决方案
在客户端和服务器端都会为用户订阅一个主题,不同的是在服务器端你可以为多个用户订阅同一个主题:
// 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')
推荐阅读
- postgresql - 在 spring 中将自定义 postgres sql 类型映射到 JPA 或 JDBC
- javascript - 在d3树图中设置节点及其子节点的自定义x,y位置
- javascript - 在 Safari 12 [Safari App Extension] 中使用 JavaScript 打开新标签页
- reactjs - 如何使用 ejs 添加对现有快速多页项目的反应?
- javascript - 谷歌趋势嵌入和 X-Frame-Options
- python - Elasticsearch streaming_bulk 生成器不可迭代
- c# - 配置 HttpClientFactory 以使用当前请求上下文中的数据
- jquery - 重定向页面并检测它是否已加载
- angular - 如何使用 Angular 通用创建 Angular 自定义元素:SSR
- javascript - 如何限制输入数字范围?