Javascript 尝试订阅Firebase云消息上的主题时出错

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

当我尝试订阅某个主题时,出现以下错误:

.subscribeToTopic不是一个函数

如果我删除
.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);
  });