Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

notification(Android,ios,winphone)支持传递JsonArray类型扩展参数,Message支持传递Json… #185

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/main/java/cn/jpush/api/push/model/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.LinkedHashMap;
import java.util.Map;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
Expand All @@ -25,13 +26,15 @@ public class Message implements PushModel {
private final Map<String, Boolean> booleanExtras;
private final Map<String, JsonObject> jsonExtras;
private final Map<String, JsonPrimitive> customData;
private final Map<String, JsonArray> jsonArrayExtras;

private Message(String title, String msgContent, String contentType,
Map<String, String> extras,
Map<String, Number> numberExtras,
Map<String, Boolean> booleanExtras,
Map<String, JsonObject> jsonExtras,
Map<String, JsonPrimitive> customData) {
Map<String, JsonPrimitive> customData,
Map<String, JsonArray> jsonArrayExtras) {
this.title = title;
this.msgContent = msgContent;
this.contentType = contentType;
Expand All @@ -40,6 +43,7 @@ private Message(String title, String msgContent, String contentType,
this.booleanExtras = booleanExtras;
this.jsonExtras = jsonExtras;
this.customData = customData;
this.jsonArrayExtras = jsonArrayExtras;
}

public static Builder newBuilder() {
Expand All @@ -64,7 +68,7 @@ public JsonElement toJSON() {
}

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

Expand Down Expand Up @@ -93,6 +97,12 @@ public JsonElement toJSON() {
}
}

if (null != jsonArrayExtras) {
for (String key : jsonArrayExtras.keySet()) {
extrasObject.add(key, jsonArrayExtras.get(key));
}
}

if (null != extras || null != numberExtras || null != booleanExtras) {
json.add(EXTRAS, extrasObject);
}
Expand All @@ -115,6 +125,7 @@ public static class Builder {
private Map<String, Boolean> booleanExtrasBuilder;
protected Map<String, JsonObject> jsonExtrasBuilder;
private Map<String, JsonPrimitive> customData;
protected Map<String, JsonArray> jsonArrayExtrasBuilder;

public Builder setTitle(String title) {
this.title = title;
Expand Down Expand Up @@ -178,6 +189,15 @@ public Builder addExtra(String key, JsonObject value) {
return this;
}

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

public Builder addCustom(Map<String, String> extras) {
if (customData == null) {
customData = new LinkedHashMap<>();
Expand Down Expand Up @@ -219,7 +239,7 @@ public Message build() {
Preconditions.checkArgument(! (null == msgContent),
"msgContent should be set");
return new Message(title, msgContent, contentType,
extrasBuilder, numberExtrasBuilder, booleanExtrasBuilder,jsonExtrasBuilder, customData);
extrasBuilder, numberExtrasBuilder, booleanExtrasBuilder,jsonExtrasBuilder, customData, jsonArrayExtrasBuilder);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.jpush.api.push.model.notification;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
Expand Down Expand Up @@ -57,8 +58,9 @@ private AndroidNotification(Object alert,
Map<String, Number> numberExtras,
Map<String, Boolean> booleanExtras,
Map<String, JsonObject> jsonExtras,
Map<String, JsonPrimitive> customData) {
super(alert, extras, numberExtras, booleanExtras, jsonExtras, customData);
Map<String, JsonPrimitive> customData,
Map<String, JsonArray> jsonArrayExtras) {
super(alert, extras, numberExtras, booleanExtras, jsonExtras, customData, jsonArrayExtras);

this.title = title;
this.builderId = builderId;
Expand Down Expand Up @@ -282,7 +284,8 @@ public AndroidNotification build() {
numberExtrasBuilder,
booleanExtrasBuilder,
jsonExtrasBuilder,
super.customData
super.customData,
jsonArrayExtrasBuilder
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Map;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
Expand Down Expand Up @@ -62,8 +63,9 @@ private IosNotification(Object alert, Object sound, String badge,
Map<String, Number> numberExtras,
Map<String, Boolean> booleanExtras,
Map<String, JsonObject> jsonExtras,
Map<String, JsonPrimitive> customData) {
super(alert, extras, numberExtras, booleanExtras, jsonExtras, customData);
Map<String, JsonPrimitive> customData,
Map<String, JsonArray> jsonArrayExtras) {
super(alert, extras, numberExtras, booleanExtras, jsonExtras, customData, jsonArrayExtras);

this.sound = sound;
this.badge = badge;
Expand Down Expand Up @@ -224,7 +226,7 @@ public Builder setThreadId(String threadId) {
public IosNotification build() {
return new IosNotification(alert, sound, badge, contentAvailable,
soundDisabled, badgeDisabled, category, mutableContent, threadId,
extrasBuilder, numberExtrasBuilder, booleanExtrasBuilder, jsonExtrasBuilder, super.customData);
extrasBuilder, numberExtrasBuilder, booleanExtrasBuilder, jsonExtrasBuilder, super.customData, jsonArrayExtrasBuilder);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
Expand All @@ -26,29 +27,34 @@ public abstract class PlatformNotification implements PushModel {
private final Map<String, Boolean> booleanExtras;
private final Map<String, JsonObject> jsonExtras;
private final Map<String, JsonPrimitive> customData;

private final Map<String, JsonArray> jsonArrayExtras;

public PlatformNotification(Object alert, Map<String, String> extras,
Map<String, Number> numberExtras,
Map<String, Boolean> booleanExtras,
Map<String, JsonObject> jsonExtras) {
Map<String, JsonObject> jsonExtras,
Map<String, JsonArray> jsonArrayExtras) {
this.alert = alert;
this.extras = extras;
this.numberExtras = numberExtras;
this.booleanExtras = booleanExtras;
this.jsonExtras = jsonExtras;
this.jsonArrayExtras = jsonArrayExtras;
customData = new LinkedHashMap<>();
}

public PlatformNotification(Object alert, Map<String, String> extras,
Map<String, Number> numberExtras,
Map<String, Boolean> booleanExtras,
Map<String, JsonObject> jsonExtras,
Map<String, JsonPrimitive> customData) {
Map<String, JsonPrimitive> customData,
Map<String, JsonArray> jsonArrayExtras) {
this.alert = alert;
this.extras = extras;
this.numberExtras = numberExtras;
this.booleanExtras = booleanExtras;
this.jsonExtras = jsonExtras;
this.jsonArrayExtras = jsonArrayExtras;
this.customData = customData;
}

Expand All @@ -67,7 +73,7 @@ public JsonElement toJSON() {
}

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

Expand Down Expand Up @@ -107,8 +113,17 @@ public JsonElement toJSON() {
}
}
}
if (null != jsonArrayExtras) {
JsonArray value = null;
for (String key : jsonArrayExtras.keySet()) {
value = jsonArrayExtras.get(key);
if (null != value) {
extrasObject.add(key, value);
}
}
}

if (null != extras || null != numberExtras || null != booleanExtras || null != jsonExtras) {
if (null != extras || null != numberExtras || null != booleanExtras || null != jsonExtras || null != jsonArrayExtras) {
json.add(EXTRAS, extrasObject);
}

Expand Down Expand Up @@ -140,7 +155,8 @@ protected abstract static class Builder<T extends PlatformNotification, B extend
protected Map<String, Boolean> booleanExtrasBuilder;
protected Map<String, JsonObject> jsonExtrasBuilder;
protected Map<String, JsonPrimitive> customData;

protected Map<String, JsonArray> jsonArrayExtrasBuilder;

public Builder () {
customData = new LinkedHashMap<>();
theBuilder = getThis();
Expand Down Expand Up @@ -217,6 +233,19 @@ public B addExtra(String key, JsonObject value) {
return theBuilder;
}

public B addExtra(String key, JsonArray value) {
Preconditions.checkArgument(! (null == key), "Key should not be null.");
if (null == value) {
LOG.debug("Extra value is null, throw away it.");
return theBuilder;
}
if (null == jsonArrayExtrasBuilder) {
jsonArrayExtrasBuilder = new HashMap<String, JsonArray>();
}
jsonArrayExtrasBuilder.put(key, value);
return theBuilder;
}

public B addCustom(Map<String, String> extras) {
for (Map.Entry<String, String> entry : extras.entrySet()) {
customData.put(entry.getKey(), new JsonPrimitive(entry.getValue()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.jpush.api.push.model.notification;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
Expand All @@ -20,8 +21,9 @@ private WinphoneNotification(Object alert, String title, String openPage,
Map<String, Number> numberExtras,
Map<String, Boolean> booleanExtras,
Map<String, JsonObject> jsonExtras,
Map<String, JsonPrimitive> customData) {
super(alert, extras, numberExtras, booleanExtras, jsonExtras, customData);
Map<String, JsonPrimitive> customData,
Map<String, JsonArray> jsonArrayExtras) {
super(alert, extras, numberExtras, booleanExtras, jsonExtras, customData, jsonArrayExtras);

this.title = title;
this.openPage = openPage;
Expand Down Expand Up @@ -84,7 +86,7 @@ public Builder setAlert(Object alert) {

public WinphoneNotification build() {
return new WinphoneNotification(alert, title, openPage,
extrasBuilder, numberExtrasBuilder, booleanExtrasBuilder, jsonExtrasBuilder, super.customData);
extrasBuilder, numberExtrasBuilder, booleanExtrasBuilder, jsonExtrasBuilder, super.customData, jsonArrayExtrasBuilder);
}
}
}