我在通过 Node 应用程序向 Google Cloud Messaging 发送消息时遇到问题。我已经创建了 API key (服务器和浏览器),我已经在服务器 key 服务器 IP 和代码中列入白名单
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);
});
我正在尝试向 Android 设备发送简单的通知。不幸的是,我收到了 401 Unauthorized 错误。我正在使用 node-curl
库,早些时候我尝试了 node-gcm
和 node-gcm-service
- 结果相同。有没有可能我错过了?
在此先感谢您的帮助, 大卫。
最佳答案
当使用node js发送推送通知时,我们可以使用下面的代码
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);
});
关于android - 在 Node 中发送 GCM 推送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21650446/