Skip to content

Commit

Permalink
Extending sms message
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshLipan committed May 24, 2018
1 parent 909dd57 commit 40a91c2
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 17 deletions.
10 changes: 5 additions & 5 deletions example/main/java/cn/jpush/api/examples/PushExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public class PushExample {
protected static final Logger LOG = LoggerFactory.getLogger(PushExample.class);

// demo App defined in resources/jpush-api.conf
protected static final String APP_KEY ="d4ee2375846bc30fa51334f5";
protected static final String MASTER_SECRET = "f3b222f7e0dde430b6d8fa5a";
protected static final String APP_KEY ="7b4b94cca0d185d611e53cca";
protected static final String MASTER_SECRET = "860803cf613ed54aa3b941a8";
protected static final String GROUP_PUSH_KEY = "2c88a01e073a0fe4fc7b167c";
protected static final String GROUP_MASTER_SECRET = "b11314807507e2bcfdeebe2e";

Expand All @@ -50,8 +50,8 @@ public class PushExample {
public static void main(String[] args) {
// testSendPushWithCustomConfig();
// testSendIosAlert();
// testSendPush();
testGetCidList();
testSendPush();
// testGetCidList();
// testSendPushes();
// testSendPush_fromJSON();
// testSendPushWithCallback();
Expand Down Expand Up @@ -408,7 +408,7 @@ public static void testSendIosAlert() {
public static void testSendWithSMS() {
JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
try {
SMS sms = SMS.content("Test SMS", 10);
SMS sms = SMS.content(1, 10);
PushResult result = jpushClient.sendAndroidMessageWithAlias("Test SMS", "test sms", sms, "alias1");
LOG.info("Got result - " + result);
} catch (APIConnectionException e) {
Expand Down
5 changes: 3 additions & 2 deletions example/main/java/cn/jpush/api/examples/ReportsExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public class ReportsExample {
protected static final Logger LOG = LoggerFactory.getLogger(ReportsExample.class);

// demo App defined in resources/jpush-api.conf
private static final String appKey = "dd1066407b044738b6479275";
private static final String masterSecret = "e8cc9a76d5b7a580859bcfa7";
private static final String appKey = "7b4b94cca0d185d611e53cca";
private static final String masterSecret = "860803cf613ed54aa3b941a8";
public static final String REGISTRATION_ID1 = "0900e8d85ef";
public static final String REGISTRATION_ID2 = "0a04ad7d8b4";
public static final String REGISTRATION_ID3 = "18071adc030dcba91c0";
Expand All @@ -29,6 +29,7 @@ public static void main(String[] args) {
testGetReport();
testGetMessages();
testGetUsers();
testGetMessageStatus();
}


Expand Down
22 changes: 20 additions & 2 deletions src/main/java/cn/jpush/api/push/model/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@ public class Message implements PushModel {
private final Map<String, String> extras;
private final Map<String, Number> numberExtras;
private final Map<String, Boolean> booleanExtras;
private final Map<String, JsonObject> jsonExtras;

private Message(String title, String msgContent, String contentType,
Map<String, String> extras,
Map<String, Number> numberExtras,
Map<String, Boolean> booleanExtras) {
Map<String, Boolean> booleanExtras,
Map<String, JsonObject> jsonExtras) {
this.title = title;
this.msgContent = msgContent;
this.contentType = contentType;
this.extras = extras;
this.numberExtras = numberExtras;
this.booleanExtras = booleanExtras;
this.jsonExtras = jsonExtras;
}

public static Builder newBuilder() {
Expand Down Expand Up @@ -80,6 +83,11 @@ public JsonElement toJSON() {
extrasObject.add(key, new JsonPrimitive(booleanExtras.get(key)));
}
}
if (null != jsonExtras) {
for (String key : jsonExtras.keySet()) {
extrasObject.add(key, jsonExtras.get(key));
}
}

if (null != extras || null != numberExtras || null != booleanExtras) {
json.add(EXTRAS, extrasObject);
Expand All @@ -95,6 +103,7 @@ public static class Builder {
private Map<String, String> extrasBuilder;
private Map<String, Number> numberExtrasBuilder;
private Map<String, Boolean> booleanExtrasBuilder;
protected Map<String, JsonObject> jsonExtrasBuilder;

public Builder setTitle(String title) {
this.title = title;
Expand Down Expand Up @@ -149,11 +158,20 @@ public Builder addExtra(String key, Boolean value) {
return this;
}

public Builder addExtra(String key, JsonObject value) {
Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null.");
if (null == jsonExtrasBuilder) {
jsonExtrasBuilder = new HashMap<String, JsonObject>();
}
jsonExtrasBuilder.put(key, value);
return this;
}

public Message build() {
Preconditions.checkArgument(! (null == msgContent),
"msgContent should be set");
return new Message(title, msgContent, contentType,
extrasBuilder, numberExtrasBuilder, booleanExtrasBuilder);
extrasBuilder, numberExtrasBuilder, booleanExtrasBuilder,jsonExtrasBuilder);
}
}
}
3 changes: 2 additions & 1 deletion src/main/java/cn/jpush/api/push/model/PushPayload.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.jpush.api.push.model;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
Expand Down Expand Up @@ -33,7 +34,7 @@ public class PushPayload implements PushModel {
private static final int MAX_GLOBAL_ENTITY_LENGTH = 4000; // Definition acording to JPush Docs
private static final int MAX_IOS_PAYLOAD_LENGTH = 2000; // Definition acording to JPush Docs

private static Gson _gson = new Gson();
private static Gson _gson = new GsonBuilder().disableHtmlEscaping().create();

private final Platform platform;
private final Audience audience;
Expand Down
139 changes: 134 additions & 5 deletions src/main/java/cn/jpush/api/push/model/SMS.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,46 @@
package cn.jpush.api.push.model;

import java.util.HashMap;
import java.util.Map;

import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;

import cn.jiguang.common.utils.Preconditions;
import cn.jiguang.common.utils.StringUtils;

public class SMS implements PushModel {

private final String content;
private final int delay_time;
private final long temp_id;
private final Map<String, String> extras;
private final Map<String, Number> numberExtras;
private final Map<String, Boolean> booleanExtras;
private final Map<String, JsonObject> jsonExtras;

private SMS(String content, int delay_time) {
private SMS(String content, int delay_time, long temp_id,
Map<String, String> extras,
Map<String, Number> numberExtras,
Map<String, Boolean> booleanExtras,
Map<String, JsonObject> jsonExtras) {
this.content = content;
this.delay_time = delay_time;
this.temp_id = temp_id;
this.extras = extras;
this.numberExtras = numberExtras;
this.booleanExtras = booleanExtras;
this.jsonExtras = jsonExtras;

}

public static Builder newBuilder() {
return new Builder();
}

/**
* This will be removed in the future. Please use content(long tempId, int delayTime) this constructor.
* Create a SMS content with a delay time.
* JPush will send a SMS if the message doesn't received within the delay time. If the delay time is 0, the SMS will be sent immediately.
* Please note the delay time only works on Android.
Expand All @@ -30,24 +50,81 @@ public static Builder newBuilder() {
* @param delayTime The seconds you want to delay, should be greater than or equal to 0.
* @return SMS payload.
*/
@Deprecated
public static SMS content(String content, int delayTime) {
return new Builder()
.setContent(content)
.setDelayTime(delayTime)
.build();
}

public static SMS content(long tempId, int delayTime) {
return new Builder()
.setTempID(tempId)
.setDelayTime(delayTime)
.build();
}


@Override
public JsonElement toJSON() {
JsonObject json = new JsonObject();
json.addProperty("content", content);

json.addProperty("delay_time", delay_time);

if (temp_id > 0) {
json.addProperty("temp_id", temp_id);
}

if (null != content) {
json.addProperty("content", content);
}


JsonObject extrasObject = null;
if (null != extras || null != numberExtras || null != booleanExtras) {
extrasObject = new JsonObject();
}

if (null != extras) {
for (String key : extras.keySet()) {
if (extras.get(key) != null) {
extrasObject.add(key, new JsonPrimitive(extras.get(key)));
} else {
extrasObject.add(key, JsonNull.INSTANCE);
}
}
}
if (null != numberExtras) {
for (String key : numberExtras.keySet()) {
extrasObject.add(key, new JsonPrimitive(numberExtras.get(key)));
}
}
if (null != booleanExtras) {
for (String key : booleanExtras.keySet()) {
extrasObject.add(key, new JsonPrimitive(booleanExtras.get(key)));
}
}
if (null != jsonExtras) {
for (String key : jsonExtras.keySet()) {
extrasObject.add(key, jsonExtras.get(key));
}
}

if (null != extras || null != numberExtras || null != booleanExtras) {
json.add("temp_para", extrasObject);
}
return json;
}

public static class Builder {
private String content;
private int delay_time;
private long temp_id;
private Map<String, String> extrasBuilder;
private Map<String, Number> numberExtrasBuilder;
private Map<String, Boolean> booleanExtrasBuilder;
protected Map<String, JsonObject> jsonExtrasBuilder;

public Builder setContent(String content) {
this.content = content;
Expand All @@ -58,12 +135,64 @@ public Builder setDelayTime(int delayTime) {
this.delay_time = delayTime;
return this;
}

public Builder setTempID(long tempID) {
this.temp_id = tempID;
return this;
}

public Builder addPara(String key, String value) {
Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null.");
if (null == extrasBuilder) {
extrasBuilder = new HashMap<String, String>();
}
extrasBuilder.put(key, value);
return this;
}

public Builder addParas(Map<String, String> extras) {
Preconditions.checkArgument(! (null == extras), "extras should not be null.");
if (null == extrasBuilder) {
extrasBuilder = new HashMap<String, String>();
}
for (String key : extras.keySet()) {
extrasBuilder.put(key, extras.get(key));
}
return this;
}

public Builder addPara(String key, Number value) {
Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null.");
if (null == numberExtrasBuilder) {
numberExtrasBuilder = new HashMap<String, Number>();
}
numberExtrasBuilder.put(key, value);
return this;
}

public Builder addPara(String key, Boolean value) {
Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null.");
if (null == booleanExtrasBuilder) {
booleanExtrasBuilder = new HashMap<String, Boolean>();
}
booleanExtrasBuilder.put(key, value);
return this;
}

public Builder addPara(String key, JsonObject value) {
Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null.");
if (null == jsonExtrasBuilder) {
jsonExtrasBuilder = new HashMap<String, JsonObject>();
}
jsonExtrasBuilder.put(key, value);
return this;
}

public SMS build() {
Preconditions.checkArgument(StringUtils.isNotEmpty(content), "The content must not be empty.");
Preconditions.checkArgument(delay_time >= 0, "The delay time must be greater than or equal to 0");

return new SMS(content, delay_time);
return new SMS(content, delay_time, temp_id,
extrasBuilder, numberExtrasBuilder, booleanExtrasBuilder,jsonExtrasBuilder);
}

}
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/cn/jpush/api/report/ReportClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class ReportClient {
private String _receivePath;
private String _userPath;
private String _messagePath;
private String _statusPath;

public ReportClient(String masterSecret, String appKey) {
this(masterSecret, appKey, null, ClientConfig.getInstance());
Expand Down Expand Up @@ -63,7 +64,8 @@ public ReportClient(String masterSecret, String appKey, int maxRetryTimes, HttpP
_receivePath = (String) conf.get(ClientConfig.REPORT_RECEIVE_PATH);
_userPath = (String) conf.get(ClientConfig.REPORT_USER_PATH);
_messagePath = (String) conf.get(ClientConfig.REPORT_MESSAGE_PATH);

_statusPath = (String) conf.get(ClientConfig.REPORT_STATUS_PATH);

String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret);
_httpClient = new NativeHttpClient(authCode, proxy, conf);
}
Expand All @@ -75,6 +77,7 @@ public ReportClient(String masterSecret, String appKey, HttpProxy proxy, ClientC
_receivePath = (String) conf.get(ClientConfig.REPORT_RECEIVE_PATH);
_userPath = (String) conf.get(ClientConfig.REPORT_USER_PATH);
_messagePath = (String) conf.get(ClientConfig.REPORT_MESSAGE_PATH);
_statusPath = (String) conf.get(ClientConfig.REPORT_STATUS_PATH);

String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret);
_httpClient = new NativeHttpClient(authCode, proxy, conf);
Expand Down Expand Up @@ -108,7 +111,7 @@ public MessagesResult getMessages(String msgIds)

public Map<String, MessageStatus> getMessagesStatus(CheckMessagePayload payload)
throws APIConnectionException, APIRequestException {
String url = _hostName + "/v3/status/message";
String url = _hostName + (_statusPath.endsWith("/message")?_statusPath:(_statusPath+"/message"));
ResponseWrapper result = _httpClient.sendPost(url, payload.toString());
Type type = new TypeToken<Map<String, MessageStatus>>(){}.getType();
return new Gson().fromJson(result.responseContent, type);
Expand Down

0 comments on commit 40a91c2

Please sign in to comment.