cowbell / cordova-plugin-geofence

Geofencing plugin for cordova
Apache License 2.0
264 stars 318 forks source link

onNotificationClicked does not fire on cold start #185

Closed tabrindle closed 6 years ago

tabrindle commented 7 years ago

If the app is not inactive/running in background (app has been closed by iOS or force closed by the user) and a notification generated by this plugin is clicked, it will invoke the application but never call onNotificationClicked

I believe this is because didReceiveLocalNotification does not fire on cold start, and instead the plugin should also have an observer for didFinishLaunchingWithOptions to detect the app was invoked by a notification. Something like this:

    NSNotificationCenter.defaultCenter().addObserver(
        self,
        selector:  #selector(GeofencePlugin.didFinishLaunchingWithOptions(_:)),
        name: UIApplicationDidFinishLaunchingNotification,
        object: nil
    )

Then a function like this to call the didRecieveLocalNotification handler inside the Geofence class

    func didFinishLaunchingWithOptions(notification: NSNotification) {
        log("didFinishLaunchingWithOptions")

        let launchOptions = notification.userInfo!
        let localNotification = (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey] as! BooleanType)
        if localNotification {
            self.didReceiveLocalNotification(notification)
        }
    }

The problem here is this observer never receives a didFinishLaunchingWithOptions event: Some of this is a bit over my head, but I believe the cordova-plugin-app-event plugin should handle rebroadcasting this, but it doesn't seem to work.