Estimote iBeacons 和 Cordova - 在 iOS 应用程序被杀死时发送推送通知

3
我正在使用Apache Cordova,Evothings Javascript插件和Estimote iBeacons制作一个应用程序。当应用程序在后台(未完全关闭)时,我已经成功地通过Phonegap的PushPlugin实现了推送通知,但是我想更进一步,在应用程序完全关闭时也能够接收到iBeacons范围内的推送通知。
我在Estimote社区和Stack Overflow上看到过使用原生Xcode开发实现此功能的各种帖子,但没有使用Cordova/Evothings Javascript SDK。
我的应用程序目前运行EstimoteBeacons.startMonitoringForRegion()函数,当应用程序关闭时可以在后台工作,但是在应用程序被杀死时似乎根本不执行任何代码。
我尝试运行EstimoteBeacons.requestAlwaysAuthorization();也没有任何效果。
我还尝试使用Cordova local notification plugin,但当应用程序被杀死时也不起作用,这导致我相信当应用程序完全关闭时没有任何代码被执行。
如果有人能够解释一下这个问题,我会非常感激。
谢谢。

请问能给我你的代码吗? - Vinod Kumar Marupu
2个回答

2
当应用程序没有在iOS上运行时,使其正常工作的关键是创建一个本地的AppDelegate类,并使其成为CLLocationManager的委托以接收didEnterRegion回调。关键是CoreLocation的委托必须是应用程序的中央AppDelegate,以便让操作系统启动应用程序。
我不熟悉Cordova在iOS上如何将插件或其他本地接口层绑定到本地AppDelegate。解决这个问题是使其正常工作的关键。

-1

我认为只要你已经实现了CoreBluetooth的状态保存和恢复,这应该不是一个问题。

我使用了苹果公司的这份文档作为参考:

https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html

“在后台执行长期操作”部分及以下内容最符合您的使用情况。 如果您已经实现了这一点,能否发布一些代码呢?

编辑: 我查看了Evothings库,我不认为那里的任何插件都可以在应用程序关闭/终止时唤醒它。

我认为在这种情况下,您需要编写自己的Cordova插件。

在Objective-C方面,该代码可能如下所示:

- (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];
}

抱歉,我没有提到我正在使用带有Cordova的EvoThings Javascript插件。我不确定直接实现CoreBluetooth是否可行,因为这通常是在Javascript插件内部的某种函数中完成的。 - JamesG

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接