Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android - Push Notifications - JSON Payload Upgrade #1346

Merged
merged 1 commit into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 90 additions & 17 deletions Source/Fuse.PushNotifications/Android/Impl.uno
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ namespace Fuse.PushNotifications

static void OnRecieve(Java.Object bundle, bool fromNotificationBar)
{
var message = GetPayloadFromBundle(bundle);
var message = BundleToJSONStr(bundle);
if (Lifecycle.State == ApplicationState.Interactive)
{
var x = ReceivedNotification;
Expand All @@ -174,25 +174,96 @@ namespace Fuse.PushNotifications
}
}


[Foreign(Language.Java)]
static string GetPayloadFromBundle(Java.Object _bundle)
static Java.Object BundleToJSONObject(Java.Object _bundle)
@{
Bundle bundle = (Bundle)_bundle;
HashMap<String,Object> result = new HashMap<String, Object>();
for (String key : bundle.keySet()) {
Object item = bundle.get(key);
/* Only map simple serializable primitive items */
if (item!=null && (
item instanceof String ||
item instanceof Long ||
item instanceof Integer ||
item instanceof Boolean ||
item instanceof Double
)) {
result.put(key, item);

JSONObject resultJson = new JSONObject();

if (bundle == null) {
return resultJson;
}

try {
for (String bundleKey : bundle.keySet()) {

Object bundleValue = bundle.get(bundleKey);

if (bundleValue instanceof Bundle) {
resultJson.put(bundleKey, @{BundleToJSONObject(Java.Object):Call((Bundle) bundleValue)} );
} else if (bundleValue instanceof String) {
resultJson.put(bundleKey, "" + bundleValue);
} else if (bundleValue instanceof Boolean) {
resultJson.put(bundleKey, (boolean) bundleValue);
} else if (bundleValue instanceof Integer) {
resultJson.put(bundleKey, (int) bundleValue);
} else if (bundleValue instanceof Double) {
resultJson.put(bundleKey, (double) bundleValue);
} else if (bundleValue instanceof Long) {
resultJson.put(bundleKey, (long) bundleValue);
}
}
} catch (JSONException je) {
Log.d("PushNotifications.Impl.BundleToJSONObject", "BAD JSON");
}
JSONObject resultJson = new JSONObject(result);

return resultJson;
@}

[Foreign(Language.Java)]
static string BundleToJSONStr(Java.Object _bundle)
@{
Bundle bundle = (Bundle)_bundle;

if (bundle == null) {
return "";
}

JSONObject resultJson = new JSONObject();

try {
for (String bundleKey : bundle.keySet()) {

Object bundleValue = bundle.get(bundleKey);

if (bundleValue instanceof Bundle) {
resultJson.put(bundleKey, @{BundleToJSONObject(Java.Object):Call((Bundle) bundleValue)} );
} else if (bundleValue instanceof String) {
resultJson.put(bundleKey, "" + bundleValue);
} else if (bundleValue instanceof Boolean) {
resultJson.put(bundleKey, (boolean) bundleValue);
} else if (bundleValue instanceof Integer) {
resultJson.put(bundleKey, (int) bundleValue);
} else if (bundleValue instanceof Double) {
resultJson.put(bundleKey, (double) bundleValue);
} else if (bundleValue instanceof Long) {
resultJson.put(bundleKey, (long) bundleValue);
}
}
} catch (JSONException je) {
Log.d("PushNotifications.Impl.BundleToJSONStr", "BAD JSON");

//fallback to older implementation

HashMap<String,Object> result = new HashMap<String, Object>();
for (String key : bundle.keySet()) {
Object item = bundle.get(key);
/* Only map simple serializable primitive items */
if (item!=null && (
item instanceof String ||
item instanceof Long ||
item instanceof Integer ||
item instanceof Boolean ||
item instanceof Double
)) {
result.put(key, item);
}
}
resultJson = new JSONObject(result);
}

String finalPayload = resultJson.toString();
return finalPayload;
@}
Expand All @@ -218,8 +289,10 @@ namespace Fuse.PushNotifications
final Bundle bundle = (Bundle)_bundle;

if (!PushNotificationReceiver.InForeground) {
String notification = bundle.getString("notification");
String aps = bundle.getString("aps");

String notification = @{BundleToJSONStr(Java.Object):Call((Bundle) bundle.get("notification"))};
String aps = @{BundleToJSONStr(Java.Object):Call((Bundle) bundle.get("aps"))};

if (notification != null) {
// using the google style 'notification' subtree
@{NotificationFromJson(Java.Object,string,Java.Object):Call(listener, notification, bundle)};
Expand Down
122 changes: 120 additions & 2 deletions Source/Fuse.PushNotifications/Android/PushNotificationReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
import android.os.Bundle;
import java.util.ArrayList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Arrays;
import java.util.Iterator;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONException;
import android.util.Log;

public class PushNotificationReceiver extends FirebaseMessagingService {
public static ArrayList<Bundle> _cachedBundles = new ArrayList<Bundle>();
Expand All @@ -27,11 +35,121 @@ public void onMessageReceived(RemoteMessage message)
{
synchronized (lock) {
Bundle bundle = new Bundle();
for (Map.Entry<String, String> entry : message.getData().entrySet()) {
bundle.putString(entry.getKey(), entry.getValue());

try {
//reconstruct JSON string from Map and test it
String jsonStr = "{";
for (Map.Entry<String, String> entry : message.getData().entrySet()) {
jsonStr += "\"" + entry.getKey() + "\":";
if (entry.getValue().charAt(0) == '{') {
jsonStr += entry.getValue() + ",";
} else {
jsonStr += "\"" + entry.getValue() + "\"" + ",";
}
}
jsonStr = jsonStr.replaceAll(",$", "");
jsonStr += "}";

JSONObject jsonObj = new JSONObject(jsonStr);

jsonStr = jsonObj.toString();

bundle = jsonStrToBundle(jsonStr);

} catch (JSONException je) {
Log.d("onMessageReceived", "BAD JSON");

//fallback to older implementation
for (Map.Entry<String, String> entry : message.getData().entrySet()) {
bundle.putString(entry.getKey(), entry.getValue());
}
}

com.foreign.Fuse.PushNotifications.AndroidImpl.OnNotificationRecieved(this, message.getFrom(), bundle);
}
}

public static Bundle jsonStrToBundle(String jsonStr) {
Bundle bundle = new Bundle();

try {
JSONObject jsonObject = new JSONObject(jsonStr.trim());
bundle = handleJSONObject(jsonObject);
} catch (JSONException notObject) {
try {
JSONArray jsonArr = new JSONArray(jsonStr.trim());
bundle = handleJSONArray(jsonArr);
} catch (JSONException badJSON) {
Log.d("jsonStrToBundle", "BAD JSON");
}
}

return bundle;
}


public static Bundle handleJSONArray(JSONArray jsonArray) {
Bundle bundle = new Bundle();

for (int i = 0; i < jsonArray.length(); i++) {

try {
Object jsonArrayValue = jsonArray.get(i);

if (jsonArrayValue instanceof JSONObject) {
bundle.putBundle("" + i, handleJSONObject((JSONObject) jsonArrayValue));
} else if (jsonArrayValue instanceof JSONArray) {
bundle.putBundle("" + i, handleJSONArray((JSONArray) jsonArrayValue));
} else if (jsonArrayValue instanceof String) {
bundle.putString("" + i, "" + jsonArrayValue);
} else if (jsonArrayValue instanceof Boolean) {
bundle.putBoolean("" + i, (boolean) jsonArrayValue);
} else if (jsonArrayValue instanceof Integer) {
bundle.putInt("" + i, (int) jsonArrayValue);
} else if (jsonArrayValue instanceof Double) {
bundle.putDouble("" + i, (double) jsonArrayValue);
} else if (jsonArrayValue instanceof Long) {
bundle.putLong("" + i, (long) jsonArrayValue);
}
} catch (JSONException je) {
Log.d("handleJSONArray", "BAD JSON VALUE IN JSON ARRAY, AT POSITION: " + i);
}
}

return bundle;
}

public static Bundle handleJSONObject(JSONObject jsonObject) {
Bundle bundle = new Bundle();

Iterator<String> keys = jsonObject.keys();

while(keys.hasNext()) {
String keyStr = keys.next();

try {
Object keyValue = jsonObject.get(keyStr);

if (keyValue instanceof JSONObject) {
bundle.putBundle(keyStr, handleJSONObject((JSONObject) keyValue));
} else if (keyValue instanceof JSONArray) {
bundle.putBundle(keyStr, handleJSONArray((JSONArray) keyValue));
} else if (keyValue instanceof String) {
bundle.putString(keyStr, "" + keyValue);
} else if (keyValue instanceof Boolean) {
bundle.putBoolean(keyStr, (boolean) keyValue);
} else if (keyValue instanceof Integer) {
bundle.putInt(keyStr, (int) keyValue);
} else if (keyValue instanceof Double) {
bundle.putDouble(keyStr, (double) keyValue);
} else if (keyValue instanceof Long) {
bundle.putLong(keyStr, (long) keyValue);
}
} catch (JSONException je) {
Log.d("handleJSONObject", "BAD JSON VALUE IN JSON OBJECT, AT KEY: " + keyStr);
}
}

return bundle;
}
}