From 9908a9ebe3398e8f4c8ea7f2fb3ec87f6a60f3b2 Mon Sep 17 00:00:00 2001 From: Liuchy1 Date: Tue, 8 Sep 2015 19:38:15 +0800 Subject: [PATCH 1/4] add ios alert json --- .../cn/jpush/api/examples/PushExample.java | 26 +++- pom.xml | 5 +- src/main/java/cn/jpush/api/JPushClient.java | 62 ++++++++- .../cn/jpush/api/device/DeviceClient.java | 37 +++-- .../cn/jpush/api/device/OnlineStatus.java | 31 +++++ .../cn/jpush/api/push/model/PushModel.java | 2 + .../notification/AndroidNotification.java | 8 +- .../api/push/model/notification/IosAlert.java | 128 ++++++++++++++++++ .../model/notification/IosNotification.java | 11 +- .../push/model/notification/Notification.java | 29 ++-- .../notification/PlatformNotification.java | 32 +++-- .../notification/WinphoneNotification.java | 8 +- .../api/device/DeviceNormalRemoteTest.java | 25 ++-- .../notification/IosNotificationTest.java | 33 +++-- .../api/push/remote/NotificationTest.java | 47 +++++-- 15 files changed, 395 insertions(+), 89 deletions(-) create mode 100644 src/main/java/cn/jpush/api/device/OnlineStatus.java create mode 100644 src/main/java/cn/jpush/api/push/model/notification/IosAlert.java diff --git a/example/main/java/cn/jpush/api/examples/PushExample.java b/example/main/java/cn/jpush/api/examples/PushExample.java index 77150e42..cb69e301 100644 --- a/example/main/java/cn/jpush/api/examples/PushExample.java +++ b/example/main/java/cn/jpush/api/examples/PushExample.java @@ -1,6 +1,7 @@ package cn.jpush.api.examples; import cn.jpush.api.common.ClientConfig; +import cn.jpush.api.push.model.notification.IosAlert; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -18,6 +19,8 @@ import cn.jpush.api.push.model.notification.IosNotification; import cn.jpush.api.push.model.notification.Notification; +import java.util.HashMap; + public class PushExample { protected static final Logger LOG = LoggerFactory.getLogger(PushExample.class); @@ -32,7 +35,8 @@ public class PushExample { public static final String TAG = "tag_api"; public static void main(String[] args) { - testSendPushWithCustomConfig(); +// testSendPushWithCustomConfig(); + testSendIosAlert(); } @@ -154,5 +158,25 @@ public static void testSendPushWithCustomConfig() { } } + public static void testSendIosAlert() { + JPushClient jpushClient = new JPushClient(masterSecret, appKey); + + IosAlert alert = IosAlert.newBuilder() + .setTitleAndBody("test alert", "test ios alert json") + .setActionLocKey("PLAY") + .build(); + try { + PushResult result = jpushClient.sendIosNotificationWithAlias(alert, new HashMap(), "alias1"); + 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()); + } + } + } diff --git a/pom.xml b/pom.xml index 511ca824..7d25af87 100644 --- a/pom.xml +++ b/pom.xml @@ -143,7 +143,6 @@ maven-surefire-plugin 2.17 - UTF-8 cn.jpush.api.FastTests -Dfile.encoding=UTF-8 @@ -200,7 +199,7 @@ org.apache.maven.plugins maven-project-info-reports-plugin - 2.7 + 2.8 @@ -214,7 +213,7 @@ org.apache.maven.plugins maven-javadoc-plugin - 2.9.1 + 2.10.3 resources/javadoc-overview.html diff --git a/src/main/java/cn/jpush/api/JPushClient.java b/src/main/java/cn/jpush/api/JPushClient.java index 270df3b1..ce25ed53 100644 --- a/src/main/java/cn/jpush/api/JPushClient.java +++ b/src/main/java/cn/jpush/api/JPushClient.java @@ -1,8 +1,5 @@ package cn.jpush.api; -import java.util.Map; -import java.util.Set; - import cn.jpush.api.common.ClientConfig; import cn.jpush.api.common.TimeUnit; import cn.jpush.api.common.Week; @@ -11,16 +8,14 @@ import cn.jpush.api.common.resp.APIRequestException; import cn.jpush.api.common.resp.BooleanResult; import cn.jpush.api.common.resp.DefaultResult; -import cn.jpush.api.device.AliasDeviceListResult; -import cn.jpush.api.device.DeviceClient; -import cn.jpush.api.device.TagAliasResult; -import cn.jpush.api.device.TagListResult; +import cn.jpush.api.device.*; import cn.jpush.api.push.PushClient; import cn.jpush.api.push.PushResult; import cn.jpush.api.push.model.Message; 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.IosAlert; import cn.jpush.api.push.model.notification.Notification; import cn.jpush.api.report.MessagesResult; import cn.jpush.api.report.ReceivedsResult; @@ -33,6 +28,9 @@ import cn.jpush.api.schedule.model.TriggerPayload; import cn.jpush.api.utils.Preconditions; +import java.util.Map; +import java.util.Set; + /** * The global entrance of JPush API library. */ @@ -249,6 +247,28 @@ public PushResult sendIosNotificationWithAlias(String alert, .build(); return _pushClient.sendPush(payload); } + + /** + * Send an iOS notification with alias. + * If you want to send alert as a Json object, maybe this method is what you needed. + * + * @param alert The wrapper of APNs alert. + * @param extras The extra params. + * @param alias The alias list. + * @return + * @throws APIConnectionException + * @throws APIRequestException + */ + public PushResult sendIosNotificationWithAlias(IosAlert alert, + Map extras, String... alias) + throws APIConnectionException, APIRequestException { + PushPayload payload = PushPayload.newBuilder() + .setPlatform(Platform.ios()) + .setAudience(Audience.alias(alias)) + .setNotification(Notification.ios(alert, extras)) + .build(); + return _pushClient.sendPush(payload); + } /** * Shortcut @@ -264,6 +284,28 @@ public PushResult sendIosNotificationWithRegistrationID(String alert, return _pushClient.sendPush(payload); } + /** + * Send an iOS notification with registrationIds. + * If you want to send alert as a Json object, maybe this method is what you needed. + * + * @param alert The wrapper of APNs alert. + * @param extras The extra params. + * @param registrationID The registration ids. + * @return + * @throws APIConnectionException + * @throws APIRequestException + */ + public PushResult sendIosNotificationWithRegistrationID(IosAlert alert, + Map extras, String... registrationID) + throws APIConnectionException, APIRequestException { + PushPayload payload = PushPayload.newBuilder() + .setPlatform(Platform.ios()) + .setAudience(Audience.registrationId(registrationID)) + .setNotification(Notification.ios(alert, extras)) + .build(); + return _pushClient.sendPush(payload); + } + // ---------------------- shortcuts - message @@ -407,6 +449,12 @@ public DefaultResult deleteAlias(String alias, String platform) return _deviceClient.deleteAlias(alias, platform); } + public Map getUserOnlineStatus(String... registrationIds) + throws APIConnectionException, APIRequestException + { + return _deviceClient.getUserOnlineStatus(registrationIds); + } + // ----------------------- Schedule /** diff --git a/src/main/java/cn/jpush/api/device/DeviceClient.java b/src/main/java/cn/jpush/api/device/DeviceClient.java index 0f4d1edf..45b4e5b7 100644 --- a/src/main/java/cn/jpush/api/device/DeviceClient.java +++ b/src/main/java/cn/jpush/api/device/DeviceClient.java @@ -1,23 +1,21 @@ package cn.jpush.api.device; -import java.util.Set; - import cn.jpush.api.common.ClientConfig; import cn.jpush.api.common.ServiceHelper; import cn.jpush.api.common.connection.HttpProxy; import cn.jpush.api.common.connection.IHttpClient; import cn.jpush.api.common.connection.NativeHttpClient; -import cn.jpush.api.common.resp.APIConnectionException; -import cn.jpush.api.common.resp.APIRequestException; -import cn.jpush.api.common.resp.BaseResult; -import cn.jpush.api.common.resp.BooleanResult; -import cn.jpush.api.common.resp.DefaultResult; -import cn.jpush.api.common.resp.ResponseWrapper; +import cn.jpush.api.common.resp.*; import cn.jpush.api.utils.Preconditions; - +import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; +import com.google.gson.reflect.TypeToken; + +import java.lang.reflect.Type; +import java.util.Map; +import java.util.Set; public class DeviceClient { @@ -196,6 +194,27 @@ public DefaultResult deleteAlias(String alias, String platform) throws APIConnec return DefaultResult.fromResponse(response); } + + // -------------- devices status + + public Map getUserOnlineStatus(String... registrationIds) + throws APIConnectionException, APIRequestException + { + Preconditions.checkArgument((null != registrationIds && registrationIds.length > 0), + "The registration id list should not be empty."); + + String url = hostName + devicesPath + "/status"; + JsonObject json = new JsonObject(); + JsonArray array = new JsonArray(); + for(int i = 0; i < registrationIds.length; i++) { + array.add(new JsonPrimitive(registrationIds[i])); + } + json.add("registration_ids", array); + Type type = new TypeToken>(){}.getType(); + ResponseWrapper response = _httpClient.sendPost(url, json.toString()); + Map map = new Gson().fromJson(response.responseContent, type); + return map; + } } diff --git a/src/main/java/cn/jpush/api/device/OnlineStatus.java b/src/main/java/cn/jpush/api/device/OnlineStatus.java new file mode 100644 index 00000000..6f67cab3 --- /dev/null +++ b/src/main/java/cn/jpush/api/device/OnlineStatus.java @@ -0,0 +1,31 @@ +package cn.jpush.api.device; + +public class OnlineStatus { + + Boolean online; + String last_online_time; + + public Boolean getOnline() { + return online; + } + + public void setOnline(Boolean online) { + this.online = online; + } + + public String getLast_online_time() { + return last_online_time; + } + + public void setLast_online_time(String last_online_time) { + this.last_online_time = last_online_time; + } + + @Override + public String toString() { + if(null == last_online_time) { + return "OnlineStatus " + online; + } + return "OnlineStatus " + online + " ," + last_online_time; + } +} diff --git a/src/main/java/cn/jpush/api/push/model/PushModel.java b/src/main/java/cn/jpush/api/push/model/PushModel.java index a5275558..2be9f5d1 100644 --- a/src/main/java/cn/jpush/api/push/model/PushModel.java +++ b/src/main/java/cn/jpush/api/push/model/PushModel.java @@ -1,9 +1,11 @@ package cn.jpush.api.push.model; +import com.google.gson.Gson; import com.google.gson.JsonElement; public interface PushModel { + public static Gson gson = new Gson(); public JsonElement toJSON(); } diff --git a/src/main/java/cn/jpush/api/push/model/notification/AndroidNotification.java b/src/main/java/cn/jpush/api/push/model/notification/AndroidNotification.java index b1d362b0..8682a8f1 100644 --- a/src/main/java/cn/jpush/api/push/model/notification/AndroidNotification.java +++ b/src/main/java/cn/jpush/api/push/model/notification/AndroidNotification.java @@ -1,11 +1,11 @@ package cn.jpush.api.push.model.notification; -import java.util.Map; - import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; +import java.util.Map; + public class AndroidNotification extends PlatformNotification { public static final String NOTIFICATION_ANDROID = "android"; @@ -15,7 +15,7 @@ public class AndroidNotification extends PlatformNotification { private final String title; private final int builderId; - private AndroidNotification(String alert, String title, int builderId, + private AndroidNotification(Object alert, String title, int builderId, Map extras, Map numberExtras, Map booleanExtras, @@ -73,7 +73,7 @@ public Builder setBuilderId(int builderId) { return this; } - public Builder setAlert(String alert) { + public Builder setAlert(Object alert) { this.alert = alert; return this; } diff --git a/src/main/java/cn/jpush/api/push/model/notification/IosAlert.java b/src/main/java/cn/jpush/api/push/model/notification/IosAlert.java new file mode 100644 index 00000000..19b4211a --- /dev/null +++ b/src/main/java/cn/jpush/api/push/model/notification/IosAlert.java @@ -0,0 +1,128 @@ +package cn.jpush.api.push.model.notification; + +import cn.jpush.api.push.model.PushModel; +import cn.jpush.api.utils.StringUtils; +import com.google.gson.*; + +public class IosAlert implements PushModel { + + private final String title; + private final String body; + private final String title_loc_key; + private final String[] title_loc_args; + private final String action_loc_key; + private final String loc_key; + private final String[] loc_args; + private final String launch_image; + + private IosAlert(String title, String body, String title_loc_key, String[] title_loc_args, + String action_loc_key, String loc_key, String[] loc_args, String launch_image) { + this.title = title; + this.body = body; + this.title_loc_key = title_loc_key; + this.title_loc_args = title_loc_args; + this.action_loc_key = action_loc_key; + this.loc_key = loc_key; + this.loc_args = loc_args; + this.launch_image = launch_image; + } + + public static Builder newBuilder() { + return new Builder(); + } + + @Override + public JsonElement toJSON() { + JsonObject json = new JsonObject(); + + if( StringUtils.isNotEmpty(title) ) { + json.addProperty("title", title); + } + + if( StringUtils.isNotEmpty(title) ) { + json.addProperty("body", body); + } + + if( StringUtils.isNotEmpty(title_loc_key) ) { + json.addProperty("title-loc-key", title_loc_key); + if( null != title_loc_args && title_loc_args.length > 0 ) { + JsonArray args = new JsonArray(); + for(int i = 0; i < title_loc_args.length; i++) { + args.add(new JsonPrimitive(title_loc_args[i])); + } + json.add("title-loc-args", args); + } + } + + if( StringUtils.isNotEmpty(action_loc_key) ) { + json.addProperty("action-loc-key", action_loc_key); + } + + if( StringUtils.isNotEmpty(loc_key) ) { + json.addProperty("loc-key", loc_key); + if( null != loc_args && loc_args.length > 0 ) { + JsonArray args = new JsonArray(); + for(int i = 0; i < loc_args.length; i++) { + args.add(new JsonPrimitive(loc_args[i])); + } + json.add("loc-args", args); + } + } + + if( StringUtils.isNotEmpty(launch_image) ) { + json.addProperty("launch-image", launch_image); + } + + return json; + } + + @Override + public String toString() { + return gson.toJson(toJSON()); + } + + public static class Builder { + private String title; + private String body; + private String title_loc_key; + private String[] title_loc_args; + private String action_loc_key; + private String loc_key; + private String[] loc_args; + private String launch_image; + + public Builder setTitleAndBody(String title, String body){ + this.title = title; + this.body = body; + return this; + } + + public Builder setTitleLoc(String title_loc_key, String... title_loc_args) { + this.title_loc_key = title_loc_key; + this.title_loc_args = title_loc_args; + return this; + } + + public Builder setActionLocKey(String action_loc_key) { + this.action_loc_key = action_loc_key; + return this; + } + + public Builder setLoc(String loc_key, String... loc_args) { + this.loc_key = loc_key; + this.loc_args = loc_args; + return this; + } + + public Builder setLaunchImage(String launch_image) { + this.launch_image = launch_image; + return this; + } + + public IosAlert build() { + return new IosAlert(title, body, title_loc_key, title_loc_args, action_loc_key, + loc_key, loc_args, launch_image); + } + } + +} diff --git a/src/main/java/cn/jpush/api/push/model/notification/IosNotification.java b/src/main/java/cn/jpush/api/push/model/notification/IosNotification.java index bcd6b191..92af6169 100644 --- a/src/main/java/cn/jpush/api/push/model/notification/IosNotification.java +++ b/src/main/java/cn/jpush/api/push/model/notification/IosNotification.java @@ -1,13 +1,12 @@ package cn.jpush.api.push.model.notification; -import java.util.Map; - import cn.jpush.api.common.ServiceHelper; - import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; +import java.util.Map; + /** * APNs 通知类 *

@@ -46,7 +45,7 @@ public class IosNotification extends PlatformNotification { private final boolean contentAvailable; private final String category; - private IosNotification(String alert, String sound, String badge, + private IosNotification(Object alert, String sound, String badge, boolean contentAvailable, boolean soundDisabled, boolean badgeDisabled, String category, Map extras, @@ -173,11 +172,11 @@ public Builder setCategory(String category) { return this; } - public Builder setAlert(String alert) { + public Builder setAlert(Object alert) { this.alert = alert; return this; } - + public IosNotification build() { return new IosNotification(alert, sound, badge, contentAvailable, diff --git a/src/main/java/cn/jpush/api/push/model/notification/Notification.java b/src/main/java/cn/jpush/api/push/model/notification/Notification.java index f8575bc9..4134a7ac 100644 --- a/src/main/java/cn/jpush/api/push/model/notification/Notification.java +++ b/src/main/java/cn/jpush/api/push/model/notification/Notification.java @@ -1,21 +1,20 @@ package cn.jpush.api.push.model.notification; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - import cn.jpush.api.push.model.PushModel; import cn.jpush.api.utils.Preconditions; - import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + public class Notification implements PushModel { - private final String alert; + private final Object alert; private final Set notifications; - private Notification(String alert, Set notifications) { + private Notification(Object alert, Set notifications) { this.alert = alert; this.notifications = notifications; } @@ -31,7 +30,7 @@ public static Builder newBuilder() { * @param alert Notification alert * @return first level notification object */ - public static Notification alert(String alert) { + public static Notification alert(Object alert) { return newBuilder().setAlert(alert).build(); } @@ -51,7 +50,7 @@ public static Notification android(String alert, String title, Map extras) { + public static Notification ios(Object alert, Map extras) { return newBuilder() .addPlatformNotification(IosNotification.newBuilder() .setAlert(alert) @@ -111,7 +110,13 @@ public static Notification winphone(String alert, Map extras) { public JsonElement toJSON() { JsonObject json = new JsonObject(); if (null != alert) { - json.add(PlatformNotification.ALERT, new JsonPrimitive(alert)); + if(alert instanceof JsonObject) { + json.add(PlatformNotification.ALERT, (JsonObject) alert); + } else if (alert instanceof IosAlert) { + json.add(PlatformNotification.ALERT, ((IosAlert) alert).toJSON()); + } else { + json.add(PlatformNotification.ALERT, new JsonPrimitive(alert.toString())); + } } if (null != notifications) { for (PlatformNotification pn : notifications) { @@ -129,10 +134,10 @@ public JsonElement toJSON() { } public static class Builder { - private String alert; + private Object alert; private Set builder; - public Builder setAlert(String alert) { + public Builder setAlert(Object alert) { this.alert = alert; return this; } diff --git a/src/main/java/cn/jpush/api/push/model/notification/PlatformNotification.java b/src/main/java/cn/jpush/api/push/model/notification/PlatformNotification.java index da87ee74..9ad0925e 100644 --- a/src/main/java/cn/jpush/api/push/model/notification/PlatformNotification.java +++ b/src/main/java/cn/jpush/api/push/model/notification/PlatformNotification.java @@ -1,17 +1,15 @@ package cn.jpush.api.push.model.notification; -import java.util.HashMap; -import java.util.Map; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import cn.jpush.api.push.model.PushModel; import cn.jpush.api.utils.Preconditions; - import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.HashMap; +import java.util.Map; public abstract class PlatformNotification implements PushModel { public static final String ALERT = "alert"; @@ -19,13 +17,13 @@ public abstract class PlatformNotification implements PushModel { protected static final Logger LOG = LoggerFactory.getLogger(PlatformNotification.class); - private String alert; + private Object alert; private final Map extras; private final Map numberExtras; private final Map booleanExtras; private final Map jsonExtras; - public PlatformNotification(String alert, Map extras, + public PlatformNotification(Object alert, Map extras, Map numberExtras, Map booleanExtras, Map jsonExtras) { @@ -41,7 +39,13 @@ public JsonElement toJSON() { JsonObject json = new JsonObject(); if (null != alert) { - json.add(ALERT, new JsonPrimitive(this.alert)); + if ( alert instanceof JsonObject) { + json.add(ALERT, (JsonObject) alert); + } else if (alert instanceof IosAlert) { + json.add(PlatformNotification.ALERT, ((IosAlert) alert).toJSON()); + } else { + json.add(PlatformNotification.ALERT, new JsonPrimitive(alert.toString())); + } } JsonObject extrasObject = null; @@ -93,11 +97,11 @@ public JsonElement toJSON() { return json; } - protected String getAlert() { + protected Object getAlert() { return this.alert; } - protected void setAlert(String alert) { + protected void setAlert(Object alert) { this.alert = alert; } @@ -106,7 +110,7 @@ protected void setAlert(String alert) { protected abstract static class Builder> { private B theBuilder; - protected String alert; + protected Object alert; protected Map extrasBuilder; protected Map numberExtrasBuilder; protected Map booleanExtrasBuilder; @@ -118,7 +122,7 @@ public Builder () { protected abstract B getThis(); - public abstract B setAlert(String alert); + public abstract B setAlert(Object alert); public B addExtra(String key, String value) { Preconditions.checkArgument(! (null == key), "Key should not be null."); diff --git a/src/main/java/cn/jpush/api/push/model/notification/WinphoneNotification.java b/src/main/java/cn/jpush/api/push/model/notification/WinphoneNotification.java index 388da698..9ceecf52 100644 --- a/src/main/java/cn/jpush/api/push/model/notification/WinphoneNotification.java +++ b/src/main/java/cn/jpush/api/push/model/notification/WinphoneNotification.java @@ -1,11 +1,11 @@ package cn.jpush.api.push.model.notification; -import java.util.Map; - import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; +import java.util.Map; + public class WinphoneNotification extends PlatformNotification { private static final String NOTIFICATION_WINPHONE = "winphone"; @@ -15,7 +15,7 @@ public class WinphoneNotification extends PlatformNotification { private final String title; private final String openPage; - private WinphoneNotification(String alert, String title, String openPage, + private WinphoneNotification(Object alert, String title, String openPage, Map extras, Map numberExtras, Map booleanExtras, @@ -73,7 +73,7 @@ public Builder setOpenPage(String openPage) { return this; } - public Builder setAlert(String alert) { + public Builder setAlert(Object alert) { this.alert = alert; return this; } diff --git a/src/test/java/cn/jpush/api/device/DeviceNormalRemoteTest.java b/src/test/java/cn/jpush/api/device/DeviceNormalRemoteTest.java index 1b7f26a6..c6b3d806 100644 --- a/src/test/java/cn/jpush/api/device/DeviceNormalRemoteTest.java +++ b/src/test/java/cn/jpush/api/device/DeviceNormalRemoteTest.java @@ -1,15 +1,5 @@ package cn.jpush.api.device; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import java.util.HashSet; -import java.util.Set; - -import org.junit.Test; -import org.junit.runner.RunWith; - import cn.jpush.api.BaseTest; import cn.jpush.api.JUnitOrderedRunner; import cn.jpush.api.TestOrder; @@ -17,6 +7,14 @@ import cn.jpush.api.common.resp.APIRequestException; import cn.jpush.api.common.resp.BooleanResult; import cn.jpush.api.common.resp.DefaultResult; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import static org.junit.Assert.*; @RunWith(JUnitOrderedRunner.class) public class DeviceNormalRemoteTest extends BaseTest { @@ -169,4 +167,11 @@ public void testDeleteAlias_2() throws APIConnectionException, APIRequestExcepti DefaultResult result = jpushClient.deleteAlias("alias2", null); assertTrue(result.isResultOK()); } + + @Test + @TestOrder(order = 340) + public void testTetUserOnlineStatus() throws APIConnectionException, APIRequestException{ + Map result = jpushClient.getUserOnlineStatus(REGISTRATION_ID1, REGISTRATION_ID2); + assertTrue(result.get(REGISTRATION_ID1) != null); + } } diff --git a/src/test/java/cn/jpush/api/push/model/notification/IosNotificationTest.java b/src/test/java/cn/jpush/api/push/model/notification/IosNotificationTest.java index 45f76fef..12b0118b 100644 --- a/src/test/java/cn/jpush/api/push/model/notification/IosNotificationTest.java +++ b/src/test/java/cn/jpush/api/push/model/notification/IosNotificationTest.java @@ -1,18 +1,16 @@ package cn.jpush.api.push.model.notification; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; - -import org.junit.Assert; -import org.junit.Test; -import org.junit.experimental.categories.Category; - import cn.jpush.api.FastTests; - import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; +import org.junit.Assert; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; @Category(FastTests.class) public class IosNotificationTest { @@ -152,6 +150,23 @@ public void testCategory() { assertThat(ios.toJSON(), is((JsonElement) json)); } + + @Test + public void testAlertJson() { + + IosNotification ios = IosNotification.alert("{\"title\":\"Game Request\",\"body\":\"Bob wants to play poker\",\"action-loc-key\":\"PLAY\" }"); + + JsonObject json = new JsonObject(); + JsonObject alert = new JsonObject(); + alert.add("title", new JsonPrimitive("Game Request")); + alert.add("body", new JsonPrimitive("Bob wants to play poker")); + alert.add("action-loc-key", new JsonPrimitive("PLAY")); + + json.add("alert", alert); + json.add("sound", new JsonPrimitive("")); + json.add("badge", new JsonPrimitive("+1")); + assertEquals("", json, ios.toJSON()); + } } diff --git a/src/test/java/cn/jpush/api/push/remote/NotificationTest.java b/src/test/java/cn/jpush/api/push/remote/NotificationTest.java index c18faa20..232811b9 100644 --- a/src/test/java/cn/jpush/api/push/remote/NotificationTest.java +++ b/src/test/java/cn/jpush/api/push/remote/NotificationTest.java @@ -1,19 +1,19 @@ package cn.jpush.api.push.remote; -import static org.junit.Assert.*; - -import org.junit.Test; -import org.junit.experimental.categories.Category; - import cn.jpush.api.SlowTests; 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.AndroidNotification; +import cn.jpush.api.push.model.notification.IosAlert; import cn.jpush.api.push.model.notification.Notification; - import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import static org.junit.Assert.assertTrue; @Category(SlowTests.class) public class NotificationTest extends BaseRemotePushTest { @@ -99,9 +99,36 @@ public void sendNotification_ios_badge() throws Exception { PushResult result = _client.sendPush(payload); assertTrue(result.isResultOK()); } - - - - + + @Test + public void sendNotification_ios_alert_jsonStr() throws Exception { + JsonObject alert = new JsonObject(); + alert.add("title", new JsonPrimitive("Game Request")); + alert.add("body", new JsonPrimitive("Bob wants to play poker")); + alert.add("action-loc-key", new JsonPrimitive("PLAY")); + PushPayload payload = PushPayload.newBuilder() + .setAudience(Audience.all()) + .setPlatform(Platform.ios()) + .setNotification(Notification.alert(alert.toString())) + .build(); + PushResult result = _client.sendPush(payload); + assertTrue(result.isResultOK()); + } + + @Test + public void sendNotification_ios_alert_jsonObj() throws Exception { + IosAlert alert = IosAlert.newBuilder() + .setTitleAndBody("ios title", "test ios title") + .build(); + + PushPayload payload = PushPayload.newBuilder() + .setAudience(Audience.all()) + .setPlatform(Platform.ios()) + .setNotification(Notification.alert(alert)) + .build(); + PushResult result = _client.sendPush(payload); + assertTrue(result.isResultOK()); + } + } From 1a2335df232e5a8792321c927570f4139a3f4409 Mon Sep 17 00:00:00 2001 From: Liuchy1 Date: Tue, 8 Sep 2015 19:47:56 +0800 Subject: [PATCH 2/4] add example --- .../cn/jpush/api/examples/DevcieExample.java | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/example/main/java/cn/jpush/api/examples/DevcieExample.java b/example/main/java/cn/jpush/api/examples/DevcieExample.java index 2ad18316..2ff1c605 100644 --- a/example/main/java/cn/jpush/api/examples/DevcieExample.java +++ b/example/main/java/cn/jpush/api/examples/DevcieExample.java @@ -1,5 +1,6 @@ package cn.jpush.api.examples; +import cn.jpush.api.device.OnlineStatus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -8,11 +9,13 @@ import cn.jpush.api.common.resp.APIRequestException; import cn.jpush.api.device.TagAliasResult; +import java.util.Map; + public class DevcieExample { protected static final Logger LOG = LoggerFactory.getLogger(DevcieExample.class); private static final String appKey = "dd1066407b044738b6479275"; - private static final String masterSecret = "2b38ce69b1de2a7fa95706ea"; + private static final String masterSecret = "6b135be0037a5c1e693c3dfa"; private static final String TAG1 = "tag1"; private static final String ALIAS1 = "alias1"; private static final String ALIAS2 = "alias2"; @@ -22,7 +25,8 @@ public class DevcieExample { private static JPushClient jpushClient = new JPushClient(masterSecret, appKey); public static void main(String[] args) { - testGetDeviceTagAlias(); +// testGetDeviceTagAlias(); + testGetUserOnlineStatus(); } public static void testGetDeviceTagAlias() { @@ -42,6 +46,22 @@ public static void testGetDeviceTagAlias() { LOG.info("Error Message: " + e.getErrorMessage()); } } + + public static void testGetUserOnlineStatus() { + try { + Map result = jpushClient.getUserOnlineStatus(REGISTRATION_ID1, REGISTRATION_ID2); + + LOG.info(result.get(REGISTRATION_ID1).toString()); + LOG.info(result.get(REGISTRATION_ID2).toString()); + } 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()); + } + } } From cbae121a3e58603c53f1a61cdb65188abdd2bb84 Mon Sep 17 00:00:00 2001 From: Liuchy1 Date: Tue, 8 Sep 2015 19:51:11 +0800 Subject: [PATCH 3/4] modify getUserOnlineStatus --- src/main/java/cn/jpush/api/device/DeviceClient.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/cn/jpush/api/device/DeviceClient.java b/src/main/java/cn/jpush/api/device/DeviceClient.java index 45b4e5b7..a68772e7 100644 --- a/src/main/java/cn/jpush/api/device/DeviceClient.java +++ b/src/main/java/cn/jpush/api/device/DeviceClient.java @@ -200,8 +200,10 @@ public DefaultResult deleteAlias(String alias, String platform) throws APIConnec public Map getUserOnlineStatus(String... registrationIds) throws APIConnectionException, APIRequestException { - Preconditions.checkArgument((null != registrationIds && registrationIds.length > 0), - "The registration id list should not be empty."); + Preconditions.checkArgument((null != registrationIds ), + "The registration id list should not be null."); + Preconditions.checkArgument(registrationIds.length > 0 && registrationIds.length <= 1000, + "The length of registration id list should between 1 and 1000."); String url = hostName + devicesPath + "/status"; JsonObject json = new JsonObject(); From 181c26356128b99312a97b210bb359b05317c7c8 Mon Sep 17 00:00:00 2001 From: Liuchy1 Date: Tue, 8 Sep 2015 19:58:32 +0800 Subject: [PATCH 4/4] fix test bug --- .../model/notification/IosNotificationTest.java | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/test/java/cn/jpush/api/push/model/notification/IosNotificationTest.java b/src/test/java/cn/jpush/api/push/model/notification/IosNotificationTest.java index 12b0118b..7a1d037c 100644 --- a/src/test/java/cn/jpush/api/push/model/notification/IosNotificationTest.java +++ b/src/test/java/cn/jpush/api/push/model/notification/IosNotificationTest.java @@ -151,22 +151,6 @@ public void testCategory() { assertThat(ios.toJSON(), is((JsonElement) json)); } - @Test - public void testAlertJson() { - - IosNotification ios = IosNotification.alert("{\"title\":\"Game Request\",\"body\":\"Bob wants to play poker\",\"action-loc-key\":\"PLAY\" }"); - - JsonObject json = new JsonObject(); - JsonObject alert = new JsonObject(); - alert.add("title", new JsonPrimitive("Game Request")); - alert.add("body", new JsonPrimitive("Bob wants to play poker")); - alert.add("action-loc-key", new JsonPrimitive("PLAY")); - - json.add("alert", alert); - json.add("sound", new JsonPrimitive("")); - json.add("badge", new JsonPrimitive("+1")); - assertEquals("", json, ios.toJSON()); - } }