【问题标题】:ionic push notification implementation离子推送通知实现
【发布时间】:2016-06-17 07:40:47
【问题描述】:

我正在使用 ionic 平台 (ionic 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!'
});
});
}  

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

【问题讨论】:

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


    【解决方案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

    【讨论】:

    • 1>注册应用推送服务也
    • 我不懂你
    • 先生,我是第一个新手,我从 ionic docs 创建了用于推送通知的应用程序,它运行良好,但我现在的要求是我必须使用我的服务器发送通知,我该怎么做,我也很困惑
    • 关于cordova和ionic
    • 即使我在您的情况下向您发送了我的代码,您也无法理解尝试了解发送推送通知和制动该任务并一一实施所需的所有要求可以帮助你
    猜你喜欢
    • 2016-07-05
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多