PinchProject / Node-GCMService

Node-GCMService
15 stars 10 forks source link

"registration_ids" field is not a JSON array #8

Open ffleandro opened 10 years ago

ffleandro commented 10 years ago

If I use the message.toJSON() I always get a Error: BAD_REQUEST : Request could not be parsed as JSON, or it contained invalid fields with the following details "registration_ids" field is not a JSON array.

Here is my code:

var gcm = require('node-gcm-service');
var gcmService = new gcm.Sender({
    key: 'API_KEY'
});

var message = new gcm.Message({
    delay_while_idle: "false",
    data: {
        test: 'testing'
    }
});

var registrationIds = ['reg_id'];

gcmService.sendMessage(message.toJSON(), registrationIds, true, function(err, data){
    if (!err) {
        console.log(data);
    } else {
        console.log(err);
    }
});

If I send the message using message.toString() everything goes well.

Am I doing something wrong? Do I need to declare the registrationIds array differently?

Barryrowe commented 10 years ago

I get the same thing. I don't believe the JSON message is being parsed correctly before being sent. Haven't tracked down the exact code location yet.

miopa commented 9 years ago

Edit: This will only work for less than 1000 devices, the real fix for the issue is here https://github.com/lemonlabs/node-gcm-service/commit/87484d200bfdbd87499fae17d3be52c2beacff45

This version works properly, sends to all devices with one request

Sender.prototype._sendJSONRequest = function (options, attemptOptions, message, registration_ids, callback) {
    var self = this;
    message['registration_ids'] = registration_ids;

    options['headers']['Content-Type'] = 'application/json; charset=utf-8';
    options['headers']['Content-length'] = Buffer.byteLength(JSON.stringify(message), 'utf8');

    if (!util.isArray(registration_ids)) return callback(new Error('registration_ids must be an array'));

    var multicastResult = new MulticastResult();

    options['body'] = JSON.stringify(message);
    debug('HTTP OPTIONS > %s', JSON.stringify(options));

    attempt(
        function (attempts) {
            debug('DEBUG > attempts : %s', attempts);
            self._sendRequest(options, this);
        },
        attemptOptions,
        function (err, data) {
            if (err) return callback(err);

            try {
                data = JSON.parse(data);

                multicastResult.addMulticastId(data.multicast_id);
                multicastResult.setSuccessLength(data.success);

                var results = data.results;

                for (var i = 0; i < results.length; i++) {
                    if (results[i].hasOwnProperty('registration_id')) {
                        multicastResult.addCanonicalIdObject({
                            message_id: results[i].message_id,
                            registration_id: registration_ids[i],
                            new_registration_id: results[i].registration_id
                        });
                    } else if (results[i].hasOwnProperty('error')) {
                        multicastResult.addFailureValueWithKey(results[i].error, registration_ids[i]);
                    }
                }
                callback(null, multicastResult.toJSON());
            } catch (ex) {
                callback(ex);
            }
        }
    );
};