【问题标题】:Node.js GCM Push Notification number of IDs allowed?Node.js GCM 推送通知允许的 ID 数量?
【发布时间】:2014-08-29 13:31:39
【问题描述】:

我进行了一些搜索,但找不到此问题的答案,如果重复,请见谅。

我正在使用 Node.JS 向 Android 设备发送 GCM 通知。我在数组中传递注册 ID 列表,然后通过 Sender.send 函数发送它。我想知道,每个发送请求允许的 ID 数量是否有最大限制?就像 send 函数中每次调用 1000 次一样,或者不存在这样的限制?

我记得读过有关使用 JSON 格式一次发送多达 1000 个 ID 的文章,这适用于 Node.JS 中的 node-gcm 模块吗?

提前致谢。

【问题讨论】:

    标签: android node.js push-notification google-cloud-messaging


    【解决方案1】:

    GCM 服务器将接受最多包含 1000 个注册 ID 的请求。如果超过 1000 个,则必须将它们拆分为多个请求。

    因此,您的问题的答案取决于您调用的代码是否为您进行了这种拆分。

    【讨论】:

    • 没有说明node-gcm是否自动进行拆分,所以我得试试看。每当我测试它时,我都会更新这个答案,感谢您指出我正确的方向。
    【解决方案2】:

    node-gcm 不允许一次发送到 1,000 台以上的设备。

    请注意,您一次最多可以向 1000 个注册 ID 发送通知。这是由于 GCM API 方面的限制。

    https://github.com/ToothlessGear/node-gcm/issues/42

    您可以像这样轻松地将令牌分成批次:

    // Max devices per request    
    var batchLimit = 1000;
    
    // Batches will be added to this array
    var tokenBatches = [];
    
    // Traverse tokens and split them up into batches of 1,000 devices each  
    for (var start = 0; start < tokens.length; start += batchLimit) {
        // Get next 1,000 tokens
        var slicedTokens = tokens.slice(start, start + batchLimit);
    
        // Add to batches array
        tokenBatches.push(slicedTokens);
    }
    
    // You can now send a push to each batch of devices, in parallel, using the caolan/async library
    async.each(batches, function (batch, callback) {
        // Assuming you already set up the sender and message
        sender.send(message, { registrationIds: batch }, function (err, result) {
            // Push failed?
            if (err) {
                // Stops executing other batches
                return callback(err);
            }
    
            // Done with batch
            callback();
        });
    }, function (err) {
        // Log the error to console
        if (err) {
            console.log(err);
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2015-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多