尝试订阅 Firebase Cloud Messaging 上的主题会出错
Trying to subscribe to topic on Firebase Cloud Messaging gives Error
当我尝试订阅主题时出现以下错误:
.subscribeToTopic is not a function
const messaging = firebase.messaging();
messaging
.requestPermission()
.then(() => {
return messaging.getToken();
})
.then(token => {
messaging
.subscribeToTopic(token, 'allUsers')
.then(response=> {
console.log(JSON.stringify(response));
})
.catch(function(error) {
console.log('Error subscribing to topic:', error);
});
})
.catch(err => {
console.log('Unable to get permission to notify.', err);
});
如果我删除那行 .subscribeToTopic
并通过 http 添加一个 POST 调用,它会使用以下 url 工作:
https://iid.googleapis.com/iid/v1/TOKEN/rel/topics/TOPIC_NAME
我查看了这个问题和文档
https://firebase.google.com/docs/cloud-messaging/js/topic-messaging
您需要使用方法 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()
was released and replaced sendtotopic/sendtodevice
in version FCM v1
https://firebase.googleblog.com/2018/02/firebase-cloud-messaging-v1-now.html
https://firebase.google.com/docs/cloud-messaging/js/topic-messaging
当我尝试订阅主题时出现以下错误:
.subscribeToTopic is not a function
const messaging = firebase.messaging();
messaging
.requestPermission()
.then(() => {
return messaging.getToken();
})
.then(token => {
messaging
.subscribeToTopic(token, 'allUsers')
.then(response=> {
console.log(JSON.stringify(response));
})
.catch(function(error) {
console.log('Error subscribing to topic:', error);
});
})
.catch(err => {
console.log('Unable to get permission to notify.', err);
});
如果我删除那行 .subscribeToTopic
并通过 http 添加一个 POST 调用,它会使用以下 url 工作:
https://iid.googleapis.com/iid/v1/TOKEN/rel/topics/TOPIC_NAME
我查看了这个问题和文档
https://firebase.google.com/docs/cloud-messaging/js/topic-messaging
您需要使用方法 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()
was released and replacedsendtotopic/sendtodevice
in version FCM v1https://firebase.googleblog.com/2018/02/firebase-cloud-messaging-v1-now.html
https://firebase.google.com/docs/cloud-messaging/js/topic-messaging