Skip to content

"registration_ids" field is not a JSON array #8

Description

@ffleandro

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?

Activity

  1. Barryrowe commented on Apr 25, 2014

    @Barryrowe

    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.

  2. miopa commented on Nov 16, 2014

    @miopa

    Edit: This will only work for less than 1000 devices, the real fix for the issue is here lemonlabs@87484d2

    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);
                }
            }
        );
    };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions