-
Notifications
You must be signed in to change notification settings - Fork 237
Expand file tree
/
Copy pathOneSignalSerializer.java
More file actions
296 lines (228 loc) · 11.4 KB
/
Copy pathOneSignalSerializer.java
File metadata and controls
296 lines (228 loc) · 11.4 KB
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package com.onesignal.flutter;
import com.onesignal.inAppMessages.IInAppMessage;
import com.onesignal.inAppMessages.IInAppMessageClickEvent;
import com.onesignal.inAppMessages.IInAppMessageClickResult;
import com.onesignal.inAppMessages.IInAppMessageDidDismissEvent;
import com.onesignal.inAppMessages.IInAppMessageDidDisplayEvent;
import com.onesignal.inAppMessages.IInAppMessageWillDismissEvent;
import com.onesignal.inAppMessages.IInAppMessageWillDisplayEvent;
import com.onesignal.notifications.IActionButton;
import com.onesignal.notifications.INotification;
import com.onesignal.notifications.INotificationClickEvent;
import com.onesignal.notifications.INotificationClickResult;
import com.onesignal.notifications.INotificationWillDisplayEvent;
import com.onesignal.user.state.UserChangedState;
import com.onesignal.user.state.UserState;
import com.onesignal.user.subscriptions.PushSubscriptionChangedState;
import com.onesignal.user.subscriptions.PushSubscriptionState;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
class OneSignalSerializer {
static HashMap<String, Object> convertNotificationToMap(INotification notification) throws JSONException {
HashMap<String, Object> hash = new HashMap<>();
hash.put("androidNotificationId", notification.getAndroidNotificationId());
if (notification.getGroupedNotifications() != null) {
hash.put("groupKey", notification.getGroupKey());
hash.put("groupMessage", notification.getGroupMessage());
hash.put("groupedNotifications", notification.getGroupedNotifications());
}
hash.put("notificationId", notification.getNotificationId());
hash.put("title", notification.getTitle());
if (notification.getBody() != null) hash.put("body", notification.getBody());
if (notification.getSmallIcon() != null) hash.put("smallIcon", notification.getSmallIcon());
if (notification.getLargeIcon() != null) hash.put("largeIcon", notification.getLargeIcon());
if (notification.getBigPicture() != null) hash.put("bigPicture", notification.getBigPicture());
if (notification.getSmallIconAccentColor() != null)
hash.put("smallIconAccentColor", notification.getSmallIconAccentColor());
if (notification.getLaunchURL() != null) hash.put("launchUrl", notification.getLaunchURL());
if (notification.getSound() != null) hash.put("sound", notification.getSound());
if (notification.getLedColor() != null) hash.put("ledColor", notification.getLedColor());
hash.put("lockScreenVisibility", notification.getLockScreenVisibility());
if (notification.getGroupKey() != null) hash.put("groupKey", notification.getGroupKey());
if (notification.getGroupMessage() != null) hash.put("groupMessage", notification.getGroupMessage());
if (notification.getFromProjectNumber() != null)
hash.put("fromProjectNumber", notification.getFromProjectNumber());
if (notification.getCollapseId() != null) hash.put("collapseId", notification.getCollapseId());
hash.put("priority", notification.getPriority());
if (notification.getAdditionalData() != null
&& notification.getAdditionalData().length() > 0)
hash.put("additionalData", convertJSONObjectToHashMap(notification.getAdditionalData()));
if (notification.getActionButtons() != null) {
hash.put("buttons", convertActionButtonsToMap(notification.getActionButtons()));
}
hash.put("rawPayload", notification.getRawPayload());
return hash;
}
static List<HashMap<String, Object>> convertActionButtonsToMap(List<IActionButton> actionButtons) {
List<HashMap<String, Object>> convertedList = new ArrayList<HashMap<String, Object>>();
for (IActionButton actionButton : actionButtons) {
HashMap<String, Object> hash = new HashMap<>();
hash.put("id", actionButton.getId());
hash.put("text", actionButton.getText());
hash.put("icon", actionButton.getIcon());
convertedList.add(hash);
}
return convertedList;
}
static HashMap<String, Object> convertNotificationWillDisplayEventToMap(INotificationWillDisplayEvent event)
throws JSONException {
HashMap<String, Object> hash = new HashMap<>();
hash.put("notification", convertNotificationToMap(event.getNotification()));
return hash;
}
private static HashMap<String, Object> convertNotificationClickResultToMap(INotificationClickResult result)
throws JSONException {
HashMap<String, Object> hash = new HashMap<>();
hash.put("action_id", result.getActionId());
hash.put("url", result.getUrl());
return hash;
}
static HashMap<String, Object> convertNotificationClickEventToMap(INotificationClickEvent event)
throws JSONException {
HashMap<String, Object> hash = new HashMap<>();
hash.put("notification", convertNotificationToMap(event.getNotification()));
hash.put("result", convertNotificationClickResultToMap(event.getResult()));
return hash;
}
static HashMap<String, Object> convertInAppMessageClickEventToMap(IInAppMessageClickEvent event)
throws JSONException {
HashMap<String, Object> hash = new HashMap<>();
hash.put("message", convertInAppMessageToMap(event.getMessage()));
hash.put("result", convertInAppMessageClickResultToMap(event.getResult()));
return hash;
}
static HashMap<String, Object> convertInAppMessageClickResultToMap(IInAppMessageClickResult result)
throws JSONException {
HashMap<String, Object> hash = new HashMap<>();
hash.put("action_id", result.getActionId());
hash.put("url", result.getUrl());
hash.put("closing_message", result.getClosingMessage());
return hash;
}
static HashMap<String, Object> convertInAppMessageWillDisplayEventToMap(IInAppMessageWillDisplayEvent event)
throws JSONException {
HashMap<String, Object> hash = new HashMap<>();
hash.put("message", convertInAppMessageToMap(event.getMessage()));
return hash;
}
static HashMap<String, Object> convertInAppMessageDidDisplayEventToMap(IInAppMessageDidDisplayEvent event)
throws JSONException {
HashMap<String, Object> hash = new HashMap<>();
hash.put("message", convertInAppMessageToMap(event.getMessage()));
return hash;
}
static HashMap<String, Object> convertInAppMessageWillDismissEventToMap(IInAppMessageWillDismissEvent event)
throws JSONException {
HashMap<String, Object> hash = new HashMap<>();
hash.put("message", convertInAppMessageToMap(event.getMessage()));
return hash;
}
static HashMap<String, Object> convertInAppMessageDidDismissEventToMap(IInAppMessageDidDismissEvent event)
throws JSONException {
HashMap<String, Object> hash = new HashMap<>();
hash.put("message", convertInAppMessageToMap(event.getMessage()));
return hash;
}
static HashMap<String, Object> convertInAppMessageToMap(IInAppMessage message) {
HashMap<String, Object> hash = new HashMap<>();
hash.put("message_id", message.getMessageId());
return hash;
}
static HashMap<String, Object> convertPushSubscriptionState(PushSubscriptionState state) throws JSONException {
HashMap<String, Object> hash = new HashMap<>();
hash.put("token", state.getToken());
hash.put("id", state.getId());
hash.put("optedIn", state.getOptedIn());
return hash;
}
static HashMap<String, Object> convertUserState(UserState state) throws JSONException {
HashMap<String, Object> hash = new HashMap<>();
String onesignalId = setNullIfEmpty(state.getOnesignalId());
String externalId = setNullIfEmpty(state.getExternalId());
hash.put("onesignalId", onesignalId);
hash.put("externalId", externalId);
return hash;
}
static HashMap<String, Object> convertOnPushSubscriptionChange(PushSubscriptionChangedState changedState)
throws JSONException {
HashMap<String, Object> hash = new HashMap<>();
hash.put("current", convertPushSubscriptionState(changedState.getCurrent()));
hash.put("previous", convertPushSubscriptionState(changedState.getPrevious()));
return hash;
}
static HashMap<String, Object> convertOnUserStateChange(UserChangedState changedState) throws JSONException {
HashMap<String, Object> hash = new HashMap<>();
hash.put("current", convertUserState(changedState.getCurrent()));
return hash;
}
static HashMap<String, Object> convertJSONObjectToHashMap(JSONObject object) throws JSONException {
HashMap<String, Object> hash = new HashMap<>();
if (object == null || object == JSONObject.NULL) return hash;
Iterator<String> keys = object.keys();
while (keys.hasNext()) {
String key = keys.next();
if (object.isNull(key)) continue;
Object val = object.get(key);
if (val instanceof JSONArray) {
val = convertJSONArrayToList((JSONArray) val);
} else if (val instanceof JSONObject) {
val = convertJSONObjectToHashMap((JSONObject) val);
}
hash.put(key, val);
}
return hash;
}
private static List<Object> convertJSONArrayToList(JSONArray array) throws JSONException {
List<Object> list = new ArrayList<>();
for (int i = 0; i < array.length(); i++) {
Object val = array.get(i);
if (val instanceof JSONArray) val = OneSignalSerializer.convertJSONArrayToList((JSONArray) val);
else if (val instanceof JSONObject) val = convertJSONObjectToHashMap((JSONObject) val);
list.add(val);
}
return list;
}
/** Helper method to return null value if string is empty **/
static String setNullIfEmpty(String value) {
return value.isEmpty() ? null : value;
}
/**
* Formats a properties map, recursively stripping null values.
* The native SDK doesn't accept null values in properties currently.
*/
static HashMap<String, Object> formatPropertiesMap(Map<String, Object> map) {
if (map == null) return null;
HashMap<String, Object> result = new HashMap<>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
Object value = entry.getValue();
if (value != null) {
result.put(entry.getKey(), convertValue(value));
}
}
return result;
}
@SuppressWarnings("unchecked")
private static Object convertValue(Object value) {
if (value instanceof Map) {
return formatPropertiesMap((Map<String, Object>) value);
} else if (value instanceof List) {
return stripNullValuesFromList((List<Object>) value);
}
return value;
}
private static List<Object> stripNullValuesFromList(List<Object> list) {
List<Object> result = new ArrayList<>();
for (Object item : list) {
if (item != null) {
result.add(convertValue(item));
}
}
return result;
}
}