Skip to content

Commit 045c34d

Browse files
committed
Deprecated stringify in favor of toJSONObject
* also added groupedNotifications to new toJSONObject method.
1 parent ed13acb commit 045c34d

File tree

5 files changed

+138
-27
lines changed

5 files changed

+138
-27
lines changed

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

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import java.util.List;
3131

32+
import org.json.JSONArray;
3233
import org.json.JSONException;
3334
import org.json.JSONObject;
3435

@@ -63,7 +64,25 @@ public enum DisplayType {
6364
// The payload will be the most recent notification received.
6465
public List<OSNotificationPayload> groupedNotifications;
6566

67+
/**
68+
* @deprecated As of release 3.4.1, replaced by {@link #toJSONObject()}
69+
*/
70+
@Deprecated
6671
public String stringify() {
72+
JSONObject mainObj = toJSONObject();
73+
74+
try {
75+
if (mainObj.has("additionalData"))
76+
mainObj.put("additionalData", mainObj.optJSONObject("additionalData").toString());
77+
}
78+
catch(JSONException e) {
79+
e.printStackTrace();
80+
}
81+
82+
return mainObj.toString();
83+
}
84+
85+
public JSONObject toJSONObject() {
6786
JSONObject mainObj = new JSONObject();
6887

6988
try {
@@ -72,33 +91,20 @@ public String stringify() {
7291
mainObj.put("androidNotificationId", androidNotificationId);
7392
mainObj.put("displayType", displayType.ordinal());
7493

75-
JSONObject pay = new JSONObject();
76-
pay.put("notificationID", payload.notificationID);
77-
pay.put("title", payload.title);
78-
pay.put("body", payload.body);
79-
if (payload.additionalData != null)
80-
pay.put("additionalData", payload.additionalData.toString());
81-
pay.put("smallIcon", payload.smallIcon);
82-
pay.put("largeIcon", payload.largeIcon);
83-
pay.put("bigPicture", payload.bigPicture);
84-
pay.put("smallIconAccentColor", payload.smallIconAccentColor);
85-
pay.put("launchURL", payload.launchURL);
86-
pay.put("sound", payload.sound);
87-
pay.put("ledColor", payload.ledColor);
88-
pay.put("lockScreenVisibility", payload.lockScreenVisibility);
89-
pay.put("groupKey", payload.groupKey);
90-
pay.put("groupMessage", payload.groupMessage);
91-
pay.put("actionButtons", payload.actionButtons);
92-
pay.put("fromProjectNumber", payload.fromProjectNumber);
93-
pay.put("collapseId", payload.collapseId);
94-
pay.put("priority", payload.priority);
95-
96-
pay.put("rawPayload", payload.rawPayload);
97-
98-
mainObj.put("payload", pay);
94+
if (groupedNotifications != null) {
95+
JSONArray payloadJsonArray = new JSONArray();
96+
for(OSNotificationPayload payload : groupedNotifications)
97+
payloadJsonArray.put(payload.toJSONObject());
98+
mainObj.put("groupedNotifications", payloadJsonArray);
99+
}
100+
101+
mainObj.put("payload", payload.toJSONObject());
102+
}
103+
catch(Throwable t) {
104+
t.printStackTrace();
99105
}
100-
catch(JSONException e) {e.printStackTrace();}
101106

102-
return mainObj.toString();
107+
return mainObj;
103108
}
109+
104110
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public class OSNotificationOpenResult {
3434
public OSNotification notification;
3535
public OSNotificationAction action;
3636

37+
/**
38+
* @deprecated As of release 3.4.1, replaced by {@link #toJSONObject()}
39+
*/
40+
@Deprecated
3741
public String stringify() {
3842
JSONObject mainObj = new JSONObject();
3943
try {
@@ -50,4 +54,21 @@ public String stringify() {
5054

5155
return mainObj.toString();
5256
}
57+
58+
public JSONObject toJSONObject() {
59+
JSONObject mainObj = new JSONObject();
60+
try {
61+
JSONObject jsonObjAction = new JSONObject();
62+
jsonObjAction.put("actionID", action.actionID);
63+
jsonObjAction.put("type", action.type.ordinal());
64+
65+
mainObj.put("action", jsonObjAction);
66+
mainObj.put("notification", notification.toJSONObject());
67+
}
68+
catch(JSONException e) {
69+
e.printStackTrace();
70+
}
71+
72+
return mainObj;
73+
}
5374
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,38 @@ public static class BackgroundImageLayout {
6464
public String titleTextColor;
6565
public String bodyTextColor;
6666
}
67+
68+
69+
public JSONObject toJSONObject() {
70+
JSONObject json = new JSONObject();
71+
72+
try {
73+
json.put("notificationID", notificationID);
74+
json.put("title", title);
75+
json.put("body", body);
76+
if (additionalData != null)
77+
json.put("additionalData", additionalData);
78+
json.put("smallIcon", smallIcon);
79+
json.put("largeIcon", largeIcon);
80+
json.put("bigPicture", bigPicture);
81+
json.put("smallIconAccentColor", smallIconAccentColor);
82+
json.put("launchURL", launchURL);
83+
json.put("sound", sound);
84+
json.put("ledColor", ledColor);
85+
json.put("lockScreenVisibility", lockScreenVisibility);
86+
json.put("groupKey", groupKey);
87+
json.put("groupMessage", groupMessage);
88+
json.put("actionButtons", actionButtons);
89+
json.put("fromProjectNumber", fromProjectNumber);
90+
json.put("collapseId", collapseId);
91+
json.put("priority", priority);
92+
93+
json.put("rawPayload", rawPayload);
94+
}
95+
catch (Throwable t) {
96+
t.printStackTrace();
97+
}
98+
99+
return json;
100+
}
67101
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public void init() {
167167
private static TrackGooglePurchase trackGooglePurchase;
168168
private static TrackAmazonPurchase trackAmazonPurchase;
169169

170-
public static final String VERSION = "030400";
170+
public static final String VERSION = "030401";
171171

172172
private static AdvertisingIdentifierProvider mainAdIdProvider = new AdvertisingIdProviderGPS();
173173

OneSignalSDK/unittest/src/test/java/com/test/onesignal/MainOneSignalClassRunner.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040

4141
import com.onesignal.BuildConfig;
4242
import com.onesignal.OSNotification;
43+
import com.onesignal.OSNotificationAction;
4344
import com.onesignal.OSNotificationOpenResult;
45+
import com.onesignal.OSNotificationPayload;
4446
import com.onesignal.OneSignal;
4547
import com.onesignal.OneSignalDbHelper;
4648
import com.onesignal.ShadowBadgeCountUpdater;
@@ -1384,8 +1386,56 @@ public void shouldCancelAndClearNotifications() throws Exception {
13841386
cursor.close();
13851387
}
13861388

1389+
1390+
// ####### Unit test toJSONObject methods
1391+
@Test
1392+
public void testOSNotificationToJSONObject() throws Exception {
1393+
OSNotification osNotification = createTestOSNotification();
1394+
1395+
JSONObject testJsonObj = osNotification.toJSONObject();
1396+
1397+
Assert.assertEquals("msg_body", testJsonObj.optJSONObject("payload").optString("body"));
1398+
1399+
JSONObject additionalData = testJsonObj.optJSONObject("payload").optJSONObject("additionalData");
1400+
Assert.assertEquals("bar", additionalData.optString("foo"));
1401+
}
1402+
1403+
@Test
1404+
public void testOSNotificationOpenResultToJSONObject() throws Exception {
1405+
OSNotificationOpenResult osNotificationOpenResult = new OSNotificationOpenResult();
1406+
osNotificationOpenResult.notification = createTestOSNotification();
1407+
osNotificationOpenResult.action = new OSNotificationAction();
1408+
osNotificationOpenResult.action.type = OSNotificationAction.ActionType.Opened;
1409+
1410+
JSONObject testJsonObj = osNotificationOpenResult.toJSONObject();
1411+
1412+
JSONObject additionalData = testJsonObj.optJSONObject("notification").optJSONObject("payload").optJSONObject("additionalData");
1413+
Assert.assertEquals("bar", additionalData.optString("foo"));
1414+
1415+
System.out.println(testJsonObj.optJSONObject("notification"));
1416+
JSONObject firstGroupedNotification = (JSONObject)testJsonObj.optJSONObject("notification").optJSONArray("groupedNotifications").get(0);
1417+
Assert.assertEquals("collapseId1", firstGroupedNotification.optString("collapseId"));
1418+
}
1419+
13871420
// ####### Unit test helper methods ########
13881421

1422+
private static OSNotification createTestOSNotification() throws Exception {
1423+
OSNotification osNotification = new OSNotification();
1424+
1425+
osNotification.payload = new OSNotificationPayload();
1426+
osNotification.payload.body = "msg_body";
1427+
osNotification.payload.additionalData = new JSONObject("{\"foo\": \"bar\"}");
1428+
1429+
osNotification.displayType = OSNotification.DisplayType.None;
1430+
1431+
osNotification.groupedNotifications = new ArrayList<>();
1432+
OSNotificationPayload groupedPayload = new OSNotificationPayload();
1433+
groupedPayload.collapseId = "collapseId1";
1434+
osNotification.groupedNotifications.add(groupedPayload);
1435+
1436+
return osNotification;
1437+
}
1438+
13891439
private static void threadWait() {
13901440
try {Thread.sleep(1000);} catch (Throwable t) {}
13911441
}

0 commit comments

Comments
 (0)