package com.dieam.reactnativepushnotification.modules;

import android.os.Build;
import android.os.Bundle;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;

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

import java.util.Set;

/**
 * Created by lambert on 2016/10/09.
 */

public class RNPushNotificationJsDelivery {
    private ReactApplicationContext mReactContext;

    public RNPushNotificationJsDelivery(ReactApplicationContext reactContext) {
        mReactContext = reactContext;
    }

    void sendEvent(String eventName, Object params) {
        if (mReactContext.hasActiveCatalystInstance()) {
            mReactContext
                    .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                    .emit(eventName, params);
        }
    }

    void notifyRemoteFetch(Bundle bundle) {
        String bundleString = convertJSON(bundle);
        WritableMap params = Arguments.createMap();
        params.putString("dataJSON", bundleString);
        sendEvent("remoteFetch", params);
    }

    void notifyNotification(Bundle bundle) {
        String bundleString = convertJSON(bundle);

        WritableMap params = Arguments.createMap();
        params.putString("dataJSON", bundleString);

        sendEvent("remoteNotificationReceived", params);
    }

    void notifyNotificationAction(Bundle bundle) {
        String bundleString = convertJSON(bundle);

        WritableMap params = Arguments.createMap();
        params.putString("dataJSON", bundleString);

        sendEvent("notificationActionReceived", params);
    }

    String convertJSON(Bundle bundle) {
        JSONObject json = new JSONObject();
        Set<String> keys = bundle.keySet();
        for (String key : keys) {
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    json.put(key, JSONObject.wrap(bundle.get(key)));
                } else {
                    json.put(key, bundle.get(key));
                }
            } catch (JSONException e) {
                return null;
            }
        }
        return json.toString();
    }

}

@@ -1790,155 +1790,618 @@
     
-JSONObject json = new JSONObject();%0A        Set%3CString%3E keys = bundle.keySet();%0A        for (String key : keys) %7B%0A            try %7B%0A               
+try %7B%0A            JSONObject json = convertJSONObject(bundle);%0A            return json.toString();%0A        %7D catch (JSONException e) %7B%0A            return null;%0A        %7D%0A    %7D%0A    %0A    // a Bundle is not a map, so we have to convert it explicitly%0A    JSONObject convertJSONObject(Bundle bundle) throws JSONException %7B%0A        JSONObject json = new JSONObject();%0A        Set%3CString%3E keys = bundle.keySet();%0A        for (String key : keys) %7B%0A            Object value = bundle.get(key);%0A            if (value instanceof Bundle) %7B%0A                json.put(key, convertJSONObject((Bundle)value));%0A            %7D else
  if 
@@ -2464,36 +2464,32 @@
                 
-    
 json.put(key, JS
@@ -2506,31 +2506,17 @@
 rap(
-bundle.get(key)));%0A    
+value));%0A
     
@@ -2540,36 +2540,32 @@
                 
-    
 json.put(key, bu
@@ -2566,111 +2566,14 @@
 ey, 
-bundle.get(key));%0A                %7D%0A            %7D catch (JSONException e) %7B%0A                return null
+value)
 ;%0A  
@@ -2609,34 +2609,23 @@
  return json
-.toString()
 ;%0A    %7D%0A%0A%7D%0A

Fix nested Bundle to JSON serialization