简体   繁体   中英

Node.js GCM Push Notification number of IDs allowed?

I did some searching and couldn't find the answer for this question, sorry if it's duplicated.

I'm using Node.JS to send a GCM notification to Android devices. I'm passing the list of Registration IDs in an array then sending it through the Sender.send function. I'm wondering, is there a maximum limit to the number of IDs allowed per send request? Like 1000 per call in the send function, or no such limit exists?

I recall reading about using JSON format to send to up to 1000 IDs at a time, does that apply to node-gcm module in Node.JS?

Thanks in advance.

GCM server will accept requests with up to 1000 registration ids. If you have more than 1000, you have to split them to multiple requests.

Therefore the answer to your question depends on whether the code you are calling does this splitting for you or not.

node-gcm does not permit sending to more than 1,000 devices at a time.

Notice that you can at most send notifications to 1000 registration ids at a time. This is due to a restriction on the side of the GCM API.

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

You can easily split the tokens into batches like this:

// 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);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM