Here is a nice documentation on how to implement Google Cloud Messaging (GCM) in Chrome. But I do not found any reference here or anywhere how to subscribe to a topic using javascript (for Chrome).
Here I have found a reference how to do the task for Android:
https://developers.google.com/cloud-messaging/topic-messaging#subscribe-to-a-topic
Java code(Android) for subscribe to a topic in GCM:
private void subscribeTopics(String token) throws IOException {
GcmPubSub pubSub = GcmPubSub.getInstance(this);
for (String topic : TOPICS) {
pubSub.subscribe(token, "/topics/" + topic, null);
}
}
WHAT I AM NOT LOOKING FOR
I am NOT looking ways for Chrome app/Extension.
WHAT DO I WANT
I want to send push notification to all my users. So far I know this can be achievable in two ways:
- Push message to a topic
- Or I have to: "You need to send the list of reg id of devices and also this list should not exceed 1000 this is a limitation of GCM if you want to send message to more than 1000 devices then you need to break the list in chunks of 1000."
I want to avoid point number 2.
MY QUESTION
So, My question is is there any way to subscribe to a topic for GCM using Javascript for Chrome browser (for web pages)? If there, then how to do that?
GCM topics are not supported by web push. The reason is likely to be related to the upcoming addition of payloads which are required to be encrypted with a different key per user.
So I'm afraid you are stuck with 2). This of course depends on how many users you have but keep in mind that with the current state of affairs you could be instantly sending a message to millions of people if you were to be using topics. Upon receiving the message all those users would be "https-ing" back to your site to fetch the information needed to display the notification so you run the risk of DOSing yourself if the topic were to be used by lot's of people. Batching in groups of 1000 helps throttling the incoming traffic.
Posting an answer for visibility to the answer I commented.
FCM recently announced (October 17, 2016) the 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.