/
NotificationSerializer.java
146 lines (134 loc) · 5.81 KB
/
NotificationSerializer.java
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
package expo.modules.notifications.notifications;
import android.content.ContentResolver;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.unimodules.core.arguments.MapArguments;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import androidx.annotation.Nullable;
import expo.modules.notifications.notifications.interfaces.NotificationTrigger;
import expo.modules.notifications.notifications.model.Notification;
import expo.modules.notifications.notifications.model.NotificationContent;
import expo.modules.notifications.notifications.model.NotificationRequest;
import expo.modules.notifications.notifications.model.NotificationResponse;
import expo.modules.notifications.notifications.model.triggers.FirebaseNotificationTrigger;
import expo.modules.notifications.notifications.triggers.DateTrigger;
import expo.modules.notifications.notifications.triggers.TimeIntervalTrigger;
public class NotificationSerializer {
public static Bundle toBundle(NotificationResponse response) {
Bundle serializedResponse = new Bundle();
serializedResponse.putString("actionIdentifier", response.getActionIdentifier());
serializedResponse.putBundle("notification", toBundle(response.getNotification()));
return serializedResponse;
}
public static Bundle toBundle(Notification notification) {
Bundle serializedNotification = new Bundle();
serializedNotification.putBundle("request", toBundle(notification.getNotificationRequest()));
serializedNotification.putLong("date", notification.getDate().getTime());
return serializedNotification;
}
public static Bundle toBundle(NotificationRequest request) {
Bundle serializedRequest = new Bundle();
serializedRequest.putString("identifier", request.getIdentifier());
serializedRequest.putBundle("content", toBundle(request.getContent()));
serializedRequest.putBundle("trigger", toBundle(request.getTrigger()));
return serializedRequest;
}
public static Bundle toBundle(NotificationContent content) {
Bundle serializedContent = new Bundle();
serializedContent.putString("title", content.getTitle());
serializedContent.putString("subtitle", content.getSubtitle());
serializedContent.putString("body", content.getText());
if (content.getColor() != null) {
serializedContent.putString("color", String.format("#%08X", content.getColor().intValue()));
}
serializedContent.putBundle("data", toBundle(content.getBody()));
if (content.getBadgeCount() != null) {
serializedContent.putInt("badge", content.getBadgeCount().intValue());
} else {
serializedContent.putString("badge", null);
}
if (content.shouldPlayDefaultSound()) {
serializedContent.putString("sound", "default");
} else if (content.getSound() != null) {
serializedContent.putString("sound", "custom");
} else {
serializedContent.putString("sound", null);
}
if (content.getPriority() != null) {
serializedContent.putString("priority", content.getPriority().getEnumValue());
}
if (content.getVibrationPattern() != null) {
double[] serializedVibrationPattern = new double[content.getVibrationPattern().length];
for (int i = 0; i < serializedVibrationPattern.length; i++) {
serializedVibrationPattern[i] = content.getVibrationPattern()[i];
}
serializedContent.putDoubleArray("vibrationPattern", serializedVibrationPattern);
}
return serializedContent;
}
private static Bundle toBundle(@Nullable JSONObject notification) {
if (notification == null) {
return null;
}
Map<String, Object> notificationMap = new HashMap<>(notification.length());
Iterator<String> keyIterator = notification.keys();
while (keyIterator.hasNext()) {
String key = keyIterator.next();
Object value = notification.opt(key);
if (value instanceof JSONObject) {
notificationMap.put(key, toBundle((JSONObject) value));
} else if (value instanceof JSONArray) {
notificationMap.put(key, toList((JSONArray) value));
} else if (JSONObject.NULL.equals(value)) {
notificationMap.put(key, null);
} else {
notificationMap.put(key, value);
}
}
return new MapArguments(notificationMap).toBundle();
}
private static List toList(JSONArray array) {
List<Object> result = new ArrayList<>(array.length());
for (int i = 0; i < array.length(); i++) {
if (array.isNull(i)) {
result.add(null);
} else if (array.optJSONObject(i) != null) {
result.add(toBundle(array.optJSONObject(i)));
} else if (array.optJSONArray(i) != null) {
result.add(toList(array.optJSONArray(i)));
} else {
result.add(array.opt(i));
}
}
return result;
}
private static Bundle toBundle(@Nullable NotificationTrigger trigger) {
if (trigger == null) {
return null;
}
Bundle bundle = new Bundle();
if (trigger instanceof FirebaseNotificationTrigger) {
bundle.putString("type", "push");
bundle.putBundle("remoteMessage", RemoteMessageSerializer.toBundle(((FirebaseNotificationTrigger) trigger).getRemoteMessage()));
} else if (trigger instanceof TimeIntervalTrigger) {
bundle.putString("type", "timeInterval");
bundle.putBoolean("repeats", ((TimeIntervalTrigger) trigger).isRepeating());
bundle.putLong("seconds", ((TimeIntervalTrigger) trigger).getTimeInterval());
} else if (trigger instanceof DateTrigger) {
bundle.putString("type", "date");
bundle.putBoolean("repeats", false);
bundle.putLong("value", ((DateTrigger) trigger).getTriggerDate().getTime());
} else {
bundle.putString("type", "unknown");
}
return bundle;
}
}