2015-10-04 51 views
2

我刚刚与离子框架的第一次接触。然而,我之前和Phonegap和AngularJS一起工作过,所以对我来说并不是那么新鲜。 我发现这项新功能可通过Ionic Push功能(http://blog.ionic.io/announcing-ionic-push-alpha/)在Ionic中使用Google Cloud Messaging推送通知。离子推动不工作 - 用户未注册

相关的代码app.js

angular.module('starter', ['ionic','ionic.service.core', 'starter.controllers', 'starter.services']) 

.run(function($ionicPlatform) { 
    $ionicPlatform.ready(function() { 
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard 
    // for form inputs) 
    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) { 
     cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); 
     cordova.plugins.Keyboard.disableScroll(true); 

    } 
    if (window.StatusBar) { 
     // org.apache.cordova.statusbar required 
     StatusBar.styleLightContent(); 
    } 

    // enable push notifications 
    Ionic.io(); 

    // enable users (http://docs.ionic.io/docs/user-quick-start) 
    // this will give you a fresh user or the previously saved 'current user' 
    var user = Ionic.User.current(); 

    // if the user doesn't have an id, you'll need to give it one. 
    if (!user.id) { 
     user.id = Ionic.User.anonymousId(); 
     // user.id = 'your-custom-user-id'; 
    } 
    console.log('user-id:' + user.id); 

    //persist the user 
    user.save(); 

    var push = new Ionic.Push({ 
     "debug": true, 
     "onNotification": function(notification) { 
     var payload = notification.payload; 
     console.log(notification, payload); 
     }, 
     "onRegister": function(data) { 
     console.log(data.token); 
     } 
    }); 

    push.register(function(token) { 
     console.log("Device token:",token.token); 
    }); 
    push.addTokenToUser(user); 
    console.log('token added to user'); 

    }); 
}) 

日志从ionic serve

ionic $ 0  361081 log  Ionic Core:, init 
1  361083 log  Ionic Core:, searching for cordova.js 
2  361085 log  Ionic Core:, attempting to mock plugins 
3  361155 log  user-id:1cc3d21c-b687-4988-b944-ad07b1a677c8 
4  361158 log  Ionic Push:, a token must be registered before you can add it to a user. 
5  361159 log  Ionic Push:, token is not a valid Android or iOS registration id. Cannot save to user. 
6  361159 log  token added to user 
7  361160 log  Ionic Push:, register 
8  361160 error ReferenceError: PushNotification is not defined, http://localhost:8100/lib/ionic-platform-web-client/dist/ionic.io.bundle.min.js, Line: 2 
9  361526 log  Ionic User:, saved user 

任何投入是值得欢迎的线路,我也乐意在必要时提供更多信息更多。

编辑2015年10月5日:

  • 发现,dev_push = false仅适用于物理设备,而不是在浏览器
  • 我想即使注册用户
+0

是通知为你工作? –

回答

1
之前添加令牌用户

我有同样的问题,目前似乎没有很多答案在线。

但即使在真实设备上,它也不会将令牌保存到用户。

我只是不得不决定先不动,然后使用离子部署来跟进。

也我觉得你必须把这一行 push.addTokenToUser(user);

寄存器回调内按照本文档 http://docs.ionic.io/docs/push-usage

+0

安装部署的应用程序时工作有效吗?你说得对addTokenToUser,发现在离子gitter通道,安装已部署的应用程序后,推功能都是异步 – platzhersh

+0

,推注册回调触发与正确的令牌,但addTokenToUser仍然不工作,感觉像需要验证用户第一,但没有api文档指示 –

+0

有没有人发现这个问题的解决方案,我仍然得到错误消息:“离子未定义”,当试图实现离子推送通知功能。 –

0

我有这样的,它的工作原理:

 Ionic.io(); 

     var user = Ionic.User.current(); 

     if (!user.id) { 
      user.id = Ionic.User.anonymousId(); 

      // save our newly created user 
      user.save(); 
     } 

     var push = new Ionic.Push({}); 

     push.register(function (token) { 
      console.log("Got Token:", token.token); 

      // now we have token, so add it to user 
      push.addTokenToUser(user); 

      // don't forget to save user to Ionic Platform with our new token 
      user.save(); 
     }); 

     // set this user as current, so we can acess him later 
     Ionic.User.current(user); 
0

你使用这个

ionic config set dev_push true-if testing in emulator or laptop 
ionic config set dev_pushfalse - if testing on the phone 

ionic push --google-api-key Your API Key 

ionic config set gcm_key Project Number 

您的凭证登记该ID对于特定设备是唯一的。这是由android发送给你的。

电话号码(通过API密钥)--------->谷歌的GCM
谷歌GCM(承认这是您通过项目数量和API密钥)----->哦,原来是你让我记下它。 (在其数据库中保存一个令牌ID并向您发送一个令牌ID)

您将获得一个设备唯一的注册ID(如果卸载了某个应用程序,该注册ID将会更改)。 你打电话给你的服务器说嘿,这是我的你的用户。如果你有东西,请通知我。

服务器责任说,好吧,伙计,我明白了。用你的详细信息保存注册ID,可能是你的用户名在它的数据库中。


现在从服务器。

我需要通知我的用户关于很多事情(或事故或某事)。
从DB拉起所有目标的注册ID的(也许在某些条件下)

registrationIds.push(REGID)//在一个循环

and sender.send(message, registration, function(err, result){ 
}); 

发送给谷歌。谷歌看到哦,只有这些API Key中的很多人(不是全部)需要通知。没问题我会通知他们,你收到通知,我亲爱的朋友。

0

the link所述,通过执行user.addPushToken(pushToken);来添加令牌到$ionicUser

对于这个工作,你应该首先配置应用程序不使用的研究与开发推动通过,

ionic config set dev_push true

初始化Ionic.io和Ionic.push后,装入用户或创建一个具有新的随机ID,

Ionic.io(); 

    var push = new Ionic.Push(); 

    Ionic.User.load(localStorage.id).then(function (user) { 
    Ionic.User.current(user); 
    pushFactory.register(push, user); 
    }, function (error) { 
    /*the user with that id is not present , so create one with a random id and register the push */ 
    }); 

推工厂定义如下,

function pushFactory() { 

return { 
    'register': function (push, user) { 
    push.register(function (pushToken) { 
     user.addPushToken(pushToken); 
     user.save().then(function (answer) { 
     console.log('user saved'+answer); 
     }) 
    }) 
    } 
} 
} 
1

如果您想使用它,还需要在您的angular模块中声明'ionic.service.push'作为依赖项。

angular.module( '启动',[ '离子', 'ionic.service.core', 'ionic.service.push'])