离子推送通知实施

时间:2016-06-17 07:40:47

标签: angularjs ionic-framework push-notification angularjs-scope

我正在实施推送通知我的推送通知,离子平台(离子io)正常工作现在我必须取出该设备令牌并将其发送到我的服务器。
下面是我的ap.js代码:

var push = new Ionic.Push({
      "debug": true
    });

    push.register(function(token) {
      console.log("My Device token:",token.token);
      push.saveToken(token);  // persist the token in the Ionic Platform

    });

这是我的

登录信息
$scope.login = function () {
$http({
method: "post",
url: "http://200.189.253.200:8081/employee-connect/oauth/token",
data: "username="+$scope.username+"&password="+$scope.password+"&grant_type=password&scope=read write&client_secret=my-secret-token-to-change-in-production&client_id=employeeConnectapp2",
withCredentials: true,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.success(function (data){
window.localStorage.setItem("token_type", data.token_type);
window.localStorage.setItem("token", data.access_token);
$state.go('tabsController.home');
})
.error(function(data) {
var alertPopup = $ionicPopup.alert({
title: 'Login failed!',
template: 'Please check your credentials!'
});
});
}  

在成功调用中,我必须通过从控制台获取令牌来发送令牌,但不知道该怎么做。请帮助我。

1 个答案:

答案 0 :(得分:1)

安装此插件

cordova plugin add https://github.com/phonegap-build/PushPlugin.git

在.run功能内部执行此操作

          var androidConfig = {
            "senderID": "xxxxxxxx", //you should place your gcm project number
          };

          document.addEventListener("deviceready", function(){
            $cordovaPush.register(androidConfig).then(function(result) {
              // Success
            }, function(err) {
              // Error
            })

            $rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) {
              switch(notification.event) {
                case 'registered':
                  if (notification.regid.length > 0 ) {
                    alert('registration ID = ' + notification.regid);
     //here you will see the device token in alert.

                    MyService.setDeviceID(notification.regid);

//here i have used MyService to access the regiser id inside my controller
                  }
                  break;

                case 'message':
                  // this is the actual push notification. its format depends on the data model from the push server
                  alert('message = ' + notification.message + ' msgCount = ' + notification.msgcnt);
                  break;

                case 'error':
                  alert('GCM error = ' + notification.msg);
                  break;

                default:
                  alert('An unknown GCM event has occurred');
                  break;
              }
            });



          }, false);

有关获取设备ID的详细信息,请查看this

查看我已发布的答案,以获取有关GCM推送通知Ionic Push Notifications: getPushPlugin is undefined

的交易