package com.selfservit.util;

import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.FirebaseApp;

import org.json.JSONArray;
import org.json.JSONException;
import org.apache.cordova. * ;
import org.json.JSONObject;

public class mPushNotification extends CordovaPlugin {
	private static CallbackContext tokenRefreshCallbackContext;

	@Override
	public boolean execute(final String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
		// FirebaseApp.initializeApp(cordova.getActivity());
		if (action.equals("GetToken")) {
			try {
				String token = FirebaseInstanceId.getInstance().getToken();
				callbackContext.success(token);
			} catch (Exception e) {
				callbackContext.error(e.getMessage());
			}
		}else if(action.equals("TokenRefresh")){
			onTokenRefresh(callbackContext);
		}
		return true;
	}
	private void onTokenRefresh(final CallbackContext callbackContext) {
		mPushNotification.tokenRefreshCallbackContext = callbackContext;

		cordova.getThreadPool().execute(new Runnable() {
			public void run() {
				try {
					String currentToken = FirebaseInstanceId.getInstance().getToken();

					if (currentToken != null) {
						mPushNotification.sendToken(currentToken);
					}
				} catch (Exception e) {
					callbackContext.error(e.getMessage());
				}
			}
		});
	}
	public static void sendToken(String token) {
		if (mPushNotification.tokenRefreshCallbackContext == null) {
			return;
		}
		final CallbackContext callbackContext = mPushNotification.tokenRefreshCallbackContext;
		if (callbackContext != null && token != null) {
			PluginResult pluginresult = new PluginResult(PluginResult.Status.OK, token);
			pluginresult.setKeepCallback(true);
			callbackContext.sendPluginResult(pluginresult);
		}
	}
}

@@ -233,16 +233,51 @@
 bject;%0A%0A
+import java.util.logging.Handler;%0A%0A
 public c
@@ -324,16 +324,18 @@
 ugin %7B%0A%09
+//
 private 
@@ -618,249 +618,16 @@
 %0A%09%09%09
-try %7B%0A%09%09%09%09String token = FirebaseInstanceId.getInstance().getToken();%0A%09%09%09%09callbackContext.success(token);%0A%09%09%09%7D catch (Exception e) %7B%0A%09%09%09%09callbackContext.error(e.getMessage());%0A%09%09%09%7D%0A%09%09%7Delse if(action.equals(%22TokenRefresh%22))%7B%0A%09%09%09onTokenRefresh
+getToken
 (cal
@@ -669,41 +669,35 @@
 %7D%0A%09p
-rivate
+ublic static
  void 
-on
+get
 Token
-Refresh(final 
+(
 Call
@@ -728,155 +728,10 @@
 ext)
- 
 %7B%0A
-%09%09mPushNotification.tokenRefreshCallbackContext = callbackContext;%0A%0A%09%09cordova.getThreadPool().execute(new Runnable() %7B%0A%09%09%09public void run() %7B%0A%09%09
 %09%09tr
@@ -741,25 +741,16 @@
 %0A%09%09%09
-%09%09
 String 
-currentT
+t
 oken
@@ -801,100 +801,43 @@
 ();%0A
-%0A%09%09%09%09%09if (currentToken != null) %7B%0A%09%09%09%09%09%09mPushNotification.sendToken(currentToken);%0A%09%09%09%09%09%7D%0A%09%09
+%09%09%09callbackContext.success(token);%0A
 %09%09%7D 
@@ -858,18 +858,16 @@
 on e) %7B%0A
-%09%09
 %09%09%09callb
@@ -904,461 +904,8 @@
 ));%0A
-%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D);%0A%09%7D%0A%09public static void sendToken(String token) %7B%0A%09%09if (mPushNotification.tokenRefreshCallbackContext == null) %7B%0A%09%09%09return;%0A%09%09%7D%0A%09%09final CallbackContext callbackContext = mPushNotification.tokenRefreshCallbackContext;%0A%09%09if (callbackContext != null && token != null) %7B%0A%09%09%09PluginResult pluginresult = new PluginResult(PluginResult.Status.OK, token);%0A%09%09%09pluginresult.setKeepCallback(true);%0A%09%09%09callbackContext.sendPluginResult(pluginresult);%0A
 %09%09%7D%0A

Update mPushNotification.java