OneSignal / react-native-onesignal

React Native Library for OneSignal Push Notifications Service
Other
1.57k stars 374 forks source link

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

Open cnian94 opened 3 months ago

cnian94 commented 3 months ago

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

else if (value instanceof ArrayList) {
      writableMap.putArray(key, convertArrayListIntoReadableArray((ArrayList<Object>) value));
}
    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?

Relevant log output

No response

Code of Conduct

jennantilla commented 2 months ago

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