cordova 集成极光推送 jpush-phonegap-plugin

1,310 阅读1分钟

准备工作

jpush-phonegap-plugin

文档

到极光官网注册app获取appkey:

安卓添加包名:

包名位置:

ios添加推送证书:

添加插件 jpush-phonegap-plugin

  • 通过 Cordova Plugins 安装,要求 Cordova CLI 5.0+:
cordova plugin add jpush-phonegap-plugin --variable APP_KEY=your_jpush_appkey
  • 或直接通过 url 安装:
cordova plugin add https://github.com/jpush/jpush-phonegap-plugin.git --variable APP_KEY=your_jpush_appkey
  • 或下载到本地安装:
cordova plugin add Your_Plugin_Path --variable APP_KEY=your_jpush_appkey

index.html 引入 cordova.js

<body>
    <div id="app"></div>
    <script src="cordova.js"></script>
</body>

main.js

document.addEventListener('deviceready', function () {
  new Vue({
    el: '#app',
    router,
    store,
    components: { App },
    template: '<App/>'
  })
  document.addEventListener('jpush.receiveNotification', function (event) {
    var alertContent;
    if (device.platform == 'Android') {
        alertContent = event.alert;
    } else {
        alertContent = event.aps.alert;
    }
    alert('open Notification:' + alertContent);
  }, false)
  initUI();
}, false);
function initUI() {
  try {
    window.JPush.init();
    window.JPush.setDebugMode(true);
    window.JPush.getRegistrationID(onGetRegistrationID);
    if (device.platform != 'Android') {
        window.JPush.setApplicationIconBadgeNumber(0);
    }
  } catch (exception) {
    console.log(exception);
    Toast('exception:' + exception);
  }
}
function onGetRegistrationID(data) {
  try {
    alert('registrationID is' + data);
    if (data.length == 0) {
      var t = window.setTimeout(getRegistrationID, 1000);
    }
  } catch (exception) {
    alert('exception:' + exception);
  }
  // window.JPush.setAlias(
  //   { sequence: 1, alias: 'ddd' },
  //   result => {
  //     var sequence = result.sequence;
  //     var alias = result.alias;
  //   },
  //   error => {
  //     var sequence = error.sequence;
  //     var errorCode = error.code;
  //   }
  // );
  // window.JPush.addTags(
  //   { sequence: 1, tags: ['test'] },
  //   result => {
  //     var sequence = result.sequence;
  //     var tags = result.tags; // 数组类型
  //   },
  //   error => {
  //     var sequence = error.sequence;
  //     var errorCode = error.code;
  //   }
  // );
  // document.addEventListener(
  //   'jpush.receiveNotification',
  //   function (event) {
  //     var alertContent;
  //     if (device.platform == 'Android') {
  //       alertContent = event.alert;
  //     } else {
  //       alertContent = event.aps.alert;
  //     }
  //     alert('open Notification:' + alertContent);
  //   },
  //   false
  // );
  // window.JPush.getUserNotificationSettings(function (result) {
  //   if (result == 0) {
  //     Toast('系统设置中已关闭应用推送');
  //   } else if (result > 0) {
  //     Toast('系统设置中打开了应用推送。');
  //   }
  // });
}

测试效果

ps: 一定要打开推送功能,我对安卓开发不太了解,一直收不到推送消息,后来才知道是我没打开app的推送功能,这个要到设置里面去找到app 手动开启通知功能