通过静默推送通知startUpdatingLocationManager
我正在创build需要在特定时间在后台唤醒的应用程序。
我努力了 :
-
UILocalNotification:但我不想使用UILocalNotification,因为它需要用户交互来点击通知,并且只有应用程序会唤醒并启动位置pipe理器。
-
我也用过
[locationManager startUpdatingLocation];
启用背景模式位置更新,这是工作,但它会占用大量的电池。
因此,使用新的iOS 7functionSilent pushNotification
和Silent pushNotification
didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:
方法我需要在后台启动位置pipe理器,
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{ NSLog(@"Receive RemoteNotification in fetchCompletionHandler with UserInfo:%@",userInfo); application.applicationIconBadgeNumber=0; __block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler:^{ [self.locationManager startUpdatingLocation]; [application endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }]; }
无声推送通知工作正常,
PushNotification的有效载荷:
{"aps" : {"content-available" : 1},"SilentPushId" : "07"}
但这不会启动位置经理,请有人帮助我。
编辑:
如果这是不可能的,请给我一些build议。
我已经成功地实现了这个使用无声Pushnotification及其调用startUpdatingLocation,我能够获取委托方法中的位置数据:
使用这个有效载荷:
{ "aps": { "content-available": 1 }, "SilentPush": "4" }
我已启用背景模式的位置和远程通知:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler { __block UIBackgroundTaskIdentifier bgTask =0; UIApplication *app = [UIApplication sharedApplication]; bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ [self.locationManager startUpdatingLocation]; }];
didUpdateLocations
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { lastLoc=[locations lastObject]; [logFile addLocObject:[NSString stringWithFormat:@"Loc: %@",lastLoc]]; }
你不能。 在应用程序处于后台时显式不允许启动位置服务。 用户必须知道应用程序打开的开始。
想象一下,如果你可以发送无声的通知来触发位置更新回到服务器,而用户不知道它,那么颠覆性的跟踪将是可用的。
你可以在后台做什么,是接收重要的位置事件和边界事件。 我可以想象利用这个function来保留最近的位置日志。 收到远程通知后,通过发送最后一个已知位置进行响应。 通过一些实验,我相信你可以细化这个,对电池的影响很小。
如果要在后台模式下更新位置,只需在plist文件中启用UIBackgroundModes即可。 请参阅苹果文档中的startUpdatingLocation描述 。
如果您启动此服务并且您的应用程序被暂停,系统会停止事件的传递,直到您的应用程序再次开始运行(在前台或后台)。 如果您的应用程序被终止,则新的位置事件的传送将完全停止。 因此,如果您的应用程序需要在后台接收位置事件,则必须在其Info.plist文件中包含UIBackgroundModes项(具有位置值)。
你可以启用这个在xcode5 +中的背景更新,如下所示。