|
| 1 | +package cn.jpush.api.push.model; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import com.google.gson.JsonElement; |
| 7 | +import com.google.gson.JsonNull; |
| 8 | +import com.google.gson.JsonObject; |
| 9 | +import com.google.gson.JsonPrimitive; |
| 10 | + |
| 11 | +import cn.jiguang.common.utils.Preconditions; |
| 12 | + |
| 13 | + |
| 14 | +public class Notification3rd implements PushModel{ |
| 15 | + private static final String TITLE = "title"; |
| 16 | + private static final String CONTENT = "content"; |
| 17 | + private static final String CHANNEL_ID = "channel_id"; |
| 18 | + private static final String URI_ACTIVITY = "uri_activity"; |
| 19 | + private static final String URI_ACTION = "uri_action"; |
| 20 | + private static final String BADGE_ADD_NUM = "badge_add_num"; |
| 21 | + private static final String BADGE_CLASS = "badge_class"; |
| 22 | + private static final String SOUND = "sound"; |
| 23 | + private static final String EXTRAS = "extras"; |
| 24 | + |
| 25 | + |
| 26 | + private final String title; |
| 27 | + private final String content; |
| 28 | + private final String channel_id; |
| 29 | + private final String uri_activity; |
| 30 | + private final String uri_action; |
| 31 | + private final int badge_add_num; |
| 32 | + private final String badge_class; |
| 33 | + private final String sound; |
| 34 | + private final Map<String, String> extras; |
| 35 | + private final Map<String, Number> numberExtras; |
| 36 | + private final Map<String, Boolean> booleanExtras; |
| 37 | + private final Map<String, JsonObject> jsonExtras; |
| 38 | + |
| 39 | + private Notification3rd(String title, String content, String channel_id, |
| 40 | + String uri_activity, String uri_action, int badge_add_num, |
| 41 | + String badge_class, String sound, |
| 42 | + Map<String, String> extras, |
| 43 | + Map<String, Number> numberExtras, |
| 44 | + Map<String, Boolean> booleanExtras, |
| 45 | + Map<String, JsonObject> jsonExtras) { |
| 46 | + this.title = title; |
| 47 | + this.content = content; |
| 48 | + this.channel_id = channel_id; |
| 49 | + this.uri_activity = uri_activity; |
| 50 | + this.uri_action = uri_action; |
| 51 | + this.badge_add_num = badge_add_num; |
| 52 | + this.badge_class = badge_class; |
| 53 | + this.sound = sound; |
| 54 | + this.extras = extras; |
| 55 | + this.numberExtras = numberExtras; |
| 56 | + this.booleanExtras = booleanExtras; |
| 57 | + this.jsonExtras = jsonExtras; |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + public static Builder newBuilder() { return new Builder(); } |
| 62 | + |
| 63 | + @Override |
| 64 | + public JsonElement toJSON() { |
| 65 | + JsonObject json = new JsonObject(); |
| 66 | + |
| 67 | + if (null != title) { |
| 68 | + json.addProperty("title", title); |
| 69 | + } |
| 70 | + |
| 71 | + //如果必填,该怎么判定 |
| 72 | + json.addProperty("content", content); |
| 73 | + |
| 74 | + if (null != channel_id) { |
| 75 | + json.addProperty("channel_id", channel_id); |
| 76 | + } |
| 77 | + |
| 78 | + if (null != uri_activity) { |
| 79 | + json.addProperty("uri_activity", uri_activity); |
| 80 | + } |
| 81 | + |
| 82 | + if (null != uri_action) { |
| 83 | + json.addProperty("uri_action", uri_action); |
| 84 | + } |
| 85 | + |
| 86 | + if (0 != badge_add_num) { |
| 87 | + json.addProperty("badge_add_num", badge_add_num); |
| 88 | + } |
| 89 | + |
| 90 | + if (null != badge_class) { |
| 91 | + json.addProperty("badge_class", badge_class); |
| 92 | + } |
| 93 | + |
| 94 | + if (null != sound) { |
| 95 | + json.addProperty("sound", sound); |
| 96 | + } |
| 97 | + |
| 98 | + JsonObject extrasObject = null; |
| 99 | + if (null != extras || null != numberExtras || null != booleanExtras || null != jsonExtras) { |
| 100 | + extrasObject = new JsonObject(); |
| 101 | + } |
| 102 | + |
| 103 | + if (null != extras) { |
| 104 | + String value = null; |
| 105 | + for (String key : extras.keySet()) { |
| 106 | + value = extras.get(key); |
| 107 | + if (null != value) { |
| 108 | + extrasObject.add(key, new JsonPrimitive(value)); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + if (null != numberExtras) { |
| 113 | + Number value = null; |
| 114 | + for (String key : numberExtras.keySet()) { |
| 115 | + value = numberExtras.get(key); |
| 116 | + if (null != value) { |
| 117 | + extrasObject.add(key, new JsonPrimitive(value)); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + if (null != booleanExtras) { |
| 122 | + Boolean value = null; |
| 123 | + for (String key : booleanExtras.keySet()) { |
| 124 | + value = booleanExtras.get(key); |
| 125 | + if (null != value) { |
| 126 | + extrasObject.add(key, new JsonPrimitive(value)); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + if (null != jsonExtras) { |
| 131 | + JsonObject value = null; |
| 132 | + for (String key : jsonExtras.keySet()) { |
| 133 | + value = jsonExtras.get(key); |
| 134 | + if (null != value) { |
| 135 | + extrasObject.add(key, value); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + if (null != extras || null != numberExtras || null != booleanExtras || null != jsonExtras) { |
| 141 | + json.add("extras", extrasObject); |
| 142 | + } |
| 143 | + |
| 144 | + return json; |
| 145 | + } |
| 146 | + |
| 147 | + public static class Builder{ |
| 148 | + private String title; |
| 149 | + private String content; |
| 150 | + private String channel_id; |
| 151 | + private String uri_activity; |
| 152 | + private String uri_action; |
| 153 | + private int badge_add_num; |
| 154 | + private String badge_class; |
| 155 | + private String sound; |
| 156 | + protected Map<String, String> extrasBuilder; |
| 157 | + protected Map<String, Number> numberExtrasBuilder; |
| 158 | + protected Map<String, Boolean> booleanExtrasBuilder; |
| 159 | + protected Map<String, JsonObject> jsonExtrasBuilder; |
| 160 | + |
| 161 | + public Builder setTitle(String title) { |
| 162 | + this.title = title; |
| 163 | + return this; |
| 164 | + } |
| 165 | + |
| 166 | + public Builder setContent(String content) { |
| 167 | + this.content = content; |
| 168 | + return this; |
| 169 | + } |
| 170 | + |
| 171 | + public Builder setChannelId(String channel_id) { |
| 172 | + this.channel_id = channel_id; |
| 173 | + return this; |
| 174 | + } |
| 175 | + |
| 176 | + public Builder setUriActivity(String uri_activity) { |
| 177 | + this.uri_activity = uri_activity; |
| 178 | + return this; |
| 179 | + } |
| 180 | + |
| 181 | + public Builder setUriAction(String uri_action) { |
| 182 | + this.uri_action = uri_action; |
| 183 | + return this; |
| 184 | + } |
| 185 | + |
| 186 | + public Builder setBadgeAddNum(int badge_add_num) { |
| 187 | + this.badge_add_num = badge_add_num; |
| 188 | + return this; |
| 189 | + } |
| 190 | + |
| 191 | + public Builder setBadgeClass(String badge_class) { |
| 192 | + this.badge_class = badge_class; |
| 193 | + return this; |
| 194 | + } |
| 195 | + |
| 196 | + public Builder setSound(String sound) { |
| 197 | + this.sound = sound; |
| 198 | + return this; |
| 199 | + } |
| 200 | + |
| 201 | + public Builder addExtra(String key, String value) { |
| 202 | + Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null."); |
| 203 | + if (null == extrasBuilder) { |
| 204 | + extrasBuilder = new HashMap<String, String>(); |
| 205 | + } |
| 206 | + extrasBuilder.put(key, value); |
| 207 | + return this; |
| 208 | + } |
| 209 | + |
| 210 | + public Builder addExtra(Map<String, String> extras) { |
| 211 | + Preconditions.checkArgument(! (null == extras), "extras should not be null."); |
| 212 | + if (null == extrasBuilder) { |
| 213 | + extrasBuilder = new HashMap<String, String>(); |
| 214 | + } |
| 215 | + for (String key : extras.keySet()) { |
| 216 | + extrasBuilder.put(key, extras.get(key)); |
| 217 | + } |
| 218 | + return this; |
| 219 | + } |
| 220 | + |
| 221 | + public Builder addExtra(String key, Number value) { |
| 222 | + Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null."); |
| 223 | + if (null == numberExtrasBuilder) { |
| 224 | + numberExtrasBuilder = new HashMap<String, Number>(); |
| 225 | + } |
| 226 | + numberExtrasBuilder.put(key, value); |
| 227 | + return this; |
| 228 | + } |
| 229 | + |
| 230 | + public Builder addExtra(String key, Boolean value) { |
| 231 | + Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null."); |
| 232 | + if (null == booleanExtrasBuilder) { |
| 233 | + booleanExtrasBuilder = new HashMap<String, Boolean>(); |
| 234 | + } |
| 235 | + booleanExtrasBuilder.put(key, value); |
| 236 | + return this; |
| 237 | + } |
| 238 | + |
| 239 | + public Builder addExtra(String key, JsonObject value) { |
| 240 | + Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null."); |
| 241 | + if (null == jsonExtrasBuilder) { |
| 242 | + jsonExtrasBuilder = new HashMap<String, JsonObject>(); |
| 243 | + } |
| 244 | + jsonExtrasBuilder.put(key, value); |
| 245 | + return this; |
| 246 | + } |
| 247 | + |
| 248 | + public Notification3rd build() { |
| 249 | + Preconditions.checkArgument(content != null && content != "", "content should not be null or empty"); |
| 250 | + |
| 251 | + return new Notification3rd(title, content, channel_id, uri_activity, uri_action, badge_add_num, |
| 252 | + badge_class, sound, extrasBuilder, numberExtrasBuilder, booleanExtrasBuilder, jsonExtrasBuilder); |
| 253 | + } |
| 254 | + } |
| 255 | +} |
0 commit comments