private static boolean isInForeground() {
        if (!deviceready || weakReferenceCordovaWebView == null) return false;

        CordovaWebView cordovaWebView = weakReferenceCordovaWebView.get();

        // Class to manage and query the state of the lock screen (also known as Keyguard).
        KeyguardManager km = (KeyguardManager) cordovaWebView.getContext().getSystemService(Context.KEYGUARD_SERVICE);

        // - km can be null if running as an instant app
        // - Only true, if lock screen is not visible and the app is visible.
        return (km == null || (km != null && !km.isKeyguardLocked())) && cordovaWebView.getView().getWindowVisibility() == View.VISIBLE;
    }
If the app is running in foreground.
src/android/LocalNotification.java