0

I have used Webpack to create a module bundle for Firebase Messaging for Web (javascript).

I have everything working correctly except I can't seem to implement the "subscribeToTopic" function. I followed the instructions on the site here , but when I export and run it I get "TypeError: n.subscribeToTopic is not a function".

I suspect I need to include another import for subscribeToToken, but the documentation doesn't tell me. Does anyone know what I can do?

import { initializeApp } from "firebase/app";
import { getMessaging, getToken, isSupported } from "firebase/messaging";

const firebaseApp = initializeApp({
    //all the info here
});

const messaging = getMessaging(firebaseApp);

getToken(messaging, { vapidKey: 'thekeyhere' }).then((currentToken) => {
  if (currentToken) {
    //test - immediately subscribe token to test topic
    messaging.subscribeToTopic(currentToken, topic);
  }else{
     console.log('need to get new token...'); 
  }
 
})
.catch((error) => {
    console.log('issupported was false:', error);
});
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Delmontee
  • 1,898
  • 2
  • 26
  • 44

1 Answers1

1

As explained in the doc you refer to, the subscribeToTopic() is a method from the Admin SDK, not from the JS SDK.

You can pass a list of registration tokens to the Firebase Admin SDK subscription method to subscribe the corresponding devices to a topic.

So you cannot call this method from the JS SDK. You can however implement a Cloud Function (Cloud Functions use the Admin SDK) and call it from your app.

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • Ah balls! Thanks for the pointer. How annoying. – Delmontee Jul 28 '22 at 07:43
  • Actually it's not that complicated :-) Just take the time to fully understand the Cloud Functions concepts and it will not take that much time. in addition to the Cloud Function doc, I would suggest you watch the 3 videos about "JavaScript Promises" from the Firebase video series: https://firebase.google.com/docs/functions/video-series/ – Renaud Tarnec Jul 28 '22 at 07:54