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.
Link to the Firebase documentation for Setting Up the JavaScript Client App.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.
I've looked around the docs. It seems that Topic Messaging for Chrome still isn't available for GCM
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
Node.Js Piping the Same Readable Stream into Multiple (Writable) Targets
Bootstrap: Open Another Modal in Modal
Access Properties of the Parent with a Handlebars 'Each' Loop
Initializing Select with Angularjs and Ng-Repeat
Basic Authentication with Fetch
How to Reload/Refresh Jquery Datatable
JavaScript Array Concat Not Working. Why
Find the Exact Height and Width of the Viewport in a Cross-Browser Way (No Prototype/Jquery)
How to Export Socket.Io into Other Modules in Nodejs
Read a Text File Using Node.Js
Should Github Be Used as a Cdn for JavaScript Libraries
JavaScript - Telling Setinterval to Only Fire X Amount of Times
Get the Current Year in JavaScript
Add Two Functions to Window.Onload
Create Copy of Multi-Dimensional Array, Not Reference - JavaScript
How to Change the Pop-Up Position of the Jquery Datepicker Control
JavaScript - Generating All Combinations of Elements in a Single Array (In Pairs)