Skip to content

Commit

Permalink
Wait for the module to be running before triggering the event
Browse files Browse the repository at this point in the history
Fix state managing to fit the actual behavior with Titanium

Review implementation and debug logs
  • Loading branch information
KtorZ committed Jul 23, 2015
1 parent 43d8b53 commit c05e824
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 52 deletions.
27 changes: 15 additions & 12 deletions android/src/eu/rebelcorp/parse/ParseModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -51,7 +55,6 @@ public ParseModule()
{
super();
module = this;
setIsModuleRunning(true);
}

@Kroll.onAppCreate
Expand All @@ -61,50 +64,49 @@ 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);
}

/* Get control over the module's state */
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*/
Expand All @@ -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) {
Expand Down
77 changes: 37 additions & 40 deletions android/src/eu/rebelcorp/parse/ParseModuleBroadcastReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit c05e824

Please sign in to comment.