Skip to content

Commit 307b8c0

Browse files
committed
Added JSONObject constructors to OSNotification* classes
* This fulfills request issue #198
1 parent d89f9b5 commit 307b8c0

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

OneSignalSDK/onesignal/src/main/java/com/onesignal/OSNotification.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
package com.onesignal;
2929

30+
import java.util.ArrayList;
3031
import java.util.List;
3132

3233
import org.json.JSONArray;
@@ -45,6 +46,26 @@ public enum DisplayType {
4546
// Notification was silent and not displayed.
4647
None
4748
}
49+
50+
public OSNotification() {
51+
}
52+
53+
public OSNotification(JSONObject jsonObject) {
54+
isAppInFocus = jsonObject.optBoolean("isAppInFocus");
55+
shown = jsonObject.optBoolean("shown", shown);
56+
androidNotificationId = jsonObject.optInt("androidNotificationId");
57+
displayType = DisplayType.values()[jsonObject.optInt("displayType")];
58+
59+
if (jsonObject.has("groupedNotifications")) {
60+
JSONArray jsonArray = jsonObject.optJSONArray("groupedNotifications");
61+
groupedNotifications = new ArrayList<>();
62+
for (int i = 0; i < jsonArray.length(); i++)
63+
groupedNotifications.add(new OSNotificationPayload(jsonArray.optJSONObject(i)));
64+
}
65+
66+
if (jsonObject.has("payload"))
67+
payload = new OSNotificationPayload(jsonObject.optJSONObject("payload"));
68+
}
4869

4970
// Is app Active.
5071
public boolean isAppInFocus;

OneSignalSDK/onesignal/src/main/java/com/onesignal/OSNotificationPayload.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.json.JSONArray;
3131
import org.json.JSONObject;
3232

33+
import java.util.ArrayList;
3334
import java.util.List;
3435

3536
// Notification properties received from OneSignal.
@@ -54,11 +55,52 @@ public class OSNotificationPayload {
5455
public int priority;
5556
public String rawPayload;
5657

58+
public OSNotificationPayload() {
59+
}
60+
61+
public OSNotificationPayload(JSONObject jsonObject) {
62+
notificationID = jsonObject.optString("notificationID");
63+
title = jsonObject.optString("title");
64+
65+
body = jsonObject.optString("body");
66+
additionalData = jsonObject.optJSONObject("additionalData");
67+
smallIcon = jsonObject.optString("smallIcon");
68+
largeIcon = jsonObject.optString("largeIcon");
69+
bigPicture = jsonObject.optString("bigPicture");
70+
smallIconAccentColor = jsonObject.optString("smallIconAccentColor");
71+
launchURL = jsonObject.optString("launchURL");
72+
sound = jsonObject.optString("sound");
73+
ledColor = jsonObject.optString("ledColor");
74+
lockScreenVisibility = jsonObject.optInt("lockScreenVisibility");
75+
groupKey = jsonObject.optString("groupKey");
76+
groupMessage = jsonObject.optString("groupMessage");
77+
78+
if (jsonObject.has("actionButtons")) {
79+
actionButtons = new ArrayList<>();
80+
JSONArray jsonArray = jsonObject.optJSONArray("actionButtons");
81+
for (int i = 0; i < jsonArray.length(); i++)
82+
actionButtons.add(new ActionButton(jsonArray.optJSONObject(i)));
83+
}
84+
85+
fromProjectNumber = jsonObject.optString("fromProjectNumber");
86+
collapseId = jsonObject.optString("collapseId");
87+
priority = jsonObject.optInt("priority");
88+
rawPayload = jsonObject.optString("rawPayload");
89+
}
90+
5791
public static class ActionButton {
5892
public String id;
5993
public String text;
6094
public String icon;
61-
95+
96+
public ActionButton() {}
97+
98+
public ActionButton(JSONObject jsonObject) {
99+
id = jsonObject.optString("id");
100+
text = jsonObject.optString("text");
101+
icon = jsonObject.optString("icon");
102+
}
103+
62104
public JSONObject toJSONObject() {
63105
JSONObject json = new JSONObject();
64106
try {

0 commit comments

Comments
 (0)