Cordova极光推送相关配置

准备材料

  • 首先去极光官网注册账号并创建应用
  • 下载Cordova推送插件并按照说明进行添加插件配置
  • iOS推送证书(开发环境和生产环境)
  • iOS打包证书和描述文件
  • 在plugin.xml和JPushConfig.plist文件中配置相应的App_key和包名

代码说明

创建自定义service

angular.module('JPushServiceModule', [])
  .factory('jPushService', function ($window, publicMethod) {
    var jpushServiceFactory = {};

    //启动极光推送
    var _init = function (config) {
      $window.plugins.jPushPlugin.init();
      /*
       * 设置tag和Alias触发事件处理
       * */
      document.addEventListener('jpush.setAlias', config.stac, false);
      document.addEventListener("jpush.openNotification", onOpenNotification, false);
      /*
       * 打开推送消息事件处理
       * */
      $window.plugins.jPushPlugin.openNotificationInAndroidCallback = config.oniac;
      $window.plugins.jPushPlugin.receiveNotificationIniOSCallback = config.onios;
      $window.plugins.jPushPlugin.setDebugMode(true);
    };
    var onOpenNotification = function (event) {
      try {
        publicMethod.goView('my-message');
      } catch (exception) {
      }
    };
    /*
     * 获取状态
     * */
    var _isPushStopped = function (fun) {
      $window.plugins.jPushPlugin.isPushStopped(fun);
    };
    /*
     * 停止极光推送
     * */
    var _stopPush = function () {
      $window.plugins.jPushPlugin.stopPush();
    };
    /*
     * 重启极光推送
     * */
    var _resumePush = function () {
      $window.plugins.jPushPlugin.resumePush();
    };
    /*
     * 设置标签和别名
     * */
    var _setTagsWithAlias = function (tags, alias) {
      $window.plugins.jPushPlugin.setTagsWithAlias(tags, alias);
    };
    /*
     * 设置标签
     * */
    var _setTags = function (tags) {
      $window.plugins.jPushPlugin.setTags(tags);
    };
    /*
     * 设置别名
     * */
    var _setAlias = function (alias) {
      $window.plugins.jPushPlugin.setAlias(alias);
    };
    /*
     * 清除角标
     * */
    var _resetBadge = function () {
      $window.plugins.jPushPlugin.resetBadge();
    };

    jpushServiceFactory.init = _init;
    jpushServiceFactory.isPushStopped = _isPushStopped;
    jpushServiceFactory.stopPush = _stopPush;
    jpushServiceFactory.resumePush = _resumePush;
    jpushServiceFactory.setTagsWithAlias = _setTagsWithAlias;
    jpushServiceFactory.setTags = _setTags;
    jpushServiceFactory.setAlias = _setAlias;
    jpushServiceFactory.resetBadge = _resetBadge;

    return jpushServiceFactory;
  });

在$ionicPlatform.ready中进行代码初始化

$ionicPlatform.ready(function () {
  if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
    cordova.plugins.Keyboard.hideKeyboardAccessoryBar(false);
    cordova.plugins.Keyboard.disableScroll(true);
  }
  if (window.StatusBar) {
    StatusBar.styleDefault();
  }
  var setAliasCallback = function (event) {
    console.log('alias' + event.alias);
  };
  //android的成功回调,ios的查看发现并没有找到回调方法
  var openNotificationInAndroidCallback = function (data) {
    var json = data;
    if (typeof data === 'string') {
      json = JSON.parse(data);
    }
    $state.go('my-message');
  };
  var config = {
    stac: setAliasCallback,
    oniac: openNotificationInAndroidCallback,
    onios: receiveNotificationIniOSCallback
  };
  window.plugins.jPushPlugin.setAlias(StorageService.getStorage('userId').replace(reg, ''));
  window.plugins.jPushPlugin.setBadge(0);//设置角标数值
  jPushService.init(config);
});

说明:Android可以通过设置openNotificationInAndroidCallback(官方提供的方法)但是iOS并没有找到,所以通过jpush.openNotification这个事件实现的,它的意思就是点击通知进入应用程序时会触发该事件(写在service里面,Android和ios都可以使用)

你可能感兴趣的:(Cordova极光推送相关配置)