-2

hi I'm creating a web app, i'm trying to have the toggle in the website it self if turned on it sends he is subscribed to a topic in firebase. i'm using flutter is there a way I tried searching all over but couldn't grasp it even in the documentation

I tried this way

    <button type="button" onclick="subscribeTokenToTopic">Tag VIP User</button>

 importScripts("https://www.gstatic.com/firebasejs/8.10.0/firebase-app.js");
    importScripts("https://www.gstatic.com/firebasejs/8.10.0/firebase-messaging.js");

  const config = {
    apiKey: "x",
          authDomain: "x",
          projectId: "x",
          storageBucket: "x",
          messagingSenderId: "x",
          appId: "x",
          measurementId: "x")
    };
    firebase.initializeApp(config);



      function subscribeTokenToTopic(token, topic) {
        await FirebaseMessaging.subscribeToTopic('weather');

      }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    Your code is javascript? – Mochamad Nizwar Syafuan Jul 31 '22 at 21:10
  • You tagged your question as being about `[flutter]` and `[php]`, yet the only code you shared is in JavaScript. To maximize the chances of somebody here being able to help, I recommend reading [how to ask a good question](https://stackoverflow.com/help/how-to-ask) and update your question based on that guidance in your question by clicking the `edit` link under it. – Frank van Puffelen Jul 31 '22 at 21:18
  • I believe there is nothing todo with `[flutter]` on this question, can you confirm nasser? – Mochamad Nizwar Syafuan Jul 31 '22 at 21:22

2 Answers2

0

Here is code to subscribe and unsubscribe Cloud Messaging's topic

///Subscribe to topic
await FirebaseMessaging.instance.subscribeToTopic('weather');

///Unsubscribe from topic
await FirebaseMessaging.instance.unsubscribeFromTopic('weather');

You can find more details here https://firebase.flutter.dev/docs/messaging/usage#subscribing-to-topics

  • i have tried using a similar method didn't word i used a button in my website which is linked to a function with on click function and it gives me subcribetokentotopic is not defined at html button element – nasser mansoor Jul 31 '22 at 20:54
0

The documentation on subscribing to FCM topics in a web app, shows that this is only possible through the (server-side) Admin SDKs for Node, Java, Python, Go, and C#. It is explicitly not possible to subscribe to an FCM topic directly from client-side JavaScript SDK.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807