package com.plugin.gcm;

import android.app.NotificationManager;
import android.content.Context;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;

public class PushHandlerActivity extends Activity {

    public static final String COLDSTART = "coldstart";

    public static final String PUSH_BUNDLE = "pushBundle";

    private static String TAG = "PushPlugin-PushHandlerActivity";

    /*
     * this activity will be started if the user touches a notification that we own.
     * We send it's data off to the push plugin for processing.
     * If needed, we boot up the main activity to kickstart the application.
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        boolean isPushPluginActive = NotificationService.getInstance(getApplicationContext())
                                                        .isActive();

        Log.v(TAG, "onCreate - isPushPluginActive: " + isPushPluginActive);

        processPushBundle(isPushPluginActive);

        GCMIntentService.cancelNotification(this);

        finish();

        if (!isPushPluginActive) {
            forceMainActivityReload();
        }
    }

    /**
     * Takes the pushBundle extras from the intent, and sends it through to the PushPlugin for
     * processing.
     */
    private void processPushBundle(boolean isPushPluginActive) {
        Bundle extras = getIntent().getExtras();

        if (extras != null) {

            Bundle originalExtras = extras.getBundle(PUSH_BUNDLE);

            if (!isPushPluginActive) {
                originalExtras.putBoolean(COLDSTART, true);
            }

            NotificationService.getInstance(getApplicationContext()).onMessage(originalExtras, true);
        }
    }


    /**
     * Forces the main activity to re-launch if it's unloaded.
     */
    private void forceMainActivityReload() {
        PackageManager pm = getPackageManager();
        String packageName = getApplicationContext().getPackageName();

        Log.v(TAG, "forceMainActivityReload() - packageName: " + packageName);

        Intent launchIntent = pm.getLaunchIntentForPackage(packageName);
        startActivity(launchIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP));
        //startActivity(launchIntent);
    }

    @Override
    protected void onResume() {
      super.onResume();
      final NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
      notificationManager.cancelAll();
    }

}

@@ -18,16 +18,76 @@
 n.gcm;%0A%0A
+import com.appgyver.cordova.AGCordovaApplicationInterface;%0A%0A
 import a
@@ -361,65 +361,8 @@
  %7B%0A%0A
-    public static final String COLDSTART = %22coldstart%22;%0A%0A
     
@@ -498,17 +498,17 @@
 %0A     * 
-t
+T
 his acti
@@ -580,72 +580,8 @@
 wn.%0A
-     * We send it's data off to the push plugin for processing.%0A
     
@@ -839,172 +839,8 @@
 );%0A%0A
-        boolean isPushPluginActive = NotificationService.getInstance(getApplicationContext())%0A                                                        .isActive();%0A%0A
     
@@ -843,25 +843,25 @@
         Log.
-v
+d
 (TAG, %22onCre
@@ -872,96 +872,54 @@
 - is
-PushPluginActive: %22 + isPushPluginActive);%0A%0A        processPushBundle(isPushPluginActive
+ApplicationRunning: %22 + isApplicationRunning()
 );%0A%0A
@@ -974,27 +974,8 @@
 );%0A%0A
-        finish();%0A%0A
     
@@ -982,35 +982,41 @@
     if (
+ 
 !is
-PushPluginActive
+ApplicationRunning() 
 ) %7B%0A    
@@ -1064,299 +1064,87 @@
   %7D%0A
-    %7D%0A%0A    /**%0A     * Takes the pushBundle extras from the intent, and sends it through to the PushPlugin for%0A     * processing.%0A     */%0A    private void processPushBundle(boolean isPushPluginActive) %7B%0A        Bundle extras = getIntent().getExtras();%0A%0A        if (extras != null
+%0A        finish();%0A    %7D%0A%0A    private boolean isApplicationRunning(
 ) %7B%0A
-%0A
         
     
@@ -1143,301 +1143,110 @@
     
-    Bundle originalExtras = extras.getBundle(PUSH_BUNDLE);%0A%0A            if (!isPushPluginActive) %7B%0A                originalExtras.putBoolean(COLDSTART, true);%0A            %7D%0A%0A            NotificationService.getInstance(getApplicationContext()).onMessage(originalExtras, true);%0A        %7D
+return ((AGCordovaApplicationInterface) getApplicationContext()).getCurrentActivity() != null;
 %0A    %7D%0A%0A
 %0A   
@@ -1241,17 +1241,16 @@
 %0A    %7D%0A%0A
-%0A
     /**%0A
@@ -1498,17 +1498,17 @@
     Log.
-v
+d
 (TAG, %22f
@@ -1722,47 +1722,8 @@
 ));%0A
-        //startActivity(launchIntent);%0A
     

Update PushHandlerActivity.java