首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >正在获取GCM meteor应用的注册ID

正在获取GCM meteor应用的注册ID
EN

Stack Overflow用户
提问于 2015-02-26 14:42:09
回答 1查看 578关注 0票数 0

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

但是我不能得到这些注册号。任何时候,任何用户唱着这个应用程序,我想点击谷歌API (或任何东西),并获得他的注册号和存储在我的数据库中。这样以后,我就可以给他发通知了。

这是我在服务器中的代码。我通过在Ubuntu 14.04的android模拟器中插入移动设备来测试它。

代码语言:javascript
复制
{{{

  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);
    }    
  });

}}}

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

任何适当的建议都会得到很好的评价。谢谢

EN

回答 1

Stack Overflow用户

发布于 2015-02-26 15:23:59

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

代码语言:javascript
复制
  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

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28736064

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档