Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"registration_ids" field is not a JSON array #8

Open
ffleandro opened this issue Mar 9, 2014 · 2 comments
Open

"registration_ids" field is not a JSON array #8

ffleandro opened this issue Mar 9, 2014 · 2 comments

Comments

@ffleandro
Copy link

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
Copy link

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
Copy link

miopa commented Nov 16, 2014

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants