cowbell / cordova-plugin-geofence

Geofencing plugin for cordova
Apache License 2.0
265 stars 316 forks source link

openAppOnClick: true makes Apps restart on Taping the Notification #204

Open AwsmOli opened 7 years ago

AwsmOli commented 7 years ago

Suspicious Behaviour When i get a Notification created using this plugin with openAppOnClick: true and the App is in background Cordova seems to destroy the App and start Bootstrapping again.

Side effects As a result of this the onNotificationClicked Eventhandler will never be executed.

Seen on: Nexus 5X running Android 7.1.1

Not tested on iOS yet!

Steps to reproduce:

  1. Clone the tsubik/ionic2-geofence repo
  2. Open it in you favorite IDE
  3. Navigate to ionic2-geofence/app/models/geofence.ts
  4. add the openAppOnClick: true flag to the Fixures so that { id: "5", latitude: 40.7257093, longitude: -74.0032786, radius: 4000, transitionType: 3, notification: { text: "New York - SOHO"} }, becommes: { id: "5", latitude: 40.7257093, longitude: -74.0032786, radius: 4000, transitionType: 3, notification: { text: "New York - SOHO", openAppOnClick: true } },
  5. Run it on an Android Device: ionic run android --device
  6. Open the app and hit "+ Add Fixtures" from the sidemenu
  7. Put the app in the background. (do not close it - just go to the Homescreen)
  8. Use a Geolocation Faker to spawn yourself into the Fence (middle of New York in the above example);
  9. Wait for the notification, and tap it.
  10. Look at the app restarting/bootstrapping

Further investigation:

Using Logcat i can see that there is some Intent to open the Main Activity, followed by an attempt to play a sound for the Notification. and after that you can see a Cordova.onDestroy() and later again a Cordova.onCreate()

To me it looks like the App itself is not restarted, and the Cordova activity as brought into foreground again, but for some reason Cordova just bootstraps the whole app again. Also while Cordova is bootstrapping you wont see the splash screen, but just a white screen until the app is loaded.

Logcat output when i tab the notification:

01-31 12:17:25.795 882 3588 I ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x1000c000 pkg=com.tsubik.ionic2geofence cmp=com.tsubik.ionic2geofence/.MainActivity} from uid 10234 on display 0 01-31 12:17:25.800 514 661 D audio_hw_primary: enable_snd_device: snd_device(78: vi-feedback) 01-31 12:17:25.800 514 661 D audio_hw_primary: enable_audio_route: usecase(21) apply and update mixer path: spkr-vi-record 01-31 12:17:25.809 7614 7614 D CordovaActivity: CordovaActivity.onDestroy() 01-31 12:17:25.818 7614 7614 D CordovaWebViewImpl: >>> loadUrl(about:blank) 01-31 12:17:25.828 514 661 D audio_hw_primary: enable_audio_route: usecase(1) apply and update mixer path: low-latency-playback 01-31 12:17:25.833 7614 7614 W cr_AwContents: WebView.destroy() called while WebView is still attached to window. 01-31 12:17:25.842 882 10208 E ActivityManager: applyOptionsLocked: Unknown animationType=0 01-31 12:17:25.886 7614 7614 I CordovaLog: Changing log level to DEBUG(3) 01-31 12:17:25.886 7614 7614 I CordovaActivity: Apache Cordova native platform version 6.1.2 is starting 01-31 12:17:25.886 7614 7614 D CordovaActivity: CordovaActivity.onCreate()

Can anyone confirm this? Can anyone try that on iOS?

I really have no idea right now how to look into this further, if anyone has some ideas, ill look into it and try to fix it.

tsubik commented 7 years ago

@AwsmOli True, that's weird and maybe a Cordova applications issue. However, onNotificationClicked is being invoked when you tap the notification, check updated logging on mentioned https://github.com/tsubik/ionic2-geofence repo.

sport4minus commented 7 years ago

I am seeing the same issue on Android 4.2.2, will test in iOS soon. I am not super-familiar with Android development, but as some random googling told me, there are several places to look:

i did some fairly random trial-and-error with flags on both intents, but no luck yet. any hints where to look first?

e4r commented 7 years ago

Guys I'm trying on Android 6.x and got it working by ensuring to initialize() the plugin as soon as the ionicPlatform is ready, so I basically added the initialization here:

app.js

angular.module('starter', ['ionic', 'starter.controllers', 'starter.services','ngCordova'])

.run(function($ionicPlatform) {
    $ionicPlatform.ready(function() {
      if (window.geofence) {
        window.geofence.initialize().then(function() {
          console.log("Successful initialization");
        }, function(error) {
          console.log("Error", error);
        });
        window.geofence.onNotificationClicked = function (notificationData) {
          alert('App opened from Geo Notification!');
        };
      }

(......)

however I couldn't make it restore the previous angularJS' state, but at least is firing the onNotificationClicked event, and from there I can manage to address the user where I prefer.

sport4minus commented 7 years ago

good news: @Tempus35 seems to have found a solution to this and already made a pull request : #116.

the magic happens in GeoNotificationNotifier.java

The pull request addresses a lot of other things, and is discussed, so it is still open. But if you urgently need the feature, you can use @Tempus35 's branch or just the file.

rodneyjoyce commented 7 years ago

Any update on this? We have the same problem.