1. 程式人生 > >ionic 極光推送

ionic 極光推送

問題描述:通過後臺傳送通知給個人。
解決方法:
1 新建賬號
https://www.jiguang.cn/
通過郵箱註冊,註冊成功之後建立應用 如下:
在這裡插入圖片描述
註冊成功之後
在這裡插入圖片描述
配置Android推送
在這裡插入圖片描述
完成以上步驟之後
2 安裝外掛
cordova-plugin-jcore 1.1.11
jpush-phonegap-plugin 3.2.13
cordova pluign add [email protected]
cordova plugin add [email protected] --variable API_KEY=34c81aa4261113f51df4542f
如果安裝外掛的時候如果沒有對應版本號安裝會出現確定版本已經安裝成功,獲取不到對應的唯一標識,推送無反應等情況


3 相關對應程式碼

$scope.messagejush = "on load view success!";
    var onDeviceReady = function () {
      $timeout(function () {
        $scope.messagejush += "JPushPlugin:Device ready!";
      })
      initiateUI();
    };
    var onOpenNotification = function (event) {
      try {
        var alertContent;
        if (device.platform == "Android") {
          alertContent = window.plugins.jPushPlugin.openNotification.alert;
          alertCode = window.plugins.jPushPlugin.receiveMessage.extras.code
        } else {
          alertContent = event.aps.alert;
        }
        $timeout(function () {
          $scope.messagejush += alertContent;
        })
        //通過推送訊息新增標識來跳轉對應頁面
        alert("推送訊息:" + alertContent);
        if(alertCode=='work'){
           location.href='#/tab/work'
        }
        
      } catch (exception) {
        console.log("JPushPlugin:onOpenNotification" + exception);
      }
    };
    var onReceiveNotification = function (event) {
      try {
        var alertContent;
        if (device.platform == "Android") {
          alertContent = window.plugins.jPushPlugin.receiveNotification.alert;
        } else {
          alertContent = event.aps.alert;
        }
        $timeout(function () {
          $scope.messagejush += alertContent;
          $scope.notificationResult = alertContent;
        })
      } catch (exception) {
        console.log(exception)
      }
    };

    var onReceiveMessage = function (event) {
      try {
        var messagejush;
        if (device.platform == "Android") {
          messagejush = window.plugins.jPushPlugin.receiveMessage.messagejush;
        } else {
          messagejush = event.content;
        }
        $timeout(function () {
          $scope.messagejush += messagejush;
          $scope.messageResult = messagejush;
        })
      } catch (exception) {
        console.log("JPushPlugin:onReceiveMessage-->" + exception);
      }
    };
    // 獲取RegistrationID
    var getRegistrationID = function () {
      window.plugins.jPushPlugin.getRegistrationID(function (data) {
        try {
          if (data.length == 0) {
            var t1 = window.setTimeout(getRegistrationID, 1000);
          }
          $timeout(function () {
            $scope.messagejush+= "JPushPlugin:registrationID is " + data;
          })
          $scope.registrationID = data;
          var sessionNews = ""
          sessionNews = datadService.getObject('loginData')
          datadService.setObject('registrationID',$scope.registrationID)
        } catch (exception) {
          console.log(exception);
        }
      },function(err){
          alert(err)
      });

    };
    var initiateUI = function () {
      try {
        window.plugins.jPushPlugin.init();
        getRegistrationID();
        if (device.platform != "Android") {
          window.plugins.jPushPlugin.setDebugModeFromIos();
          window.plugins.jPushPlugin.setApplicationIconBadgeNumber(0);
        } else {
          window.plugins.jPushPlugin.setDebugMode(true);
          window.plugins.jPushPlugin.setStatisticsOpen(true);
        }
        $timeout(function () {
          $scope.messagejush += '初始化成功! \r\n';
        })
      } catch (exception) {
        console.log(exception);
      }
    }
    document.addEventListener("deviceready", onDeviceReady, false);
    document.addEventListener("jpush.openNotification", onOpenNotification, false);
    document.addEventListener("jpush.receiveNotification", onReceiveNotification, false);
    document.addEventListener("jpush.receiveMessage", onReceiveMessage, false);

4 最後別忘了依賴注入外掛