標籤:java 極光推送
註:需要去官網下載 jpush-client-3.2.3.jar
/** * 極光推送工具類 * @author why * */@SuppressWarnings("all")public class JPushAllUtil {private final static String appKey = "";private final static String masterSecret = "";/** * 測試方法 */public static void main(String[] args) {//發送通知jSend_notification("0a139889879","您有一條新的物流訊息_123");}/** * 發送通知 * @param registrationId 裝置標識 * @param alert 推送內容 */public static void jSend_notification(String registrationId, String alert){JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3);PushPayload payload = send_N(registrationId, alert);try { PushResult result = jpushClient.sendPush(payload); System.out.println(result); } catch (APIConnectionException e) { System.out.println(e); } catch (APIRequestException e) { System.out.println(e); System.out.println("Error response from JPush server. Should review and fix it. "+ e); System.out.println("HTTP Status: " + e.getStatus()); System.out.println("Error Code: " + e.getErrorCode()); System.out.println("Error Message: " + e.getErrorMessage()); System.out.println("Msg ID: " + e.getMsgId()); }}public static PushPayload send_N(String registrationId, String alert){return PushPayload.newBuilder() .setPlatform(Platform.android_ios())//必填 推送平台設定 .setAudience(Audience.registrationId(registrationId)) .setNotification(Notification.alert(alert)) /** * 如果目標平台為 iOS 平台 需要在 options * 中通過 apns_production 欄位來制定推送環境。 * True 表示推送生產環境,False 表示要推送開發環境; 如 * 果不指定則為推送生產環境 */ .setOptions(Options.newBuilder() .setApnsProduction(false) .build()) .build();}}
java 極光推送