一.簡介:java
極光推送(JPush)是獨立的第三方雲推送平臺,致力於爲全球移動應用開發者提供專業、高效的移動消息推送服務。android
極光推送,英文簡稱 JPush,是一個面向普通開發者開放的,免費的第三方消息推送服務。git
二.官網:https://www.jiguang.cn/github
首先註冊一個帳號,登錄平臺,在控制檯添加咱們的應用信息express
點擊提交apache
會成一個AppKey和Master Secret這兩個密鑰主要用來配置服務端發送通知使用。api
三.開整app
官方的栗子:https://github.com/jpush/jpush-phonegap-pluginionic
1.首先建立一個ionic項目jpushDemoide
ionic start -a jpushDemo -i com.kangnuo.jpushDemo jpushDemo blank
ionic start -a 應用名 -i 包名 文件夾名 blank
ionic platform add android
ionic plugin add cordova-plugin-device
<dependency id=」org.apache.cordova.device」 url=」https://github.com/apache/cordova-plugin-device.git」 />
angular.module(‘starter’, [‘ionic’,’ui.router’]) .run(function($ionicPlatform) { $ionicPlatform.ready(function () { // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard // for form inputs) if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) { cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); cordova.plugins.Keyboard.disableScroll(true) } if (window.StatusBar) { StatusBar.styleDefault(); } /** * 極光推送業務開始 */ //啓動極光推送服務 window.plugins.jPushPlugin.init(); //設置顯示最新得條數 window.plugins.jPushPlugin.setLatestNotificationNum(5); //調試模式 if (device.platform == "Android") { window.plugins.jPushPlugin.setDebugMode(true); window.plugins.jPushPlugin.setStatisticsOpen(true); }else { window.plugins.jPushPlugin.setDebugModeFromIos(); window.plugins.jPushPlugin.setApplicationIconBadgeNumber(0); } //獲取RegistrationID getRegistrationID(); //設置別名 window.plugins.jPushPlugin.setAlias("bbbb"); if (window.localStorage.getItem("platformType") == "WEIXIN" && window.localStorage.getItem("RegistrationID")){ jPushInfo(window.localStorage.getItem("key"),window.localStorage.getItem("RegistrationID")); window.plugins.jPushPlugin.setAlias(window.localStorage.getItem("RegistrationID")); } if (device.platform == "Android") { //ANDROID接收消息 window.plugins.jPushPlugin.receiveNotificationInAndroidCallback = function (data) { //expresstion }; //ANDROID打開消息 window.plugins.jPushPlugin.openNotificationInAndroidCallback = function(data){ //expresstion }; }else { //IOS接收消息(APP在前臺) document.addEventListener("jpush.receiveNotification", onReceiveNotification, false); //IOS打開消息 document.addEventListener("jpush.openNotification", onOpenNotification, false); //IOS接收消息(APP在後臺) document.addEventListener("jpush.backgoundNotification", onBackgroundNotification, false); } /** * 極光推送業務結束 */ }); var onReceiveNotification = function(event) { //expresstion }; var onBackgroundNotification = function (event) { //expresstion }; var onOpenNotification = function(event) { //expresstion }; var getRegistrationID = function() { window.plugins.jPushPlugin.getRegistrationID(onGetRegistrationID); }; var onGetRegistrationID = function(data) { try { console.log("JPushPlugin:registrationID is " + data); if (data.length == 0) { window.setTimeout(getRegistrationID, 1000); }else { window.localStorage.setItem("RegistrationID",data); } } catch (exception) { console.log(exception); } }; var isReceiveFunc = function (pushId) { //expresstion }; var isOpenFunc = function (pushId) { //expresstion }; var jPushInfo = function (key,RegistrationID) { //expresstion }; });
import cn.jpush.android.api.JPushInterface; @Override protected void onResume() { super.onResume(); JPushInterface.onResume(this); } @Override protected void onPause() { super.onPause(); JPushInterface.onPause(this); }
4.調試
jPush插件在使用過程當中會用到手機接收通知,以及用戶打開手機通知等事件,這些jPush都已有現成的方法(receiveMessageInAndroidCallback,openNotificationInAndroidCallback ==)只要在這些回調方法中添加本身的業務邏輯代碼就能夠,調試過程當中SDK使用logCat來打印APP運行狀態。並且jPush在接受自定義消息時是不會顯示在手機通知欄中,須要經過log來進行調試,ionic APP中可使用CLI方式運行來調試。
ionic cli 使用說明: http://ionicframework.com/docs/v2/cli/run/
命令:ionic run android -l -i
注:在使用中發現 app安裝到手機上打開一直提示 application error,network error ,須要添加白名單插件 ,運行
ionic plugin add cordova-plugin-whitelist
而後就能夠在terminal看到手機app運行時log信息了。。
5.服務端推送java實現方式
首先在項目中添加依賴包,打開pom.xml添加
<dependency> <groupId>cn.jpush.api</groupId> <artifactId>jpush-client</artifactId> <version>3.2.9</version> </dependency>
推送實現方法(官方文檔很全)
JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3); // For push, all you need do is to build PushPayload object. PushPayload payload = buildPushObject_all_all_alert(); try { PushResult result = jpushClient.sendPush(payload); LOG.info("Got result - " + result); } catch (APIConnectionException e) { // Connection error, should retry later LOG.error("Connection error, should retry later", e); } catch (APIRequestException e) { // Should review the error, and fix the request LOG.error("Should review the error, and fix the request", e); LOG.info("HTTP Status: " + e.getStatus()); LOG.info("Error Code: " + e.getErrorCode()); LOG.info("Error Message: " + e.getErrorMessage()); } //進行推送的關鍵在於構建一個 PushPayload 對象。如下示例通常的構建對象的用法。 //快捷地構建推送對象:全部平臺,全部設備,內容爲 ALERT 的通知。 public static PushPayload buildPushObject_all_all_alert() { return PushPayload.alertAll(ALERT); }
6.完成