我是node-gcm
的新人,
我尝试将数据发送到android使用node-gcm
...我不知道我在哪里开始构建项目..我遵循本教程https://github.com/ToothlessGear/node-gcm但仍然困惑......
这里是我的代码:
var gcm = require('node-gcm');
// create a message with default values
var message = new gcm.Message();
// or with object values
var message = new gcm.Message({
collapseKey: 'demo',
delayWhileIdle: true,
timeToLive: 3,
data: {
key1: 'message1'
}
});
var sender = new gcm.Sender('myApi');
var registrationIds = [];
// Optional
// add new key-value in data object
message.addDataWithKeyValue('key1','message1');
// or add a data object
message.addDataWithObject({
key1: 'message1',
});
// or with backwards compability of previous versions
message.addData('key1','testdarinodegcm');
message.collapseKey = 'demo';
message.delayWhileIdle = true;
message.timeToLive = 3;
// END Optional
// At least one required
registrationIds.push('myToken');
//registrationIds.push('regId2');
/**
* Parameters: message-literal, registrationIds-array, No. of retries, callback-function
*/
sender.send(message, registrationIds, 4, function (err, result) {
console.log(result);
});
我在控制台中找不到错误...相反成功但我无法在Android设备中接收数据...这里来自控制台的消息......
{"multicast_id":7521418564872032002,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1367890688015497%f11e78b0f9fd7ecd"}]}
为什么我无法从Android设备中的node-gcm
接收数据?也许我的代码不完整?
答案 0 :(得分:7)
试试这个..
var sender = new gcm.Sender(googleApiKey);
var message = new gcm.Message();
message.addData('key1','testdarinodegcm');
message.delay_while_idle = 1;
var registrationIds = [];
registrationIds.push('APA91bHCzBP6wEfExCZlSXgf0wuOC6QEWJ-7MVFl3hgaW3Jv8FslsofGJ- qgdliyS8zjwy_W7zPaKsEZx6kbeuWdoAAexawKl1Qd6GEGV_v844n1LMRaTsWeiwI9iaLGKKq_R3scY_wuRG8uG2SZ5X9q0J67Ko3gcw');
sender.send(message, registrationIds, 4, function (err, result) {
console.log(result);
});
本教程介绍了库为您提供的所有可用选项。这并不意味着您需要根据具体情况立即需要所有这些。尝试使用此代码,它会向您的设备发送简单信息。