How to Subscribe to Topics with Web Browser Using Firebase Cloud Messaging

How to subscribe to topics with web browser using Firebase Cloud Messaging

There is no direct API to subscribe clients to topics in the Firebase Cloud Messaging SDK for JavaScript. Instead you can subscribe a token to a topic through the REST API. Calling this API requires that you specify the FCM server key, which means that you should only do this on a trusted environment, such as your development machine, a server you control, or Cloud Functions. This is necessary, since having the FCM server key allows sending messages on your app's behalf to all of your app's users.

It turns out that in my tests I was using an older project, where client API keys were allows to subscribe to topics. This ability has been removed from newer projects for security reasons.

In Node.js you could for example call the REST API to create a relation mapping for an app instance like this:

function subscribeTokenToTopic(token, topic) {
fetch('https://iid.googleapis.com/iid/v1/'+token+'/rel/topics/'+topic, {
method: 'POST',
headers: new Headers({
'Authorization': 'key='+fcm_server_key
})
}).then(response => {
if (response.status < 200 || response.status >= 400) {
throw 'Error subscribing to topic: '+response.status + ' - ' + response.text();
}
console.log('Subscribed to "'+topic+'"');
}).catch(error => {
console.error(error);
})
}

Where fcm_server_key is the FCM server key, taken from the Firebase console of your project.


Update: the option to subscribe a FCM token a topic is now also part of most of the Firebase Admin SDK, which makes it easier to do this on supported environments. For more on this, see the documentation on subscribing the client app to a topic/

Web Firebase Cloud Messaging - How to subscribe to a topic from client side?

The client-side web SDK for Firebase Cloud Messaging does not support subscribing to topics. If you want to subscribe your web clients to FCM topics, you will have to do so from server-side code, either through the Admin SDK or through the corresponding REST APIs.

Firebase cloud messaging - subcribing to topic from a JavaScript client

The JavaScript SDK for Firebase Cloud Messaging does not support subscribing to a topic.

To subscribe a web app to a topic you will have to the REST API. Since this requires that you specify the FCM Server Key, you'll have to do this from a trusted environment, such as your development machine, a server you control, or Cloud Functions.

Once you've subscribed a web client to a topic it can receive the message similar to how iOS and Android do.

FCM, How to make all users in group to subscribe to topic

There is no API to get the current subscribers to a topic, nor is there an API that verbatim subscribes all subscribers to one topic to another topic.

If you already track group membership yourself, you can either let each client subscriber themselves to the additional topic, or you can determine the list of tokens for the group members and then subscribe them to the topic on the server.

Subscribe to Topics in FCM for Chrome

Update: Announced just recently (October 17, 2016), Firebase JavaScript library:

Today we're announcing web support for Firebase Cloud Messaging (FCM) with the release of a JavaScript library. This extends our current browser support, enables a dramatically simpler implementation process, and brings powerful features such as Topics and Device Group Messaging to the web.

--

With the FCM JavaScript library, you can send web push notifications to single devices, topics or groups of devices. With the addition of topic support on the Web, we are making it possible for developers to send a message to their Android, iOS and Web users who have opted in to a particular topic. To take advantage of topics and device groups, you can use the server-side APIs to manage your topics and groups subscriptions.

Link to the Firebase documentation for Setting Up the JavaScript Client App.


I've looked around the docs. It seems that Topic Messaging for Chrome still isn't available for GCM and FCM.

Can I subscribe a topic to another topic in Firebase Cloud Messaging

No, only client apps can determine which topics they want to subscribe to in order to receive messages. You can't route messages going to a topic through another topic. What you would have to do instead is program the server side code that sends the message to send to multiple topics as needed.



Related Topics



Leave a reply



Submit