Cordova应用监控会在应用处于kill状态时改进estimmote /iBeacon



我已经在cordova应用程序中安装了https://github.com/katzer/cordova-plugin-background-mode和https://github.com/katzer/cordova-plugin-local-notifications。

当我的应用程序关闭时,我试图监控后台的信标,并在检测到信标并处于区域时发送通知。

使用这两个插件,当用户退出应用程序屏幕时,应用程序成功工作,但当用户完全杀死进程时,应用程序仍在运行,但不工作。

这可以单独使用Javascript完成,还是我必须修改AppDelegate.m中的代码?

我已经尝试使用以下代码:

#import "AppDelegate.h"
#import "MainViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface AppDelegate ()
@property (nonatomic, strong) CLLocationManager *locationManager;
@property(nonatomic, assign) BOOL notifyEntryStateOnDisplay;
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    self.viewController = [[MainViewController alloc] init];
    self.locationManager = [[CLLocationManager alloc] init];
    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region{
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    NSLog(@"BLUETOOTH");
    if(state == CLRegionStateInside)
    {
        notification.alertBody = [NSString stringWithFormat:@"You are inside region %@", region.identifier];
    }
    else if(state == CLRegionStateOutside)
    {
        notification.alertBody = [NSString stringWithFormat:@"You are outside region %@", region.identifier];
    }
    else
    {
        return;
    }
    [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
@end

尽管应用程序没有启动。我还更改了xCode [General -> Capabilities]中的设置,使后台模式和推送通知处于ON状态。

应用程序做它需要做的事情,即使在后台没有被杀死,但一旦应用程序被杀死,它就会停止。我正在尝试发送一个通知,当应用程序离线时,信标在范围内,以便用户可以打开应用程序。

几点说明:

  1. 你必须设置self.locationManager.delegate=self,并使你的AppDelegate实现CLLocationManagerDelegate协议,特别是didEnterRegion方法,当发现信标时应该调用。

  2. 信标监控必须在AppDelegate中配置和初始化。你不能依靠插件来做到这一点,因为当应用程序切换到前台时,它不一定会使信标监控处于活动状态。

  3. 当测试时,在AppDelegate的didEnterRegion方法中设置一个日志消息或断点,这样你就知道它是否被调用了。

最新更新