package com.ifish.jpush.examples; import java.util.HashMap; import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.gson.JsonObject; import com.ifish.jpush.JPushClient; import com.ifish.jpush.common.ClientConfig; import com.ifish.jpush.common.resp.APIConnectionException; import com.ifish.jpush.common.resp.APIRequestException; import com.ifish.jpush.push.PushResult; import com.ifish.jpush.push.model.Message; import com.ifish.jpush.push.model.Options; import com.ifish.jpush.push.model.Platform; import com.ifish.jpush.push.model.PushPayload; import com.ifish.jpush.push.model.audience.Audience; import com.ifish.jpush.push.model.audience.AudienceTarget; import com.ifish.jpush.push.model.notification.AndroidNotification; import com.ifish.jpush.push.model.notification.IosNotification; import com.ifish.jpush.push.model.notification.Notification; public class PushExample { protected static final Logger LOG = LoggerFactory.getLogger(PushExample.class); // demo App defined in resources/jpush-api.conf private static final String appKey ="d970d5e193cb2a0bbe41653c"; private static final String masterSecret = "60162c8cf195ce9f4dc76629"; public static final String TITLE = "Test from API example"; public static final String ALERT = "Test from API Example - alert"; public static final String MSG_CONTENT = "Test from API Example - msgContent"; public static final String REGISTRATION_ID = "0900e8d85ef"; public static final String TAG = "tag_api"; public static void main(String[] args) { //testSendPushWithCustomConfig(); Map map = new HashMap(); map.put("device_id", "1"); map.put("device_name", "设备1"); map.put("msg_type", "remove_device"); testSendIosAlert("300","设备解除绑定", "你已失去对“设备1”设备的控制权", map); //testSendAndroidNotification("40","标题401","内容40",map); //testSendAndroidNotification("40","标题","内容"); } public static void testSendPush() { // HttpProxy proxy = new HttpProxy("localhost", 3128); // Can use this https proxy: https://github.com/Exa-Networks/exaproxy 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) { LOG.error("Connection error. Should retry later. ", e); } 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()); } } public static PushPayload buildPushObject_all_all_alert() { return PushPayload.alertAll(ALERT); } public static PushPayload buildPushObject_all_alias_alert() { return PushPayload.newBuilder() .setPlatform(Platform.all()) .setAudience(Audience.alias("alias1")) .setNotification(Notification.alert(ALERT)) .build(); } public static PushPayload buildPushObject_android_tag_alertWithTitle() { return PushPayload.newBuilder() .setPlatform(Platform.android()) .setAudience(Audience.tag("tag1")) .setNotification(Notification.android(ALERT, TITLE, null)) .build(); } public static PushPayload buildPushObject_android_and_ios() { return PushPayload.newBuilder() .setPlatform(Platform.android_ios()) .setAudience(Audience.tag("tag1")) .setNotification(Notification.newBuilder() .setAlert("alert content") .addPlatformNotification(AndroidNotification.newBuilder() .setTitle("Android Title").build()) .addPlatformNotification(IosNotification.newBuilder() .incrBadge(1) .addExtra("extra_key", "extra_value").build()) .build()) .build(); } public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage() { return PushPayload.newBuilder() .setPlatform(Platform.ios()) .setAudience(Audience.tag_and("tag1", "tag_all")) .setNotification(Notification.newBuilder() .addPlatformNotification(IosNotification.newBuilder() .setAlert(ALERT) .setBadge(5) .setSound("happy") .addExtra("from", "JPush") .build()) .build()) .setMessage(Message.content(MSG_CONTENT)) .setOptions(Options.newBuilder() .setApnsProduction(true) .build()) .build(); } public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras() { return PushPayload.newBuilder() .setPlatform(Platform.android_ios()) .setAudience(Audience.newBuilder() .addAudienceTarget(AudienceTarget.tag("tag1", "tag2")) .addAudienceTarget(AudienceTarget.alias("alias1", "alias2")) .build()) .setMessage(Message.newBuilder() .setMsgContent(MSG_CONTENT) .addExtra("from", "JPush") .build()) .build(); } public static void testSendPushWithCustomConfig() { ClientConfig config = ClientConfig.getInstance(); // Setup the custom hostname config.setPushHostName("https://api.jpush.cn"); JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3, null, config); // 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) { LOG.error("Connection error. Should retry later. ", e); } 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()); } } public static void testSendIosAlert(String alias,String title,String body,Map extras) { JPushClient jpushClient = new JPushClient(masterSecret, appKey, true ,86400); try { JsonObject json = new JsonObject(); json.addProperty("title", title); json.addProperty("body", body); PushResult result = jpushClient.sendIosNotificationWithAlias(json, extras, alias); System.out.println("Got result - " + result); } catch (APIConnectionException e) { LOG.error("Connection error. Should retry later. ", e); } catch (APIRequestException e) { LOG.error("Error response from JPush server. Should review and fix it. "+e); LOG.error("HTTP Status: " + e.getStatus()); LOG.error("Error Code: " + e.getErrorCode()); System.out.println(e.getErrorCode()); LOG.error("Error Message: " + e.getErrorMessage()); } } public static void testSendAndroidNotification(String alias,String title,String alert,Map map) { JPushClient jpushClient = new JPushClient(masterSecret, appKey , true ,86400); try { PushResult result = jpushClient.sendAndroidNotificationWithAlias(title ,alert, map, alias); System.out.println("Got result - " + result); } catch (APIConnectionException e) { LOG.error("Connection error. Should retry later. ", e); } catch (APIRequestException e) { LOG.error("Error response from JPush server. Should review and fix it. "+e); LOG.error("HTTP Status: " + e.getStatus()); LOG.error("Error Code: " + e.getErrorCode()); System.out.println(e.getMessage()); LOG.error("Error Message: " + e.getErrorMessage()); } } }