Javascript 尝试订阅Firebase云消息上的主题时出错
当我尝试订阅某个主题时,出现以下错误: .subscribeToTopic不是一个函数 如果我删除Javascript 尝试订阅Firebase云消息上的主题时出错,javascript,firebase,firebase-cloud-messaging,progressive-web-apps,Javascript,Firebase,Firebase Cloud Messaging,Progressive Web Apps,当我尝试订阅某个主题时,出现以下错误: .subscribeToTopic不是一个函数 如果我删除.subscribeToPIC的那一行并通过http添加POST调用,它将使用以下url工作: 我看了一下这个问题和文件 您需要使用方法send而不是sendToTopic: // The topic name can be optionally prefixed with "/topics/". var topic = 'highScores'; var message = { data
.subscribeToPIC
的那一行并通过http添加POST调用,它将使用以下url工作:
我看了一下这个问题和文件
您需要使用方法
send
而不是sendToTopic
:
// 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);
});
send()
已在FCM v1版中发布并替换sendtotopic/sendtodevice
你好,彼得!谢谢你的回答。如果我错了,请纠正我,但答案似乎是将消息发送给已订阅的用户,但我的问题是如何订阅他们
。订阅主题(topic,'alluser'标记)
lines向我提供了问题中提到的错误。要订阅,您需要使用admin sdk//这些注册令牌来自客户端FCM sdk。var registrationTokens=[“您的注册令牌”/…“您的注册令牌”];//将注册令牌对应的设备订阅到//主题。admin.messaging().subscribeToTopic(registrationTokens,topic).then(函数(响应){console.log('Successfully subscribed to topic:',response);}).catch(函数(error){console.log('error subscribing to topic:',error);})
@devpato您需要先安装程序包npm install firebase admin--save
然后才能导入它var admin=require('firebase-admin')代码>然后初始化它var app=admin.initializeApp()代码>检查此处。firebase.messaging()的方法没有subscribeTopic
// 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);
});