原創做品,能夠轉載,可是請標註出處地址http://www.cnblogs.com/V1haoge/p/6439313.htmlhtml
Java後臺實現極光推送有兩種方式,一種是使用極光推送官方提供的推送請求API:https://api.jpush.cn/v3/push,另外一種則是使用官方提供的第三方Java SDK,這裏先進行第一種方式推送的實現代碼:java
import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.alibaba.fastjson.JSONArray; import net.sf.json.JSONObject; import sun.misc.BASE64Encoder; /** * java後臺極光推送方式一:使用Http API * 此種方式須要自定義http請求發送客戶端:HttpClient */ @SuppressWarnings({ "deprecation", "restriction" }) public class JiguangPush { private static final Logger log = LoggerFactory.getLogger(JiguangPush.class); private String masterSecret = "xxxxxxxxxxxxxxxxxxxx"; private String appKey = "xxxxxxxxxxxxxxxxxxx"; private String pushUrl = "https://api.jpush.cn/v3/push"; private boolean apns_production = true; private int time_to_live = 86400; private static final String ALERT = "推送信息"; /** * 極光推送 */ public void jiguangPush(){ String alias = "123456";//聲明別名 try{ String result = push(pushUrl,alias,ALERT,appKey,masterSecret,apns_production,time_to_live); JSONObject resData = JSONObject.fromObject(result); if(resData.containsKey("error")){ log.info("針對別名爲" + alias + "的信息推送失敗!"); JSONObject error = JSONObject.fromObject(resData.get("error")); log.info("錯誤信息爲:" + error.get("message").toString()); } log.info("針對別名爲" + alias + "的信息推送成功!"); }catch(Exception e){ log.error("針對別名爲" + alias + "的信息推送失敗!",e); } } /** * 組裝極光推送專用json串 * @param alias * @param alert * @return json */ public static JSONObject generateJson(String alias,String alert,boolean apns_production,int time_to_live){ JSONObject json = new JSONObject(); JSONArray platform = new JSONArray();//平臺 platform.add("android"); platform.add("ios"); JSONObject audience = new JSONObject();//推送目標 JSONArray alias1 = new JSONArray(); alias1.add(alias); audience.put("alias", alias1); JSONObject notification = new JSONObject();//通知內容 JSONObject android = new JSONObject();//android通知內容 android.put("alert", alert); android.put("builder_id", 1); JSONObject android_extras = new JSONObject();//android額外參數 android_extras.put("type", "infomation"); android.put("extras", android_extras); JSONObject ios = new JSONObject();//ios通知內容 ios.put("alert", alert); ios.put("sound", "default"); ios.put("badge", "+1"); JSONObject ios_extras = new JSONObject();//ios額外參數 ios_extras.put("type", "infomation"); ios.put("extras", ios_extras); notification.put("android", android); notification.put("ios", ios); JSONObject options = new JSONObject();//設置參數 options.put("time_to_live", Integer.valueOf(time_to_live)); options.put("apns_production", apns_production); json.put("platform", platform); json.put("audience", audience); json.put("notification", notification); json.put("options", options); return json; } /** * 推送方法-調用極光API * @param reqUrl * @param alias * @param alert * @return result */ public static String push(String reqUrl,String alias,String alert,String appKey,String masterSecret,boolean apns_production,int time_to_live){ String base64_auth_string = encryptBASE64(appKey + ":" + masterSecret); String authorization = "Basic " + base64_auth_string; return sendPostRequest(reqUrl,generateJson(alias,alert,apns_production,time_to_live).toString(),"UTF-8",authorization); } /** * 發送Post請求(json格式) * @param reqURL * @param data * @param encodeCharset * @param authorization * @return result */ @SuppressWarnings({ "resource" }) public static String sendPostRequest(String reqURL, String data, String encodeCharset,String authorization){ HttpPost httpPost = new HttpPost(reqURL); HttpClient client = new DefaultHttpClient(); HttpResponse response = null; String result = ""; try { StringEntity entity = new StringEntity(data, encodeCharset); entity.setContentType("application/json"); httpPost.setEntity(entity); httpPost.setHeader("Authorization",authorization.trim()); response = client.execute(httpPost); result = EntityUtils.toString(response.getEntity(), encodeCharset); } catch (Exception e) { log.error("請求通訊[" + reqURL + "]時偶遇異常,堆棧軌跡以下", e); }finally{ client.getConnectionManager().shutdown(); } return result; } /**
* BASE64加密工具
*/ public static String encryptBASE64(String str) { byte[] key = str.getBytes(); BASE64Encoder base64Encoder = new BASE64Encoder(); String strs = base64Encoder.encodeBuffer(key); return strs; } }
以上代碼中使用的是第一種方式實現推送,下面介紹第二種方式:使用java SDKandroid
import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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.Options; 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.AndroidNotification; import cn.jpush.api.push.model.notification.IosNotification; import cn.jpush.api.push.model.notification.Notification; /** * java後臺極光推送方式二:使用Java SDK */ @SuppressWarnings({ "deprecation", "restriction" }) public class JiguangPush { private static final Logger log = LoggerFactory.getLogger(JiguangPush.class); private static String masterSecret = "xxxxxxxxxxxxxxxxx"; private static String appKey = "xxxxxxxxxxxxxxxx"; private static final String ALERT = "推送信息"; /** * 極光推送 */ public void jiguangPush(){ String alias = "123456";//聲明別名 log.info("對別名" + alias + "的用戶推送信息"); PushResult result = push(String.valueOf(alias),ALERT); if(result != null && result.isResultOK()){ log.info("針對別名" + alias + "的信息推送成功!"); }else{ log.info("針對別名" + alias + "的信息推送失敗!"); } } /** * 生成極光推送對象PushPayload(採用java SDK) * @param alias * @param alert * @return PushPayload */ public static PushPayload buildPushObject_android_ios_alias_alert(String alias,String alert){ return PushPayload.newBuilder() .setPlatform(Platform.android_ios()) .setAudience(Audience.alias(alias)) .setNotification(Notification.newBuilder() .addPlatformNotification(AndroidNotification.newBuilder() .addExtra("type", "infomation") .setAlert(alert) .build()) .addPlatformNotification(IosNotification.newBuilder() .addExtra("type", "infomation") .setAlert(alert) .build()) .build()) .setOptions(Options.newBuilder() .setApnsProduction(false)//true-推送生產環境 false-推送開發環境(測試使用參數) .setTimeToLive(90)//消息在JPush服務器的失效時間(測試使用參數) .build()) .build(); } /** * 極光推送方法(採用java SDK) * @param alias * @param alert * @return PushResult */ public static PushResult push(String alias,String alert){ ClientConfig clientConfig = ClientConfig.getInstance(); JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, clientConfig); PushPayload payload = buildPushObject_android_ios_alias_alert(alias,alert); try { return jpushClient.sendPush(payload); } catch (APIConnectionException e) { log.error("Connection error. Should retry later. ", e); return null; } catch (APIRequestException e) { log.error("Error response from JPush server. Should review and fix it. ", e); log.info("HTTP Status: " + e.getStatus()); log.info("Error Code: " + e.getErrorCode()); log.info("Error Message: " + e.getErrorMessage()); log.info("Msg ID: " + e.getMsgId()); return null; } } }
能夠看出使用Java SDK實現推送的方式很簡單,代碼量也少,理解起來也不難,官方提供的SDK中將不少內容都實現了,咱們只是須要配置一下信息,而後發起推送便可。須要注意的是使用第二種方式,須要導入極光官網提供的jar包。ios
直接在maven中的pom文件中加入:數據庫
<dependency> <groupId>cn.jpush.api</groupId> <artifactId>jpush-client</artifactId> <version>3.2.15</version> </dependency>
注意:在這裏極有可能會出現jar包衝突:具體哪一個包我也忘記了,好像是個日誌包,找到後刪除便可,我曾經記錄過這個jar包衝突問題,詳見jar包衝突解決。apache
本來咱們項目中也是採用第二種方式實現的,可是最後在提交代碼時發現一個問題,那就是雖然咱們只是帶導入了官網提供的那個jar包,可是最後一統計,居然平白無故增多了80+個jar包,如此多的jar包提交過於臃腫,並且不現實,因此才臨時改變方案,採用第一種方式進行編碼。json
代碼中採用的是別名方式進行推送,須要在在手機APP端進行別名設置,最好就是在用戶登陸以後就設置好,這樣只要用戶登陸一次,它的綁定別名就能夠保存到極光服務器,而咱們推送時,指定這個別名,就能將信息推送到對應用戶的手機上。api
其實咱們發起推送請求,只是將信息發送到了極光服務器之上,這個信息有一個保存時限,默認一天,只要用戶使用手機APP登陸系統,極光服務器就會將信息自動推送到對應別名的手機上,因而可知,信息並不是由咱們後臺直接推送到手機,而是經過極光服務器這個中轉站,而這正式極光的工做。安全
注意:這裏告知一個技巧,這個別名設置的時候,其實直接將用戶ID設置爲別名便可,既方便,又安全,不用再去想辦法生成一個惟一的串來進行標識,甚至須要在後臺數據庫中用戶表中新增字段。服務器