关于重大位置变更的本地通知
Local Notification on significant location change
我正在开发一个原型,用于在位置发生重大变化时通知用户。当应用程序为 closed/terminated 时,我发送一个本地通知来通知用户。通知工作完美,但是,仅一次。虽然我在 didFinishLaunching:
收到位置更改,但我没有收到本地通知。下面是我的简单代码。
在ViewController
我注册通知。
#import "LocationViewController.h"
@interface LocationViewController ()
@end
@implementation LocationViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Registerimg for Motification
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
}
下面是我的didFinishLaunching:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog(@"didFinishLaunchingWithOptions");
self.shareModel = [LocationShareModel sharedModel];
self.shareModel.afterResume = NO;
[self addApplicationStatusToPList:@"didFinishLaunchingWithOptions"];
UIAlertView * alert;
//We have to make sure that the Background App Refresh is enable for the Location updates to work in the background.
if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusDenied){
alert = [[UIAlertView alloc]initWithTitle:@""
message:@"The app doesn't work without the Background App Refresh enabled. To turn it on, go to Settings > General > Background App Refresh"
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil, nil];
[alert show];
}else if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusRestricted){
alert = [[UIAlertView alloc]initWithTitle:@""
message:@"The functions of this app are limited because the Background App Refresh is disable."
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil, nil];
[alert show];
} else{
// When there is a significant changes of the location,
// The key UIApplicationLaunchOptionsLocationKey will be returned from didFinishLaunchingWithOptions
// When the app is receiving the key, it must reinitiate the locationManager and get
// the latest location updates
// This UIApplicationLaunchOptionsLocationKey key enables the location update even when
// the app has been killed/terminated (Not in th background) by iOS or the user.
if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) {
NSLog(@"UIApplicationLaunchOptionsLocationKey");
[[UIApplication sharedApplication] cancelAllLocalNotifications];
//Establish notification details
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate date];
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.repeatInterval = 0;
notification.alertBody = [NSString stringWithFormat:@"Success"];
notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
// This "afterResume" flag is just to show that he receiving location updates
// are actually from the key "UIApplicationLaunchOptionsLocationKey"
self.shareModel.afterResume = YES;
self.shareModel.anotherLocationManager = [[CLLocationManager alloc]init];
self.shareModel.anotherLocationManager.delegate = self;
self.shareModel.anotherLocationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
self.shareModel.anotherLocationManager.activityType = CLActivityTypeOtherNavigation;
if(IS_OS_8_OR_LATER) {
[self.shareModel.anotherLocationManager requestAlwaysAuthorization];
}
[self.shareModel.anotherLocationManager startMonitoringSignificantLocationChanges];
[self addResumeLocationToPList];
}
}
return YES;
}
我遵循 this 位置更改教程,并向其中添加了我的通知方法。
在每次重大位置更改时,我哪里会延迟收到通知?
我猜您正在使用 iOS 8 或更高版本作为部署目标。
你有没有在你info.plist中添加NSLocationWhenInUseUsageDescription?我在你的教程中没有看到,这对于位置库是强制性的。
经过一番调查,我自己得到了答案。
当应用程序正在侦听显着的位置变化时,如果应用程序关闭,当位置发生变化时,会调用 didFinishLaunchingWithOptions:
并且我会收到问题中讨论的本地通知。
这意味着应用程序已启动,但不在前台。
对于下一次位置更改,不会调用 didFinishLaunchingWithOptions:
,因为应用程序已经启动。因此,通用委托方法 CLLocationManagerDelegate
、didUpdateLocations:
之后将调用所有位置更改。
我正在开发一个原型,用于在位置发生重大变化时通知用户。当应用程序为 closed/terminated 时,我发送一个本地通知来通知用户。通知工作完美,但是,仅一次。虽然我在 didFinishLaunching:
收到位置更改,但我没有收到本地通知。下面是我的简单代码。
在ViewController
我注册通知。
#import "LocationViewController.h"
@interface LocationViewController ()
@end
@implementation LocationViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Registerimg for Motification
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
}
下面是我的didFinishLaunching:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog(@"didFinishLaunchingWithOptions");
self.shareModel = [LocationShareModel sharedModel];
self.shareModel.afterResume = NO;
[self addApplicationStatusToPList:@"didFinishLaunchingWithOptions"];
UIAlertView * alert;
//We have to make sure that the Background App Refresh is enable for the Location updates to work in the background.
if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusDenied){
alert = [[UIAlertView alloc]initWithTitle:@""
message:@"The app doesn't work without the Background App Refresh enabled. To turn it on, go to Settings > General > Background App Refresh"
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil, nil];
[alert show];
}else if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusRestricted){
alert = [[UIAlertView alloc]initWithTitle:@""
message:@"The functions of this app are limited because the Background App Refresh is disable."
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil, nil];
[alert show];
} else{
// When there is a significant changes of the location,
// The key UIApplicationLaunchOptionsLocationKey will be returned from didFinishLaunchingWithOptions
// When the app is receiving the key, it must reinitiate the locationManager and get
// the latest location updates
// This UIApplicationLaunchOptionsLocationKey key enables the location update even when
// the app has been killed/terminated (Not in th background) by iOS or the user.
if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) {
NSLog(@"UIApplicationLaunchOptionsLocationKey");
[[UIApplication sharedApplication] cancelAllLocalNotifications];
//Establish notification details
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate date];
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.repeatInterval = 0;
notification.alertBody = [NSString stringWithFormat:@"Success"];
notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
// This "afterResume" flag is just to show that he receiving location updates
// are actually from the key "UIApplicationLaunchOptionsLocationKey"
self.shareModel.afterResume = YES;
self.shareModel.anotherLocationManager = [[CLLocationManager alloc]init];
self.shareModel.anotherLocationManager.delegate = self;
self.shareModel.anotherLocationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
self.shareModel.anotherLocationManager.activityType = CLActivityTypeOtherNavigation;
if(IS_OS_8_OR_LATER) {
[self.shareModel.anotherLocationManager requestAlwaysAuthorization];
}
[self.shareModel.anotherLocationManager startMonitoringSignificantLocationChanges];
[self addResumeLocationToPList];
}
}
return YES;
}
我遵循 this 位置更改教程,并向其中添加了我的通知方法。
在每次重大位置更改时,我哪里会延迟收到通知?
我猜您正在使用 iOS 8 或更高版本作为部署目标。 你有没有在你info.plist中添加NSLocationWhenInUseUsageDescription?我在你的教程中没有看到,这对于位置库是强制性的。
经过一番调查,我自己得到了答案。
当应用程序正在侦听显着的位置变化时,如果应用程序关闭,当位置发生变化时,会调用 didFinishLaunchingWithOptions:
并且我会收到问题中讨论的本地通知。
这意味着应用程序已启动,但不在前台。
对于下一次位置更改,不会调用 didFinishLaunchingWithOptions:
,因为应用程序已经启动。因此,通用委托方法 CLLocationManagerDelegate
、didUpdateLocations:
之后将调用所有位置更改。