获取GCM meteor应用程序的注册ID

时间:2015-02-26 06:42:09

标签: javascript android node.js meteor google-cloud-messaging

我正在尝试在Android设备的Meteor应用程序中实现推送通知。我正在使用名为node-gcm的这个npm包。 我创建了一个API项目,得到了我的项目ID,API密钥和所有内容。但唯一剩下的就是设备的注册ID。我知道注册ID是移动和应用程序的唯一#。

但我无法获得这些注册号码。每当有任何用户唱出应用时,我想点击Google 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);
    }    
  });

}}}

到目前为止,该项目正在开发中,尚未投入生产。

任何适当的建议都很受欢迎。 感谢

1 个答案:

答案 0 :(得分:-1)

我会在PubNub(免费)注册,并查看用JavaScript编写的GCM应用程序的分步教程。现在看来检索RegistrationID [s]的主要内容是以下插件代码调用:

  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

相关问题