I've got problem with sending message to Google Cloud Messaging by Node application. I've created API key (both server and browser), I've whitelisted in server key server IP 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 simple notification to Android device. Unfortunately, I get 401 Unauthorized error. I am using node-curl
library, earlier I tried node-gcm
and node-gcm-service
- both with same result. Is there any possibility that I've missed?
Thanks in advance for help, Dawid.
I think that you are using a incorrect API Key and that is the reason why you is failing. You have to take the API SERVER KEY, of your Google Cloud Console.
When sending push notification with node js we can use 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);
});
You have to make use of concatenation operator in php for appending values to keys
'Authorization: key=' . $apiKey
I had the same problem. "Error 401" occurred because I forgot to enable the API - Google Cloud Messaging for Android in Google Developers Console. After enabling it worked. Hope it helps you.