From 3cc23fae3834f6c70629cd121c1cc3e9df6f2d4c Mon Sep 17 00:00:00 2001 From: zlay0701 <906105783@qq.com> Date: Wed, 20 Jan 2021 16:02:57 +0800 Subject: [PATCH] =?UTF-8?q?notification(Android,ios,winphone)=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E4=BC=A0=E9=80=92JsonArray=E7=B1=BB=E5=9E=8B=E6=89=A9?= =?UTF-8?q?=E5=B1=95=E5=8F=82=E6=95=B0,Message=E6=94=AF=E6=8C=81=E4=BC=A0?= =?UTF-8?q?=E9=80=92JsonArray=E7=B1=BB=E5=9E=8B=E6=89=A9=E5=B1=95=E5=8F=82?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/jpush/api/push/model/Message.java | 26 ++++++++++-- .../notification/AndroidNotification.java | 9 ++-- .../model/notification/IosNotification.java | 8 ++-- .../notification/PlatformNotification.java | 41 ++++++++++++++++--- .../notification/WinphoneNotification.java | 8 ++-- 5 files changed, 74 insertions(+), 18 deletions(-) diff --git a/src/main/java/cn/jpush/api/push/model/Message.java b/src/main/java/cn/jpush/api/push/model/Message.java index 73ff2422..ccc8f53c 100644 --- a/src/main/java/cn/jpush/api/push/model/Message.java +++ b/src/main/java/cn/jpush/api/push/model/Message.java @@ -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; @@ -25,13 +26,15 @@ public class Message implements PushModel { private final Map booleanExtras; private final Map jsonExtras; private final Map customData; + private final Map jsonArrayExtras; private Message(String title, String msgContent, String contentType, Map extras, Map numberExtras, Map booleanExtras, Map jsonExtras, - Map customData) { + Map customData, + Map jsonArrayExtras) { this.title = title; this.msgContent = msgContent; this.contentType = contentType; @@ -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() { @@ -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(); } @@ -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); } @@ -115,6 +125,7 @@ public static class Builder { private Map booleanExtrasBuilder; protected Map jsonExtrasBuilder; private Map customData; + protected Map jsonArrayExtrasBuilder; public Builder setTitle(String title) { this.title = title; @@ -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(); + } + jsonArrayExtrasBuilder.put(key, value); + return this; + } + public Builder addCustom(Map extras) { if (customData == null) { customData = new LinkedHashMap<>(); @@ -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); } } } 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 ab2407cd..4e3b4c8d 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,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; @@ -57,8 +58,9 @@ private AndroidNotification(Object alert, Map numberExtras, Map booleanExtras, Map jsonExtras, - Map customData) { - super(alert, extras, numberExtras, booleanExtras, jsonExtras, customData); + Map customData, + Map jsonArrayExtras) { + super(alert, extras, numberExtras, booleanExtras, jsonExtras, customData, jsonArrayExtras); this.title = title; this.builderId = builderId; @@ -282,7 +284,8 @@ public AndroidNotification build() { numberExtrasBuilder, booleanExtrasBuilder, jsonExtrasBuilder, - super.customData + super.customData, + jsonArrayExtrasBuilder ); } } 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 4e7cf7ca..1d7bf483 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 @@ -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; @@ -62,8 +63,9 @@ private IosNotification(Object alert, Object sound, String badge, Map numberExtras, Map booleanExtras, Map jsonExtras, - Map customData) { - super(alert, extras, numberExtras, booleanExtras, jsonExtras, customData); + Map customData, + Map jsonArrayExtras) { + super(alert, extras, numberExtras, booleanExtras, jsonExtras, customData, jsonArrayExtras); this.sound = sound; this.badge = badge; @@ -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); } } } 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 163efe3e..a3374aa9 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 @@ -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; @@ -26,16 +27,19 @@ public abstract class PlatformNotification implements PushModel { private final Map booleanExtras; private final Map jsonExtras; private final Map customData; - + private final Map jsonArrayExtras; + public PlatformNotification(Object alert, Map extras, Map numberExtras, Map booleanExtras, - Map jsonExtras) { + Map jsonExtras, + Map jsonArrayExtras) { this.alert = alert; this.extras = extras; this.numberExtras = numberExtras; this.booleanExtras = booleanExtras; this.jsonExtras = jsonExtras; + this.jsonArrayExtras = jsonArrayExtras; customData = new LinkedHashMap<>(); } @@ -43,12 +47,14 @@ public PlatformNotification(Object alert, Map extras, Map numberExtras, Map booleanExtras, Map jsonExtras, - Map customData) { + Map customData, + Map jsonArrayExtras) { this.alert = alert; this.extras = extras; this.numberExtras = numberExtras; this.booleanExtras = booleanExtras; this.jsonExtras = jsonExtras; + this.jsonArrayExtras = jsonArrayExtras; this.customData = customData; } @@ -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(); } @@ -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); } @@ -140,7 +155,8 @@ protected abstract static class Builder booleanExtrasBuilder; protected Map jsonExtrasBuilder; protected Map customData; - + protected Map jsonArrayExtrasBuilder; + public Builder () { customData = new LinkedHashMap<>(); theBuilder = getThis(); @@ -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(); + } + jsonArrayExtrasBuilder.put(key, value); + return theBuilder; + } + public B addCustom(Map extras) { for (Map.Entry entry : extras.entrySet()) { customData.put(entry.getKey(), new JsonPrimitive(entry.getValue())); 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 33003797..d854d705 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,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; @@ -20,8 +21,9 @@ private WinphoneNotification(Object alert, String title, String openPage, Map numberExtras, Map booleanExtras, Map jsonExtras, - Map customData) { - super(alert, extras, numberExtras, booleanExtras, jsonExtras, customData); + Map customData, + Map jsonArrayExtras) { + super(alert, extras, numberExtras, booleanExtras, jsonExtras, customData, jsonArrayExtras); this.title = title; this.openPage = openPage; @@ -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); } } }