Skip to content

Commit

Permalink
Deprecated stringify in favor of toJSONObject
Browse files Browse the repository at this point in the history
* also added groupedNotifications to new toJSONObject method.
  • Loading branch information
jkasten2 committed Nov 1, 2016
1 parent ed13acb commit 045c34d
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

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

/**
* @deprecated As of release 3.4.1, replaced by {@link #toJSONObject()}
*/
@Deprecated
public String stringify() {
JSONObject mainObj = toJSONObject();

try {
if (mainObj.has("additionalData"))
mainObj.put("additionalData", mainObj.optJSONObject("additionalData").toString());
}
catch(JSONException e) {
e.printStackTrace();
}

return mainObj.toString();
}

public JSONObject toJSONObject() {
JSONObject mainObj = new JSONObject();

try {
Expand All @@ -72,33 +91,20 @@ public String stringify() {
mainObj.put("androidNotificationId", androidNotificationId);
mainObj.put("displayType", displayType.ordinal());

JSONObject pay = new JSONObject();
pay.put("notificationID", payload.notificationID);
pay.put("title", payload.title);
pay.put("body", payload.body);
if (payload.additionalData != null)
pay.put("additionalData", payload.additionalData.toString());
pay.put("smallIcon", payload.smallIcon);
pay.put("largeIcon", payload.largeIcon);
pay.put("bigPicture", payload.bigPicture);
pay.put("smallIconAccentColor", payload.smallIconAccentColor);
pay.put("launchURL", payload.launchURL);
pay.put("sound", payload.sound);
pay.put("ledColor", payload.ledColor);
pay.put("lockScreenVisibility", payload.lockScreenVisibility);
pay.put("groupKey", payload.groupKey);
pay.put("groupMessage", payload.groupMessage);
pay.put("actionButtons", payload.actionButtons);
pay.put("fromProjectNumber", payload.fromProjectNumber);
pay.put("collapseId", payload.collapseId);
pay.put("priority", payload.priority);

pay.put("rawPayload", payload.rawPayload);

mainObj.put("payload", pay);
if (groupedNotifications != null) {
JSONArray payloadJsonArray = new JSONArray();
for(OSNotificationPayload payload : groupedNotifications)
payloadJsonArray.put(payload.toJSONObject());
mainObj.put("groupedNotifications", payloadJsonArray);
}

mainObj.put("payload", payload.toJSONObject());
}
catch(Throwable t) {
t.printStackTrace();
}
catch(JSONException e) {e.printStackTrace();}

return mainObj.toString();
return mainObj;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public class OSNotificationOpenResult {
public OSNotification notification;
public OSNotificationAction action;

/**
* @deprecated As of release 3.4.1, replaced by {@link #toJSONObject()}
*/
@Deprecated
public String stringify() {
JSONObject mainObj = new JSONObject();
try {
Expand All @@ -50,4 +54,21 @@ public String stringify() {

return mainObj.toString();
}

public JSONObject toJSONObject() {
JSONObject mainObj = new JSONObject();
try {
JSONObject jsonObjAction = new JSONObject();
jsonObjAction.put("actionID", action.actionID);
jsonObjAction.put("type", action.type.ordinal());

mainObj.put("action", jsonObjAction);
mainObj.put("notification", notification.toJSONObject());
}
catch(JSONException e) {
e.printStackTrace();
}

return mainObj;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,38 @@ public static class BackgroundImageLayout {
public String titleTextColor;
public String bodyTextColor;
}


public JSONObject toJSONObject() {
JSONObject json = new JSONObject();

try {
json.put("notificationID", notificationID);
json.put("title", title);
json.put("body", body);
if (additionalData != null)
json.put("additionalData", additionalData);
json.put("smallIcon", smallIcon);
json.put("largeIcon", largeIcon);
json.put("bigPicture", bigPicture);
json.put("smallIconAccentColor", smallIconAccentColor);
json.put("launchURL", launchURL);
json.put("sound", sound);
json.put("ledColor", ledColor);
json.put("lockScreenVisibility", lockScreenVisibility);
json.put("groupKey", groupKey);
json.put("groupMessage", groupMessage);
json.put("actionButtons", actionButtons);
json.put("fromProjectNumber", fromProjectNumber);
json.put("collapseId", collapseId);
json.put("priority", priority);

json.put("rawPayload", rawPayload);
}
catch (Throwable t) {
t.printStackTrace();
}

return json;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public void init() {
private static TrackGooglePurchase trackGooglePurchase;
private static TrackAmazonPurchase trackAmazonPurchase;

public static final String VERSION = "030400";
public static final String VERSION = "030401";

private static AdvertisingIdentifierProvider mainAdIdProvider = new AdvertisingIdProviderGPS();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@

import com.onesignal.BuildConfig;
import com.onesignal.OSNotification;
import com.onesignal.OSNotificationAction;
import com.onesignal.OSNotificationOpenResult;
import com.onesignal.OSNotificationPayload;
import com.onesignal.OneSignal;
import com.onesignal.OneSignalDbHelper;
import com.onesignal.ShadowBadgeCountUpdater;
Expand Down Expand Up @@ -1384,8 +1386,56 @@ public void shouldCancelAndClearNotifications() throws Exception {
cursor.close();
}


// ####### Unit test toJSONObject methods
@Test
public void testOSNotificationToJSONObject() throws Exception {
OSNotification osNotification = createTestOSNotification();

JSONObject testJsonObj = osNotification.toJSONObject();

Assert.assertEquals("msg_body", testJsonObj.optJSONObject("payload").optString("body"));

JSONObject additionalData = testJsonObj.optJSONObject("payload").optJSONObject("additionalData");
Assert.assertEquals("bar", additionalData.optString("foo"));
}

@Test
public void testOSNotificationOpenResultToJSONObject() throws Exception {
OSNotificationOpenResult osNotificationOpenResult = new OSNotificationOpenResult();
osNotificationOpenResult.notification = createTestOSNotification();
osNotificationOpenResult.action = new OSNotificationAction();
osNotificationOpenResult.action.type = OSNotificationAction.ActionType.Opened;

JSONObject testJsonObj = osNotificationOpenResult.toJSONObject();

JSONObject additionalData = testJsonObj.optJSONObject("notification").optJSONObject("payload").optJSONObject("additionalData");
Assert.assertEquals("bar", additionalData.optString("foo"));

System.out.println(testJsonObj.optJSONObject("notification"));
JSONObject firstGroupedNotification = (JSONObject)testJsonObj.optJSONObject("notification").optJSONArray("groupedNotifications").get(0);
Assert.assertEquals("collapseId1", firstGroupedNotification.optString("collapseId"));
}

// ####### Unit test helper methods ########

private static OSNotification createTestOSNotification() throws Exception {
OSNotification osNotification = new OSNotification();

osNotification.payload = new OSNotificationPayload();
osNotification.payload.body = "msg_body";
osNotification.payload.additionalData = new JSONObject("{\"foo\": \"bar\"}");

osNotification.displayType = OSNotification.DisplayType.None;

osNotification.groupedNotifications = new ArrayList<>();
OSNotificationPayload groupedPayload = new OSNotificationPayload();
groupedPayload.collapseId = "collapseId1";
osNotification.groupedNotifications.add(groupedPayload);

return osNotification;
}

private static void threadWait() {
try {Thread.sleep(1000);} catch (Throwable t) {}
}
Expand Down

0 comments on commit 045c34d

Please sign in to comment.