所以我在服务器端发送这样的节点,使用node-gcm
(https://github.com/ToothlessGear/node-gcm):
var gcm = require('node-gcm');
var message = new gcm.Message({
collapseKey: 'demo',
delayWhileIdle: true,
timeToLive: 3,
data: {
type: 'pong',
message: 'Hello Android!'
}
});
var sender = new gcm.Sender(myAPIKey);
var registrationIds = [aDoc.registrationId];
sender.send(message, registrationIds, 4, function (err, result) {
console.log(result);
});
然后在客户端,在我的BroadcastReceiver中,我得到了Logcat消息打印和接收通知,但附加内容(来自intent.getStringExtra(" data")为空。我该怎么做得到它吗?我无法在任何地方找到它。注册案例完美无缺。
@Override
protected void onHandleIntent(Intent intent) {
String action = intent.getAction();
Log.i(TAG, "GCM Received");
switch(action){
case GCM.ACTION_REGISTRATION:
String registrationId = intent.getStringExtra(GCM.KEY_REG_ID);
Log.i(TAG, "action received: registration: " + registrationId);
...
break;
case GCM.ACTION_RECEIVE:
String extras = intent.getStringExtra("data");
Log.v(TAG, "" + extras);
...
...
extras一直无效
答案 0 :(得分:1)
通过实验,我解决了我的问题。 node-gcm不是直接访问数据对象,而是直接将键值对添加到intent的extras中:
错:
// returns null
String dataJson = intent.getStringExtra("data");
右:
// returns "pong"
String type = intent.getStringExtra("type");