我正在尝试在我的Meteor应用程序中为android设备实现推送通知,我正在使用这个名为node-gcm的npm包。我已经创建了一个API项目,得到了我的项目id、API密钥和所有东西。但剩下的只有设备的注册ids了。我知道注册I是手机和应用程序的unique#。
但是我不能得到这些注册号。任何时候,任何用户唱着这个应用程序,我想点击谷歌API (或任何东西),并获得他的注册号和存储在我的数据库中。这样以后,我就可以给他发通知了。
这是我在服务器中的代码。我通过在Ubuntu 14.04的android模拟器中插入移动设备来测试它。
{{{
var gcm = Meteor.npmRequire('node-gcm');
var message = new gcm.Message({
collapseKey: 'demo',
delayWhileIdle: true,
timeToLive: 3,
data: {
key1: 'message1',
key2: 'message2'
}
});
// Set up the sender with you API key
var sender = new gcm.Sender('########################');
// Add the registration IDs of the devices you want to send to
var registrationIds = [];
registrationIds.push("DONT HAVE ANY");
// ... or retrying a specific number of times (10)
sender.send(message, registrationIds, 5, function (err, result) {
if(err) {
console.log("Notification not send");
console.error(err);
}
else{
console.log("Notification not send");
console.log(result);
}
});
}}}到目前为止,该项目正在开发中,尚未投入生产。
任何适当的建议都会得到很好的评价。谢谢
发布于 2015-02-26 15:23:59
我会注册PubNub(免费),并查看用JavaScript编写的GCM应用程序的分步教程。现在看来,检索RegistrationID的主要内容是以下插件代码调用:
var pushNotification = window.plugins.pushNotification;
pushNotification.register(successHandler, errorHandler, {
'senderID': 'your_sender_id',
'ecb': 'onNotificationGCM'
});
function errorHandler(error) {
console.log('Error: ' + error);
}
function successHandler(result) {
console.log('Success: ' + result);
}
function onNotificationGCM(e) {
switch (e.event) {
case 'registered':
if (e.regid.length > 0) {
deviceRegistered(e.regid);
}
break;
case 'message':
if (e.foreground) { // When the app is running foreground.
alert('The room temperature is set too high')
}
break;
case 'error':
console.log('Error: ' + e.msg);
break;
default:
console.log('An unknown event was received');
break;
}
}PubNub tutorial: Sending Android Push Notifications via GCM in JavaScript
https://stackoverflow.com/questions/28736064
复制相似问题