admin
2022-01-12 4a7367a869ef12375ea6678ca44e102b8919c624
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package cn.jpush.api.push.model.notification;
 
import java.util.*;
 
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
 
import cn.jiguang.common.utils.Preconditions;
import cn.jpush.api.push.model.PushModel;
 
public class Notification implements PushModel {
    private static final String AI_OPPORTUNITY = "ai_opportunity";
    private static final String VOIP = "voip";
 
    // default is false
    private boolean aiOpportunity;
    private final Object alert;
    private final Set<PlatformNotification> notifications;
    private final Map<String, String> voip;
    private final Map<String, Number> numberVoip;
    private final Map<String, Boolean> booleanVoip;
    private final Map<String, JsonObject> jsonVoip;
    
    private Notification(boolean aiOpportunity, Object alert, Set<PlatformNotification> notifications,
                         Map<String, String> voip, Map<String, Number> numberVoip, Map<String, Boolean> booleanVoip,
                         Map<String, JsonObject> jsonVoip) {
        this.aiOpportunity = aiOpportunity;
        this.alert = alert;
        this.notifications = notifications;
        this.voip = voip;
        this.numberVoip = numberVoip;
        this.booleanVoip = booleanVoip;
        this.jsonVoip = jsonVoip;
    }
    
    public static Builder newBuilder() {
        return new Builder();
    }
 
    public static Notification aiOpportunity(boolean ai_opportunity) {
        return newBuilder().setAiOpportunity(ai_opportunity).build();
    }
    
    /**
     * Quick set all platform alert. 
     * Platform notification can override this alert. 
     * 
     * @param alert Notification alert
     * @return first level notification object
     */
    public static Notification alert(Object alert) {
        return newBuilder().setAlert(alert).build();
    }
 
    public static Notification android(String alert, String title, Map<String, String> extras) {
        return newBuilder()
                .addPlatformNotification(AndroidNotification.newBuilder()
                    .setAlert(alert)
                    .setTitle(title)
                    .addExtras(extras)
                    .build())
                .build();
    }
 
    public static Notification ios(Object alert, Map<String, String> extras) {
        return newBuilder()
                .addPlatformNotification(IosNotification.newBuilder()
                    .setAlert(alert)
                    .addExtras(extras)
                    .build())
                .build();
    }
 
    public static Notification ios_auto_badge() {
        return newBuilder()
                .addPlatformNotification(IosNotification.newBuilder()
                    .setAlert("")
                    .autoBadge()
                    .build())
                .build();
    }
 
    public static Notification ios_set_badge(int badge) {
        return newBuilder()
                .addPlatformNotification(IosNotification.newBuilder()
                    .setAlert("")
                    .setBadge(badge)
                    .build())
                .build();
    }
 
    public static Notification ios_incr_badge(int badge) {
        return newBuilder()
                .addPlatformNotification(IosNotification.newBuilder()
                    .setAlert("")
                    .incrBadge(badge)
                    .build())
                .build();
    }
 
    public static Notification winphone(String alert, Map<String, String> extras) {
        return newBuilder()
                .addPlatformNotification(WinphoneNotification.newBuilder()
                    .setAlert(alert)
                    .addExtras(extras)
                    .build())
                .build();
    }
 
    public JsonElement toJSON() {
        JsonObject json = new JsonObject();
 
        json.addProperty(AI_OPPORTUNITY, aiOpportunity);
 
        if (null != 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) {
                if (this.alert != null && pn.getAlert() == null) {
                    pn.setAlert(this.alert);
                }
                
                Preconditions.checkArgument(! (null == pn.getAlert()), 
                        "For any platform notification, alert field is needed. It can be empty string.");
 
                json.add(pn.getPlatform(), pn.toJSON());
            }
        }
 
 
        JsonObject extrasObject = null;
        if (null != voip || null != numberVoip || null != booleanVoip || null != jsonVoip) {
            extrasObject = new JsonObject();
        }
 
        if (null != voip) {
            String value = null;
            for (String key : voip.keySet()) {
                value = voip.get(key);
                if (null != value) {
                    extrasObject.add(key, new JsonPrimitive(value));
                }
            }
        }
        if (null != numberVoip) {
            Number value = null;
            for (String key : numberVoip.keySet()) {
                value = numberVoip.get(key);
                if (null != value) {
                    extrasObject.add(key, new JsonPrimitive(value));
                }
            }
        }
        if (null != booleanVoip) {
            Boolean value = null;
            for (String key : booleanVoip.keySet()) {
                value = booleanVoip.get(key);
                if (null != value) {
                    extrasObject.add(key, new JsonPrimitive(value));
                }
            }
        }
        if (null != jsonVoip) {
            JsonObject value = null;
            for (String key : jsonVoip.keySet()) {
                value = jsonVoip.get(key);
                if (null != value) {
                    extrasObject.add(key, value);
                }
            }
        }
 
        if (null != voip || null != numberVoip || null != booleanVoip || null != jsonVoip) {
            json.add(VOIP, extrasObject);
        }
 
        return json;
    }
 
    public static class Builder {
        private boolean aiOpportunity;
        private Object alert;
        private Set<PlatformNotification> builder;
        protected Map<String, String> voipBuilder;
        protected  Map<String, Number> numberVoipBuilder;
        protected  Map<String, Boolean> booleanVoipBuilder;
        protected  Map<String, JsonObject> jsonVoipBuilder;
 
 
 
        public Builder setAiOpportunity(boolean aiOpportunity) {
            this.aiOpportunity = aiOpportunity;
            return this;
        }
 
        public Builder setAlert(Object alert) {
            this.alert = alert;
            return this;
        }
        
        public Builder addPlatformNotification(PlatformNotification notification) {
            if (null == builder) {
                builder = new HashSet<PlatformNotification>();
            }
            builder.add(notification);
            return this;
        }
 
        public Builder addVoip(String key, String value) {
            Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null.");
            if (null == voipBuilder) {
                voipBuilder = new HashMap<String, String>();
            }
            voipBuilder.put(key, value);
            return this;
        }
 
        public Builder addVoip(Map<String, String> voip) {
            Preconditions.checkArgument(! (null == voip), "extras should not be null.");
            if (null == voipBuilder) {
                voipBuilder = new HashMap<String, String>();
            }
            for (String key : voip.keySet()) {
                voipBuilder.put(key, voip.get(key));
            }
            return this;
        }
 
        public Builder addVoip(String key, Number value) {
            Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null.");
            if (null == numberVoipBuilder) {
                numberVoipBuilder = new HashMap<String, Number>();
            }
            numberVoipBuilder.put(key, value);
            return this;
        }
 
        public Builder addVoip(String key, Boolean value) {
            Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null.");
            if (null == booleanVoipBuilder) {
                booleanVoipBuilder = new HashMap<String, Boolean>();
            }
            booleanVoipBuilder.put(key, value);
            return this;
        }
 
        public Builder addVoip(String key, JsonObject value) {
            Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null.");
            if (null == jsonVoipBuilder) {
                jsonVoipBuilder = new HashMap<String, JsonObject>();
            }
            jsonVoipBuilder.put(key, value);
            return this;
        }
        
        public Notification build() {
            Preconditions.checkArgument(! (null == builder && null == alert),
                    "No notification payload is set.");
            return new Notification(aiOpportunity, alert, builder, voipBuilder, numberVoipBuilder, booleanVoipBuilder
            , jsonVoipBuilder);
        }
    }
}