Java後臺伺服器實現極光推送的兩種實現方式
Java後臺實現極光推送有兩種方式,一種是使用極光推送官方提供的推送請求API:https://api.jpush.cn/v3/push,另一種則是使用官方提供的第三方Java SDK,這裡先進行第一種方式推送的實現程式碼:
第一種推送方式:極光官方提供的推送請求API
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 SDK
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包。
直接在maven中的pom檔案中加入:
<dependency> <groupId>cn.jpush.api</groupId> <artifactId>jpush-client</artifactId> <version>3.2.15</version> </dependency>
注意:在這裡極有可能會出現jar包衝突:具體哪個包我也忘記了,好像是個日誌包,找到後刪除即可,我曾經記錄過這個jar包衝突問題,詳見jar包衝突解決
原本我們專案中也是採用第二種方式實現的,但是最後在提交程式碼時發現一個問題,那就是雖然我們只是帶匯入了官網提供的那個jar包,但是最後一統計,竟然無緣無故增多了80+個jar包,如此多的jar包提交過於臃腫,而且不現實,所以才臨時改變方案,採用第一種方式進行編碼。
程式碼中採用的是別名方式進行推送,需要在在手機APP端進行別名設定,最好就是在使用者登入之後就設定好,這樣只要使用者登入一次,它的繫結別名就可以儲存到極光伺服器,而我們推送時,指定這個別名,就能將資訊推送到對應使用者的手機上。
其實我們發起推送請求,只是將資訊傳送到了極光伺服器之上,這個資訊有一個儲存時限,預設一天,只要使用者使用手機APP登入系統,極光伺服器就會將資訊自動推送到對應別名的手機上,由此可見,資訊並非由我們後臺直接推送到手機,而是通過極光伺服器這個中轉站,而這正式極光的工作。
注意:這裡告知一個技巧,這個別名設定的時候,其實直接將使用者ID設定為別名即可,既方便,又安全,不用再去想辦法生成一個唯一的串來進行標識,甚至需要在後臺數據庫中使用者表中新增欄位。