Попытка подписки на topi c в Firebase Cloud Messaging приводит к ошибке - PullRequest
0 голосов
/ 09 января 2020

Когда я пытаюсь подписаться на topi c, я получаю следующую ошибку:

.subscribeToTopi c не является функцией

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 и добавляю вызов POST через http, он работает, используя следующий URL: https://iid.googleapis.com/iid/v1/TOKEN/rel/topics/TOPIC_NAME

Я посмотрел на это вопрос и документы Облачные сообщения в облаке Функции: admin.messagin (...). отправка не является функцией

https://firebase.google.com/docs/cloud-messaging/js/topic-messaging

1 Ответ

1 голос
/ 09 января 2020

Вам необходимо использовать метод 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() выпущен и заменен sendtotopic/sendtodevice в версии FCM v1

https://firebase.googleblog.com/2018/02/firebase-cloud-messaging-v1-now.html

https://firebase.google.com/docs/cloud-messaging/js/topic-messaging

...