如何使用GCM为Ionic + iOS应用程序获取推送通知?

时间:2016-04-26 05:31:16

标签: ios ionic-framework push-notification apple-push-notifications

我是离子框架的新手。在我的iOS应用程序中,我需要从远程服务器接收推送通知。我遵循了几种方法但没有正常工作。没有关于iOS推送通知的详细教程。登录我的应用程序时,我将设备令牌传递给服务器。当他们在后端发送通知时,回复他们得到的结果{success:0; “invalid_registration id”}。我无法弄清楚为什么我无法收到通知。我添加了我在我的应用中使用的完整代码。

的index.html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="styles/style.css" rel="stylesheet">

    <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
    <link href="css/ionic.app.css" rel="stylesheet">
    -->
<!--[if IE]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>
      <script src="bower_components/angular/angular.js"></script>
    <script src="bower_components/angular-animate/angular-animate.js"></script>
    <script src="bower_components/angular-sanitize/angular-sanitize.js"></script>
    <script src="bower_components/angular-ui-router/release/angular-ui-router.js"></script>
    <script src="bower_components/ionic/release/js/ionic.js"></script>
    <script src="bower_components/ionic/release/js/ionic-angular.js"></script>
    <script src="bower_components/ngCordova/dist/ng-cordova.js"></script>

    <!-- cordova script (this will be a 404 during development) -->
    <script src="cordova.js"></script>

    <!-- your app's js -->
    <script src="scripts/app.js"></script>
    <script src="scripts/controllers.js"></script>
     <title>Freshey's</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="msapplication-tap-highlight" content="no">
        <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
        <meta name="apple-mobile-web-app-capable" content="yes"/>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
        <link href="css/style.css" type="text/css" rel="stylesheet"/>
        <link href="css/bootstrap.css" type="text/css" rel="stylesheet"/>
        <link href="css/font-awesome.css" type="text/css" rel="stylesheet"/>
                <script type="text/javascript" charset="utf-8" src="js/PushNotification.js"></script>

        <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700,800,300' rel='stylesheet' type='text/css'>
        <link href='https://fonts.googleapis.com/css?family=Fira+Sans:400,700,400italic,300,500' rel='stylesheet' type='text/css'>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
            <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?&sensor=false"></script>

        <script src="js/jquery-1.9.1.min.js"></script>
        <script src="js/bootstrap.js"></script>
        <meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'"/>

    <!-- endbuild -->  
  </head>

  <body ng-app="starter">
    <ion-nav-view></ion-nav-view>
  </body>
</html>

Controllers.js:

.controller('LoginCtrl', function($scope, $cordovaStatusbar,$state,$http,$ionicLoading, $ionicPopup,httpService, $cordovaPush, $cordovaDevice,$rootScope) {
 var iosConfig = {
   "badge": true,
    "sound": true,
    "alert": true,

  };
  document.addEventListener("deviceready", function(){
   $cordovaPush.register(iosConfig).then(function(deviceToken) {
 // Success -- send deviceToken to server, and store for future use
       console.log("deviceToken: " + deviceToken)
       window.localStorage.setItem('regid',deviceToken);
     }, function(err) {
      alert("Registration error: " + err)
    });  

 $rootScope.$on(pushNotificationReceived, function(event, notification) {
                   alert("1");
      if (notification.alert) {
        navigator.notification.alert(notification.alert);
      }

      if (notification.sound) {
        var snd = new Media(event.sound);
        snd.play();
      }

      if (notification.badge) {
        $cordovaPush.setBadgeNumber(notification.badge).then(function(result) {
          // Success!
        }, function(err) {
          // An error occurred. Show a message to the user
        });
      }
    });

    // WARNING! dangerous to unregister (results in loss of tokenID)
    $cordovaPush.unregister(options).then(function(result) {
      // Success!
    }, function(err) {
      // Error
    });

   }, false);
$scope.data = {};
       $scope.login = function() {       
       var baseUrl = "http://mywebworks/login.php";
       var param = {"phone" : $scope.data.phone,"reg_id" : window.localStorage.getItem('regid') };   
alert("session"+window.localStorage.getItem('regid'));

$ionicLoading.show({
 template: '<ion-spinner icon="android"></ion-spinner>'+
            '<p>Loading...</p>'
        });
});

0 个答案:

没有答案