【问题标题】:Why does UILocalNotification with region not fire?为什么带有区域的 UILocalNotification 不会触发?
【发布时间】:2014-08-31 05:10:14
【问题描述】:

我已按照WWDC 2014 session "What's New in iOS Notifications" 中给出的示例设置跨越区域边界时的UILocalNotification 交付。

但是,我的通知永远不会在运行 iOS beta 3 的设备或模拟器上触发。当我使用 fireDate 而不是 region 进行测试时,通知工作正常(不能同时使用这两个 - 这是不允许的)。

我在Info.plist 中设置了NSLocationWhenInUseUsageDescription

这是我的 Obj-C 代码:

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface ViewController () <CLLocationManagerDelegate>

@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) CLLocation *notifLocation;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // test points at Apple HQ
    self.notifLocation = [[CLLocation alloc] initWithLatitude:37.331741 longitude:-122.030333];

    [self setupLocationMonitoring];
}

- (void) setupLocationMonitoring
{
    if (self.locationManager == nil) {
        self.locationManager = [[CLLocationManager alloc] init];
    }

    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    self.locationManager.distanceFilter = 10; // meters

    // iOS 8+ request authorization to track the user’s location
    if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [self.locationManager requestWhenInUseAuthorization];
    }

    [self.locationManager startUpdatingLocation];
}

- (void)registerLocationNotification
{
    UILocalNotification *locNotification = [[UILocalNotification alloc] init];
    locNotification.alertBody = @"Hello!";

    // doesn't work
    locNotification.regionTriggersOnce = NO;
    locNotification.region = [[CLCircularRegion alloc] initWithCenter:self.notifLocation.coordinate radius:50 identifier:@"PlaceName"];

    // works:
//    locNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];

    UIApplication *application = [UIApplication sharedApplication];
    [application cancelAllLocalNotifications];
    [application scheduleLocalNotification:locNotification];
}

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    // check status to see if we’re authorized
    BOOL canUseLocationNotifications = (status == kCLAuthorizationStatusAuthorizedWhenInUse);
    if (canUseLocationNotifications) {
        [self registerLocationNotification];
    }
}

@end

注意:这个问题是关于 iOS 8 的新位置通知,而不是关于旧版本 iOS 上的区域监控。

【问题讨论】:

    标签: objective-c core-location uilocalnotification ios8


    【解决方案1】:

    您的代码对我来说很好用。以下是iPhone Simulator上的步骤:

    1) 打开 Simulator 应用程序并从顶部菜单中选择以下选项 Debug -> Location -> Custom Location 并在代码中输入坐标:(37.331741, -122.030333)

    2) 在 iPhone Simulator 上运行您的 Xcode 项目

    3) 应用后台

    4) 打开相同的顶部菜单项 Debug -> Location,但将其从 Custom Location 更改为 Freeway DriveCity Run。玩City Run/Freeway DriveCustom Location

    当我执行这些步骤时,我会看到通知。看起来它在适当的坐标是起始坐标的情况下不起作用,但是当坐标从其他坐标变为适当的坐标时它起作用。

    【讨论】:

    • 感谢您使用模拟器的程序。由于某种原因,我原来的模拟器程序没有弹出通知。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-08
    • 2014-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多