首先,極光官網(wǎng)注冊賬號
需要去這里注冊https://www.jiguang.cn
注冊成功獲取AppKey
備注填寫應(yīng)用包名規(guī)范點,在項目還要用那
安裝插件
通過 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。