From c05e824dc2b68feb5bcf7b3dc327bd3ca0224f88 Mon Sep 17 00:00:00 2001 From: Matthias Benkort Date: Thu, 23 Jul 2015 11:54:13 +0200 Subject: [PATCH] Wait for the module to be running before triggering the event Fix state managing to fit the actual behavior with Titanium Review implementation and debug logs --- .../src/eu/rebelcorp/parse/ParseModule.java | 27 ++++--- .../parse/ParseModuleBroadcastReceiver.java | 77 +++++++++---------- 2 files changed, 52 insertions(+), 52 deletions(-) diff --git a/android/src/eu/rebelcorp/parse/ParseModule.java b/android/src/eu/rebelcorp/parse/ParseModule.java index f2416a4..5dbf24f 100644 --- a/android/src/eu/rebelcorp/parse/ParseModule.java +++ b/android/src/eu/rebelcorp/parse/ParseModule.java @@ -41,8 +41,12 @@ public class ParseModule extends KrollModule public static String PROPERTY_APP_ID = "Parse_AppId"; public static String PROPERTY_CLIENT_KEY = "Parse_ClientKey"; + public static final int STATE_RUNNING = 1; + public static final int STATE_STOPPED = 2; + public static final int STATE_DESTROYED = 3; + /* Control the state of the activity */ - private boolean isModuleRunning; + private int state = STATE_DESTROYED; // You can define constants with @Kroll.constant, for example: // @Kroll.constant public static final String EXTERNAL_NAME = value; @@ -51,7 +55,6 @@ public ParseModule() { super(); module = this; - setIsModuleRunning(true); } @Kroll.onAppCreate @@ -61,7 +64,6 @@ public static void onAppCreate(TiApplication app) String clientKey = TiApplication.getInstance().getAppProperties().getString(ParseModule.PROPERTY_CLIENT_KEY, ""); Log.d(TAG, "Initializing with: " + appId + ", " + clientKey + ";"); - Parse.initialize(TiApplication.getInstance(), appId, clientKey); } @@ -69,42 +71,42 @@ public static void onAppCreate(TiApplication app) public void onStart(Activity activity) { super.onStart(activity); - setIsModuleRunning(true); + setState(STATE_RUNNING); } public void onResume(Activity activity) { super.onResume(activity); - setIsModuleRunning(true); + setState(STATE_RUNNING); } public void onPause(Activity activity) { super.onPause(activity); - setIsModuleRunning(false); + setState(STATE_STOPPED); } public void onStop(Activity activity) { super.onStop(activity); - setIsModuleRunning(false); + setState(STATE_STOPPED); } public void onDestroy(Activity activity) { super.onDestroy(activity); - setIsModuleRunning(false); + setState(STATE_DESTROYED); } - private void setIsModuleRunning(boolean isModuleRunning) + private void setState(int state) { - this.isModuleRunning = isModuleRunning; + this.state = state; } /* An accessor from the outside */ - public boolean isModuleRunning() + public int getState() { - return isModuleRunning; + return state; } /* Get an instance of that module*/ @@ -118,6 +120,7 @@ public void start() { // Track Push opens ParseAnalytics.trackAppOpened(TiApplication.getAppRootOrCurrentActivity().getIntent()); + setState(STATE_RUNNING); ParseInstallation.getCurrentInstallation().saveInBackground(new SaveCallback() { public void done(ParseException e) { diff --git a/android/src/eu/rebelcorp/parse/ParseModuleBroadcastReceiver.java b/android/src/eu/rebelcorp/parse/ParseModuleBroadcastReceiver.java index 2ae7b03..9a157ba 100644 --- a/android/src/eu/rebelcorp/parse/ParseModuleBroadcastReceiver.java +++ b/android/src/eu/rebelcorp/parse/ParseModuleBroadcastReceiver.java @@ -28,70 +28,67 @@ public class ParseModuleBroadcastReceiver extends ParsePushBroadcastReceiver { @Override public void onPushOpen(Context context, Intent intent) { - Log.d("onPushOpen", "Clicked"); + Intent i = context.getPackageManager().getLaunchIntentForPackage(context.getApplicationContext().getPackageName()); - if (ParseModule.getInstance() != null && ParseModule.getInstance().isModuleRunning()) { - Log.d("onPushOpen", "App is running in foreground"); + /* Check if the app is running or in background. If not, just start the app and add the + * notification as Extra */ + if (ParseModule.getInstance() == null || ParseModule.getInstance().getState() == ParseModule.STATE_DESTROYED) { + Log.d("onPushOpen", "App was killed; resume the app without triggering 'notificationopen'"); + i.putExtras(intent.getExtras()); + context.startActivity(i); return; } - Log.d("onPushOpen", "App is not running or is in background. Now resume the app."); - Intent i = context.getPackageManager().getLaunchIntentForPackage(context.getApplicationContext().getPackageName()); - i.putExtras(intent.getExtras()); - context.startActivity(i); - - /* Now, the module should be opened */ - if (ParseModule.getInstance() != null) { - try { - Log.d("onPushOpen", "Open a notification"); - KrollDict data = new KrollDict(new JSONObject(intent.getExtras().getString("com.parse.Data"))); - ParseModule.getInstance().fireEvent("notificationopen", data); - } catch (Exception e) { - Log.d("JSON Failure", e.getMessage()); + /* Otherwise, just resume the app if necessary, and trigger the event */ + try { + KrollDict data = new KrollDict(new JSONObject(intent.getExtras().getString("com.parse.Data"))); + + if (ParseModule.getInstance().getState() != ParseModule.STATE_RUNNING) { + Log.d("onPushOpen", "App was in background; resume the app and trigger 'notificationopen'"); + context.startActivity(i); + } else { + Log.d("onPushOpen", "App is running in foreground; trigger 'notificationopen'"); } + + ParseModule.getInstance().fireEvent("notificationopen", data); + } catch (Exception e) { + Log.d("onPushOpen", e.getMessage()); } } - + @Override - public void onReceive(Context context, Intent intent) { + public void onPushReceive(Context context, Intent intent) { try { if (intent == null) { - Log.d("onReceive", "Receiver intent null"); + Log.d("onPushReceive", "Receiver intent null"); + super.onPushReceive(context, intent); return; } if (ParseModule.getInstance() == null) { - Log.d("onReceive", "no instance of ParseModule found"); + Log.d("onPushReceive", "No instance of ParseModule found"); + super.onPushReceive(context, intent); return; } - String action = intent.getAction(); - KrollDict data = new KrollDict(new JSONObject(intent.getExtras().getString("com.parse.Data"))); - - Log.d("onReceive", "got action " + action ); - if (action.equals("com.parse.push.intent.RECEIVE")) { - /* The notification is received by the device */ - Log.d("onReceive", "New notification received"); + /* The notification is received by the device */ + if (ParseModule.getInstance().getState() == ParseModule.STATE_RUNNING) { + Log.d("onPushReceive", "App is in foreground; trigger event 'notificationreceive'"); - if (ParseModule.getInstance().isModuleRunning()) { + try { + KrollDict data = new KrollDict(new JSONObject(intent.getExtras().getString("com.parse.Data"))); ParseModule.getInstance().fireEvent("notificationreceive", data); - } else { - Log.d("onReceive", "App is in background, the event won't be triggered"); - } - } else if (action.equals("com.parse.push.intent.OPEN")) { - /* The user has clicked on the notification */ - Log.d("onReceive", "Notification opened"); - - if (ParseModule.getInstance().isModuleRunning()) { - ParseModule.getInstance().fireEvent("notificationopen", data); - } else { - Log.d("onReceive", "App is in background, the event will be triggered later."); + } catch (Exception e) { + Log.d("onPushReceive", e.getMessage()); } + } else { + Log.d("onPushReceive", "App is in background; 'notificationreceive' won't be triggered"); } + + super.onPushReceive(context, intent); } catch (Exception e) { Log.e("Push", "Exception: " + e.toString()); } - super.onReceive(context, intent); } }