From 3c8e9288780b57d3e6ff3a217f87fa8c2673658e Mon Sep 17 00:00:00 2001 From: Tim Kelly Date: Fri, 29 Jan 2021 19:16:43 -0500 Subject: [PATCH] Fix "launchInApp" notifications in background - 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 --- src/android/LocalNotification.java | 5 +++ src/android/RestoreReceiver.java | 5 --- src/android/TriggerReceiver.java | 5 --- .../AbstractNotificationReceiver.java | 43 +------------------ 4 files changed, 7 insertions(+), 51 deletions(-) diff --git a/src/android/LocalNotification.java b/src/android/LocalNotification.java index d22e0e1f0..21cbf939b 100644 --- a/src/android/LocalNotification.java +++ b/src/android/LocalNotification.java @@ -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); + } } }); } diff --git a/src/android/RestoreReceiver.java b/src/android/RestoreReceiver.java index b54ef5eb7..82cb7add8 100644 --- a/src/android/RestoreReceiver.java +++ b/src/android/RestoreReceiver.java @@ -81,11 +81,6 @@ public boolean checkAppRunning() { return isAppRunning(); } - @Override - public boolean checkAppInForeground() { - return isInForeground(); - } - /** * Build notification specified by options. * diff --git a/src/android/TriggerReceiver.java b/src/android/TriggerReceiver.java index 5fac0d1ce..ccbccfce5 100644 --- a/src/android/TriggerReceiver.java +++ b/src/android/TriggerReceiver.java @@ -76,11 +76,6 @@ public boolean checkAppRunning() { return isAppRunning(); } - @Override - public boolean checkAppInForeground() { - return isInForeground(); - } - /** * Build notification specified by options. * diff --git a/src/android/notification/receiver/AbstractNotificationReceiver.java b/src/android/notification/receiver/AbstractNotificationReceiver.java index 6fcdeaeb7..aadc03e09 100644 --- a/src/android/notification/receiver/AbstractNotificationReceiver.java +++ b/src/android/notification/receiver/AbstractNotificationReceiver.java @@ -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; @@ -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. @@ -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) { @@ -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(); @@ -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 @@ -132,8 +95,6 @@ private Options getOptionsWithBaseDate(Options options, long baseDateMillis) { */ abstract public boolean checkAppRunning(); - abstract public boolean checkAppInForeground(); - /** * Wakeup the device. *