首先,极光官网注册账号
需要去这里注册https://www.jiguang.cn
注册成功获取AppKey
备注填写应用包名规范点,在项目还要用那
安装插件
通过 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
app.js
$ionicPlatform.ready(function() {
//初始化
window.plugins.jPushPlugin.init();
if(ionic.Platform.isIOS()) {
window.plugins.jPushPlugin.setDebugModeFromIos();
window.plugins.jPushPlugin.setApplicationIconBadgeNumber(0);
window.plugins.jPushPlugin.resetBadge();
$ionicPlatform.on('resume',function() {
window.plugins.jPushPlugin.setApplicationIconBadgeNumber(0);
window.plugins.jPushPlugin.resetBadge();
})
}else if(ionic.Platform.isAndroid()) {
window.plugins.jPushPlugin.setDebugMode(true);
window.plugins.jPushPlugin.setStatisticsOpen(true);
}
document.addEventListener("jpush.openNotification",onOpenNotification,false);
document.addEventListener("jpush.receiveMessage",onReceiveMessage,false);
document.addEventListener("jpush.receiveNotification",onReceiveNotification,false);
})
//$ionicPlatform.on('resume',function() {})中是手机进入前台后清除角标,resetBadge可以解决ios上面角标累加后不清零的问题。
//如果不添加$ionicPlatform.on('resume',function() {})中的方法,也可以修改插件AppDelegate+JPush.m,
将上图所示的方法中解除注释,并在applicationDidEnterBackground方法中添加[JPUSHService resetBadge];
最后
var onOpenNotification=function(event) {
if (ionic.Platform.isAndroid()) {
for(var key in event.extras){
alert(key+':'+event.extras[key]);
}
} else if (ionic.Platform.isIOS()) {
for(var key in event.aps){
alert(key+':'+event.aps[key]);
}
}
};
var onReceiveMessage=function(event) {
//alert('onReceiveMessage');
}
var onReceiveNotification=function() {
//alert('onReceiveNotification');
}
参考demo:https://github.com/jpush/jpush-phonegap-plugin/blob/master/example/index.html
插件地址:https://github.com/jpush/jpush-phonegap-plugin
更多功能查看官方demo。
网友评论