正在获取GCM流星应用程序的注册ID

Fetching registration IDs for GCM meteor app

本文关键字:注册 ID 应用程序 流星 获取 GCM      更新时间:2024-06-03

我正在尝试在android设备的Meteor应用程序中实现推送通知。我正在使用这个名为node-gcm的npm包。我已经创建了一个API项目,得到了我的项目id,API密钥和所有的东西。但剩下的只有设备的注册ID。我知道注册ID是手机和应用程序的唯一#。

但我无法获得这些注册号。任何时候,任何用户都会唱出这个应用程序,我想点击谷歌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);
    }    
  });
}}}

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

任何适当的建议都将不胜感激。感谢

我会在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教程:在JavaScript 中通过GCM发送Android推送通知