如何从 javascript(针对 Chrome)注册到 GCM 主题

How to get registered into GCM topics from javascript (for Chrome)

Here 是关于如何在 Chrome 中实现 Google 云消息传递 (GCM) 的很好的文档。但是我在这里或任何地方都没有找到任何参考如何使用 javascript(对于 Chrome)订阅主题。

在这里我找到了如何为 Android 完成任务的参考资料: https://developers.google.com/cloud-messaging/topic-messaging#subscribe-to-a-topic

Java 代码(Android) 用于订阅 GCM 中的主题:

private void subscribeTopics(String token) throws IOException {
    GcmPubSub pubSub = GcmPubSub.getInstance(this);
    for (String topic : TOPICS) {
        pubSub.subscribe(token, "/topics/" + topic, null);
    }
}

我不想要什么

我不是在寻找 Chrome app/Extension 的方法。

我想要什么

我想向所有用户发送推送通知。到目前为止,我知道这可以通过两种方式实现:

  1. 将消息推送到主题
  2. 或者我必须:"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."

我想避开第 2 点。

我的问题

所以,我的问题是有没有什么方法可以使用 Java 脚本为 Chrome 浏览器(用于网页)订阅 GCM 主题?如果有,那怎么办?

网络推送不支持 GCM 主题。原因可能与即将添加的 payloads 有关,每个用户需要使用不同的密钥进行加密。

所以恐怕你被 2) 困住了。这当然取决于您有多少用户,但请记住,在当前情况下,如果您要使用主题,您可能会立即向数百万人发送消息。收到消息后,所有这些用户都会 "https-ing" 返回您的站点以获取显示通知所需的信息,这样您 运行 如果该主题被很多人使用,您将面临自己 DOSing 的风险.以 1000 个为一组进行批处理有助于限制传入流量。

发布答案以查看我评论的答案。


FCM 最近宣布(2016 年 10 月 17 日)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 Setting Up the JavaScript Client App.

的 Firebase 文档