问题
I'm making an app with Apache Cordova with Evothings Javascript plugin and Estimote iBeacons. I've managed to get push notifications working with the Phonegap PushPlugin when the app is int eh background (not completely closed), however I want to take it a step further and receive a push notification when in range of the iBeacons when the app is completely killed.
I've seen this is possible from various posts in the Estimote community and here on Stack overflow by using native Xcode development, but just not using the Cordova/Evothings Javascript SDK.
My app currently runs the EstimoteBeacons.startMonitoringForRegion()
function which does work in the background when the app is closed, but just doesn't seem to run any code at all when the app is killed.
I've tried running EstimoteBeacons.requestAlwaysAuthorization();
which also doesn't do anything.
I also tried using the Cordova local notification plugin which again didn'tg work when the app was killed, which leads me to believe that no code even gets executed when the app is completely closed.
If anyone can shed some light on this I would appreciate.
Thanks
回答1:
The key to making this work when the app is not running on iOS is to make a native AppDelegate class that is also the delegate for CLLocationManager
to receive didEnterRegion
callbacks. It is critical that the delegate for CoreLocation be the app's central AppDelegate in order to get the OS to launch the app.
I am not familiar enough with Cordova on iOS to know how to bind a plugin or other native interface layer to the native AppDelegate. Solving this problem is the key to making it work.
回答2:
I would assume as long as you have implemented CoreBluetooth's state preservation and restoration it shouldn't be a problem.
I have used this document from Apple as a reference:
https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html
The section "Performing Long-Term Actions in the Background" and below is most relevant to your use case. If you have already implemented that, could you post some code?
EDIT: I looked at the Evothings libraries and I don't think any of the plugins there will allow you to wake the app up when closed/terminated.
I think in this case you will need to write your own Cordova plugin.
That code would look something like this on the Objective-C side:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if([launchOptions objectForKey:@"UIApplicationLaunchOptionsLocationKey"])
{
self.beaconManager = [ESTBeaconManager new];
self.beaconManager.delegate = self;
// don't forget the NSLocationAlwaysUsageDescription in your Info.plist
[self.beaconManager requestAlwaysAuthorization];
[self.beaconManager startMonitoringForRegion:[[ESTBeaconRegion alloc]
initWithProximityUUID:ESTIMOTE_PROXIMITY_UUID
identifier:@"AppRegion"]];
}
return YES;
}
-(void)beaconManager:(ESTBeaconManager *)manager didEnterRegion:(ESTBeaconRegion *)region
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = @"Enter region";
notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
-(void)beaconManager:(ESTBeaconManager *)manager didExitRegion:(ESTBeaconRegion *)region
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = @"Exit region";
notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
来源:https://stackoverflow.com/questions/32616937/estimote-ibeacons-and-cordova-sending-push-notifications-when-app-is-killed-io