Submitting GCM with Node

I'm having trouble sending a message to Google Cloud Messaging using the Node app. I created an API key (both server and browser), I am included in the white server of the IP server and by code

        var headers = [
            'Content-Type: application/json',
            'Authorization: key=' + apiKey
        ];

        curl("https://android.googleapis.com/gcm/send", {
            HTTPHEADER: headers,
            POST: true,
            POSTFIELDS: JSON.stringify({
                data: {
                    type: 'xxx',
                    city: 'xxx',
                    url: 'xxx'
                },
                registration_ids: [phoneId]
            })
        }, function () {
            console.log(arguments);
        });

I am trying to send a simple notification to an Android device. Sorry, I got 401 Unauthorized error . I use the library node-curl, I tried before node-gcmand node-gcm-service- both with the same result. Is there a chance that I missed?

Thanks in advance for your help, Dawid.

+3
source share
4 answers

When sending push notifications using node js, we can use the following code

var gcm = require('node-gcm'),
    gcm_message = new gcm.Message(),
    sender = new gcm.Sender(GCM_API_KEY),
    RETRY_COUNT = 4;

gcm_message.addDataWithKeyValue('key1', message);
gcm_message.delayWhileIdle = true;

sender.send(gcm_message, registrationIds, RETRY_COUNT, function(err, result) {
    callback(err, result);
});
+2
source

, API, . API SERVER KEY, Google Cloud Console.

+1

You need to use the concatenation operator in php to add values ​​to keys

'Authorization: key=' . $apiKey
0
source

I had the same problem. "Error 401" occurred because I forgot to enable the Google Cloud Messaging for Android API in the Google Developers Console . After turning on his work. Hope this helps you.

0
source

All Articles