首先是在極光官網註冊登陸帳號,而後建立推送應用,建立完應用以後,點擊打開應用,設置應用的包名,保存;css
而後回到應用主界面,看到AppKey,以及MasterSecret,這時候MasterSecret應該能夠點擊查看了。AppKey是添加插件的時候,須要用到的,而後在服務器端給移動端發送推送的時候,須要用到AppKey以及MasterSecret。html
接下來是添加插件,使用git安裝了以後,應用一直閃退,報錯找不到DataProvider,我最後是經過普通的安裝方式安裝的:java
cordova pluginadd jpush-phonegap-plugin --variable APP_KEY=your_jpush_appkeyandroid
插件github地址:https://github.com/jpush/jpush-phonegap-plugingit
插件安裝成功以後,能夠直接跑github項目中example文件夾下的代碼,就是直接把example目錄下的index.html、css以及js拷貝到項目根目錄www文件夾下,而後cordova run android,若是看到這個界面,而且已經獲取到registrationId,就表示已經成功搭建好推送環境了,這個是android示例:github
在IOS上測試的時候,安裝完插件以後,不要忘了打開IOS工程,而後Capabilities設置中打開Push Notification開關以及Background Mode開關,在Background Mode中還要勾選remote notification選項;web
最後還要設置APP_KEY,這個通常在Resource目錄下,編輯JPushConfig.plist文件,填寫AppKey和Channel,AppKey就是極光官網應用設置給的AppKey,channel就填IOS便可:spring
注意的是,測試最好使用真機。api
安裝完插件以後,在極光推送的管理界面,輸入要推送的消息,點擊發送,以後若是沒有在頁面上顯示錯誤,並且設備接受到推送消息,代表已經能夠成功接收到推送消息:服務器
打開index.html,我從裏面拿到了一些關鍵初始化代碼:
let onDeviceReady = function () { document.addEventListener("jpush.receiveRegistrationId", function (event) { console.log("receiveRegistrationId" + JSON.stringify(event)); }, false); initJPush(); }; function initJPush() { if ('JPush' in window) { console.log('initialize JPush...'); try { window.JPush.init(); window.JPush.setDebugMode(true); window.setTimeout(() => { window.JPush.getRegistrationID((data) => { console.log(data); console.log('JPush initialize successful...'); }); }, 1000); if (device.platform != "Android") { window.JPush.setApplicationIconBadgeNumber(0); } } catch (exception) { console.log(exception); } } else { console.error('JPush is not exist...'); } } document.addEventListener("deviceready", onDeviceReady, false);
而後是服務端環境搭建,首先是添加依賴:
<!--極光推送相關--> <dependency> <groupId>cn.jpush.api</groupId> <artifactId>jpush-client</artifactId> <version>3.3.3</version> </dependency> <dependency> <groupId>cn.jpush.api</groupId> <artifactId>jiguang-common</artifactId> <version>1.0.8</version> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.6.Final</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.7</version> </dependency> <!-- For log4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.7</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
而後編寫一條請求,在請求中發送推送消息:
package com.martsforever.core.template.jpush; import cn.jiguang.common.ClientConfig; import cn.jiguang.common.resp.APIConnectionException; import cn.jiguang.common.resp.APIRequestException; import cn.jpush.api.JPushClient; import cn.jpush.api.push.PushResult; import cn.jpush.api.push.model.Platform; import cn.jpush.api.push.model.PushPayload; import cn.jpush.api.push.model.audience.Audience; import cn.jpush.api.push.model.notification.Notification; import com.martsforever.core.global.RequestManage; import org.springframework.web.bind.annotation.*; import java.util.Collection; import java.util.Map; import static javax.accessibility.AccessibleRole.ALERT; @RestController @RequestMapping("push") public class JPushController { private static String MASTER_SECRET = "5a1c4d4abb80ac481a44257a"; private static String APP_KEY = "41259c975595d3c56c9e74ef"; @PostMapping("sendAll") public static Map<String, Object> sendAll(@RequestBody PushMessage pushMessage) { JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, ClientConfig.getInstance()); // For push, all you need do is to build PushPayload object. PushPayload payload = buildPushObject_all_all_alert(pushMessage.getMessage()); try { PushResult result = jpushClient.sendPush(payload); System.out.println("Got result - " + result); } catch (APIConnectionException e) { // Connection error, should retry later System.out.println("Connection error, should retry later" + e.getMessage()); } catch (APIRequestException e) { // Should review the error, and fix the request System.out.println("Should review the error, and fix the request" + e.getErrorMessage()); System.out.println("HTTP Status: " + e.getStatus()); System.out.println("Error Code: " + e.getErrorCode()); System.out.println("Error Message: " + e.getErrorMessage()); } return RequestManage.success(pushMessage); } public static PushPayload buildPushObject_all_all_alert(String msg) { return PushPayload.alertAll(msg); } //發給一個客戶端 public static PushPayload buildPushObject_all_registrationid_alert() { return PushPayload.newBuilder() .setPlatform(Platform.all()) //設置平臺-全部平臺 .setAudience(Audience.registrationId("")) //設置受衆-極光註冊id .setNotification(Notification.alert(ALERT)) //設置通知 - 消息 .build(); } //多個客戶端 public static PushPayload buildPushObject_all_registrationids_alert(Collection<String> strings) { return PushPayload.newBuilder() .setPlatform(Platform.all()) //設置平臺-全部平臺 .setAudience(Audience.registrationId(strings)) //設置受衆-極光註冊id-多個客戶端 .setNotification(Notification.alert(ALERT)) //設置通知-推送信息 .build(); } }
當請求這條請求的時候,就會把請求中的參數做爲消息發送到全部的客戶端,若是客戶端能夠接收到推送消息,證實服務器端環境搭建也完成了。我這裏不知道是否是本地調試的緣由仍是其餘緣由,經過服務器端發送推送消息有點慢,可能由於我不是付費用戶……,從發送消息到接收到推送消息,中間大概隔了一分鐘的時間,同窗們須要耐心等待一下。