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

[Bug]: Arrays on the additionalData not being parsed correctly on android #1724

Open
2 of 3 tasks
cnian94 opened this issue Jul 26, 2024 · 1 comment
Open
2 of 3 tasks

Comments

@cnian94
Copy link

cnian94 commented Jul 26, 2024

What happened?

Description

Arrays on the additional data becomes null while retrieving them on the notification click event listener only on android.

Suspected Cause

I think the "convertHashMapToWritableMap" method below may be causing this issue

    public static WritableMap convertHashMapToWritableMap(HashMap<String, Object> hashMap) throws JSONException {
        WritableMap writableMap = Arguments.createMap();
        for (Map.Entry<String, Object> entry : hashMap.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();

            if (value instanceof String) {
                writableMap.putString(key, (String) value);
            } else if (value instanceof Boolean) {
                writableMap.putBoolean(key, (Boolean) value);
            } else if (value instanceof Integer) {
                writableMap.putInt(key, (Integer) value);
            } else if (value instanceof Double) {
                writableMap.putDouble(key, (Double) value);
            } else if (value instanceof Float) {
                writableMap.putDouble(key, ((Float) value).doubleValue());
            } else if (value instanceof Long) {
                writableMap.putDouble(key, ((Long) value).doubleValue());
            } else if (value instanceof HashMap) {
                writableMap.putMap(key, convertHashMapToWritableMap((HashMap<String, Object>) value));
            } else {
                writableMap.putNull(key);
            }
        }
        return writableMap;
    }

If the additional data contains a value that is array of objects, the "value" variable on the above function becomes an instance of ArrayList class. However, the function is not checking this condition and executing the last else statement that puts null for that key.

Possible solution

  • Adding check for the value instanceof the ArrayList
else if (value instanceof ArrayList) {
      writableMap.putArray(key, convertArrayListIntoReadableArray((ArrayList<Object>) value));
}
  • implementing an additional function that converts an ArrayList into ReadableArray
    public static ReadableArray convertArrayListIntoReadableArray(ArrayList<Object> arrayList) throws JSONException {
        WritableArray array = Arguments.createArray();
        for (Object object : arrayList) {
            if (object instanceof HashMap)
                array.pushMap(convertHashMapToWritableMap((HashMap<String, Object>) object));
        }
        return array;
    }

Steps to reproduce?

1. Install react-native-onesignal: 5.2.2
2. Setup notification click event listener as docs suggested
3. send notification via onesignal api which contains data like below:
{
  "data": {
    "route": "Search",
    "basket": [
      {
        "sku": "12qw3er56y",
        "itemName": "Hand-crafted Peppermint Candy Canes",
        "qty": 3,
        "cost": "9.99"
      },
      {
        "sku": "90ujr5383",
        "itemName": "Fat Squirrel",
        "qty": 1,
        "cost": "29.99"
      }
    ]
  },
}

4. check the additionalData on the notification click listener
5. you will see that the basket property going to be null

What did you expect to happen?

I expected to see the arrays on the additional data parsed correctly and not getting null value

React Native OneSignal SDK version

5.2.2

Which platform(s) are affected?

  • iOS
  • Android

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@jennantilla
Copy link
Contributor

@cnian94 thank you for flagging! We'll get this fixed ASAP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants