Skip to content

Commit

Permalink
Fix "launchInApp" notifications in background
Browse files Browse the repository at this point in the history
- Dispatch a visibility change event on the webview.
- Allows running of asynchronous code in webview (otherwise not working)
- Remove no longer used checkAppInForeground() methods in receivers
  • Loading branch information
timkellypa committed Jan 30, 2021
1 parent 965d356 commit 3c8e928
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 51 deletions.
5 changes: 5 additions & 0 deletions src/android/LocalNotification.java
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,11 @@ private static synchronized void sendJavascript(final String js) {
((Activity) (view.getContext())).runOnUiThread(new Runnable() {
public void run() {
view.loadUrl("javascript:" + js);
View engineView = view.getEngine().getView();

if (!isInForeground()) {
engineView.dispatchWindowVisibilityChanged(View.VISIBLE);
}
}
});
}
Expand Down
5 changes: 0 additions & 5 deletions src/android/RestoreReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,6 @@ public boolean checkAppRunning() {
return isAppRunning();
}

@Override
public boolean checkAppInForeground() {
return isInForeground();
}

/**
* Build notification specified by options.
*
Expand Down
5 changes: 0 additions & 5 deletions src/android/TriggerReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,6 @@ public boolean checkAppRunning() {
return isAppRunning();
}

@Override
public boolean checkAppInForeground() {
return isInForeground();
}

/**
* Build notification specified by options.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
import android.content.BroadcastReceiver;
import android.content.Context;
import android.os.PowerManager;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Calendar;

Expand All @@ -26,8 +22,6 @@
* The base class for any receiver that is trying to display a notification.
*/
abstract public class AbstractNotificationReceiver extends BroadcastReceiver {
private final String TAG = "AbstractNotification";

/**
* Perform a notification. All notification logic is here.
* Determines whether to dispatch events, autoLaunch the app, use fullScreenIntents, etc.
Expand All @@ -40,13 +34,6 @@ public void performNotification(Notification notification) {
PowerManager pm = (PowerManager) context.getSystemService(POWER_SERVICE);
boolean autoLaunch = options.isAutoLaunchingApp() && SDK_INT <= P && !options.useFullScreenIntent();

// Check device sleep status here (not after wake)
// If device is asleep in this moment, waking it up with our wakelock
// is not enough to allow the app to have CPU to trigger an event
// in Android 8+. Limitations on CPU can also occur when the app is in the background,
// so prevent that as well.
boolean canTriggerInApp = SDK_INT < O ||
(pm != null && pm.isInteractive() && checkAppInForeground());
int badge = options.getBadgeNumber();

if (badge > 0) {
Expand All @@ -67,8 +54,7 @@ public void performNotification(Notification notification) {
// 2. Any SDK >= Oreo is asleep (must be triggered here)
boolean didShowNotification = false;
if (!options.triggerInApp() ||
(!autoLaunch && !checkAppRunning())
|| !canTriggerInApp
(!autoLaunch && !checkAppRunning())
) {
didShowNotification = true;
notification.show();
Expand All @@ -90,34 +76,11 @@ public void performNotification(Notification notification) {

Calendar cal = Calendar.getInstance();
cal.add(MINUTE, 1);

Request req = new Request(
getOptionsWithBaseDate(options, cal.getTimeInMillis()),
cal.getTime()
);
Request req = new Request(options, cal.getTime());

manager.schedule(req, this.getClass());
}

/**
* Clone options with base date attached to trigger.
* Used so that persisted objects know the last execution time.
* @param baseDateMillis base date represented in milliseconds
* @return new Options object with base time set in requestBaseDate.
*/
private Options getOptionsWithBaseDate(Options options, long baseDateMillis) {
JSONObject optionsDict = options.getDict();
try {
JSONObject triggerDict = optionsDict.getJSONObject("trigger");
triggerDict.put("requestBaseDate", baseDateMillis);
optionsDict.remove("trigger");
optionsDict.put("trigger", triggerDict);
} catch (JSONException e) {
Log.e(TAG, "Unexpected error adding requestBaseDate to JSON structure: " + e.toString());
}
return new Options(optionsDict);
}

/**
* Send the application an event using our notification
* @param key key for our event in the app
Expand All @@ -132,8 +95,6 @@ private Options getOptionsWithBaseDate(Options options, long baseDateMillis) {
*/
abstract public boolean checkAppRunning();

abstract public boolean checkAppInForeground();

/**
* Wakeup the device.
*
Expand Down

0 comments on commit 3c8e928

Please sign in to comment.