2015-10-04 52 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'])