cowbell / cordova-plugin-geofence

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

didReceiveLocalNotification is never called on iOS #183

Closed tabrindle closed 6 years ago

tabrindle commented 8 years ago

It seems that there is something wrong with the addObserver call in the pluginInitialize on iOS: (tested on iOS 9 and 10)

NSNotificationCenter.defaultCenter().addObserver(
            self,
            selector: "didReceiveLocalNotification:",
            name: "CDVLocalNotification",
            object: nil
        )

It seems the warning use of string literal for Objective-C selectors is deprecated is unrelated, as when it is fixed, the didReceiveLocalNotification still does not fire.

To get it working, I updated the selector and removed the quotes from CDVLocalNotification.

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

Also, I had to re-broadcast the event from AppDelegate using the following code, as I suspect another plugin may be interfering with this observer working.

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    [[NSNotificationCenter defaultCenter] postNotificationName:CDVLocalNotification object:notification];
}

I believe https://github.com/cowbell/cordova-plugin-geofence/issues/182 and https://github.com/cowbell/cordova-plugin-geofence/issues/180 are duplicates of this, but with less detail.

I can make a PR for the selector and name update, but I am less sure of what to do to ensure the UILocalNotification event is received, as my solution feels like a bit of a hack.

EDIT: I believe there are other installed plugins interfering with the events/observers. I believe a way to get around this is this plugin: https://github.com/katzer/cordova-plugin-app-event. This rebroadcasts events so that multiple plugins can listen to the same one without interference.