Search code examples
firebasefirebase-cloud-messagingtoken

Firebase Cloud Messaging for web: how to send to multiple tokens?


i hope i don't get downvotes on this one, i've been trying to set up web notifications for my CMS using Firebase, and i noticed that Google's Firebase documentations on the topic are huge, i mean very huge you get confused.

So far i managed to add the functionality of letting people subscribe to the notification by letting the browser asking their permission to send them notifications, then i get the unique tokens after they accept and store those tokens in my database, i also managed to change the location of the service worker and everything looks good and dandy.

Now, i want to send a notification to all my users (tokens) that are stored in my database, i think looping through them and send a notification using CURL to each one individually is a nasty solution. I can't find a documentation on how to send a notification to all my tokens in one CURL call.

This is my code so far:

<!-- Firebase Technologies -->
<!-- Firebase App is always required and must be first -->
<script src="https://www.gstatic.com/firebasejs/5.1.0/firebase-app.js"> 
</script>
<script src="https://www.gstatic.com/firebasejs/5.1.0/firebase- 
messaging.js"></script>
<script>
// Initialize Firebase
var config = {
apiKey: "AIzaSyAR84lF2vbnfUWPZ2899dnqiTthgvfv7Ms",
authDomain: "lazemnicms.firebaseapp.com",
databaseURL: "https://lazemnicms.firebaseio.com",
projectId: "lazemnicms",
storageBucket: "lazemnicms.appspot.com",
messagingSenderId: "268754114869"
};
firebase.initializeApp(config);
messaging = firebase.messaging();
//Registering the service worker
navigator.serviceWorker.register("firebase-messaging-sw.js", {scope: "firebase-cloud-messaging-push-scope"}).then(function (registration) {

messaging.useServiceWorker(registration);

}).catch(function (err) {
  // registration failed :(
  console.log('ServiceWorker registration failed: ', err);
});

permissionGranted = false;

messaging.getToken().then(function(currentToken) {
if (currentToken) {
    console.log(currentToken);
    permissionGranted = true;
    //sendTokenToServer(currentToken);
    //updateUIForPushEnabled(currentToken);
} else {
    permissionGranted = false;
}
}).catch(function(err) {
    permissionGranted = false;
});

And also if a user got a refreshedToken, how can i know this user's old token so i can remove it from my database after i store his/her new token?

Those questions are really troubling me.

Thanks in advance.


Solution

  • The v1 API currently only allows sending to a single token at a time. Multicast is planned to be added (it was present in the previous API), but I don't have a timeline for when it will be available. So right now that means that you'll need to do a call to the FCM API for each token.

    There is nothing built-in to know the previous token for a user. The typical way to do this is to keep the "last known token" in local storage, and unregister that when you get a new token. Alternatively, you can instead catch the errors that indicate an invalid token when sending messages, and remove them from the database that way (see an example of that here). A combination of these two approaches is probably best.