package com.geektime.reactnativeonesignal;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.onesignal.OneSignal;

import org.json.JSONObject;


/**
 * Created by Avishay on 1/31/16.
 */
public class RNOneSignal extends ReactContextBaseJavaModule {
    public static final String NOTIFICATION_OPENED_INTENT_FILTER = "GTNotificatinOpened";

    private ReactContext mReactContext;
    private Activity mActivity;

    public RNOneSignal(ReactApplicationContext reactContext, Activity activity) {
        super(reactContext);
        mActivity = activity;
        mReactContext = reactContext;

        OneSignal.startInit(mActivity)
                .setNotificationOpenedHandler(new NotificationOpenedHandler(reactContext))
                .init();
        OneSignal.enableNotificationsWhenActive(true);

        registerNotificationsReceiveNotification();
    }

    public RNOneSignal(ReactApplicationContext reactContext) {
        super(reactContext);
    }

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

    @ReactMethod
    public void sendTag(String key, String value) {
        OneSignal.sendTag(key, value);
    }

    @ReactMethod
    public void sendTags(ReadableMap tags) {
        OneSignal.sendTags(RNUtils.readableMapToJson(tags));
    }

    @ReactMethod
    public void getTags(final Callback callback) {
        OneSignal.getTags(new OneSignal.GetTagsHandler() {
            @Override
            public void tagsAvailable(JSONObject tags) {
                callback.invoke(RNUtils.jsonToWritableMap(tags));
            }
        });
    }

    @ReactMethod
    public void deleteTag(String key) {
        OneSignal.deleteTag(key);
    }

    private void registerNotificationsReceiveNotification() {
        IntentFilter intentFilter = new IntentFilter(NOTIFICATION_OPENED_INTENT_FILTER);

        mReactContext.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                notifyNotification(intent.getExtras());
            }
        }, intentFilter);
    }

    private void notifyNotification(Bundle bundle) {
        final WritableMap params = Arguments.createMap();
        params.putString("message", bundle.getString("message"));
        params.putString("additionalData", bundle.getString("additionalData"));
        params.putBoolean("isActive", bundle.getBoolean("isActive"));

        sendEvent("remoteNotificationOpened", params);
    }

    @Override
    public String getName() {
        return "RNOneSignal";
    }
}

@@ -2427,32 +2427,659 @@
 ag(key);%0A    %7D%0A%0A
+    @ReactMethod%0A    public void enableVibrate(Boolean enable) %7B%0A        OneSignal.enableVibrate(enable);%0A    %7D%0A%0A    @ReactMethod%0A    public void enableSound(Boolean enable) %7B%0A        OneSignal.enableSound(enable);%0A    %7D%0A%0A    @ReactMethod%0A    public void enableNotificationsWhenActive(Boolean enable) %7B%0A        OneSignal.enableNotificationsWhenActive(enable);%0A    %7D%0A%0A    @ReactMethod%0A    public void enableInAppAlertNotification(Boolean enable) %7B%0A        OneSignal.enableInAppAlertNotification(enable);%0A    %7D%0A%0A    @ReactMethod%0A    public void setSubscription(Boolean enable) %7B%0A        OneSignal.setSubscription(enable);%0A    %7D%0A%0A
     private void

Add some methods