我希望在收到静默通知时将用户位置更新为服务器。我已启用推送通知和后台模式作为远程通知和位置。
但我的
当应用程序处于被杀或未运行状态时,不会调用-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
。我正在通过向pubnub发送位置数据来测试它。主要的问题是,当我开始编码这部分时,它工作得很好,但经过一段时间它已经停止工作,之后到现在为止我正在努力解决这个问题。任何帮助将不胜感激。
这是我的代码:
//在app delegate
中-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler{
handler(UIBackgroundFetchResultNewData);
[[RemoteNotification sharedInstance] pushNotificationReceived:userInfo];
}
// RemoteNotification Modal
- (void) pushNotificationReceived:(NSDictionary *) userInfo
{
[self registerBackgroundTaskHandler];
//get the location here, or start getting the location
locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy= kCLLocationAccuracyBest;
locationManager.delegate = self;
[locationManager startUpdatingLocation];
}
- (void) registerBackgroundTaskHandler
{
__block UIApplication *app = [UIApplication sharedApplication];
self.backgroundTaskId = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:self.backgroundTaskId];
self.backgroundTaskId = 0;
}];
}
- (void) endBackgroundTask
{
if (self.backgroundTaskId)
{
UIApplication *app = [UIApplication sharedApplication];
[app endBackgroundTask:self.backgroundTaskId];
self.backgroundTaskId = 0;
}
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
[locationManager stopUpdatingLocation];
[[DHWebservices sharedInstance] sendLocationWithLocation:[locations objectAtIndex:0] success:^(NSDictionary *data) {
[self endBackgroundTask];
} errorBlock:^(id error) {
}];
}
我正在使用Firebase使用邮递员发送通知。
这是我的通知格式:
{
"to" : "fF97NwwMTvw:APA91bERqtpIxpSVXNujO_Wl1dVuPXuaJyNP2ygtVlNXxp618ykSubhQZXnh5BS3VPkqdto0lzPzJtQIzR0OIgq8oFZAuhhWfdWim5XRlxv3ut9Y1QGzu6VafNU3OqU8pJT75gg9J3-g",
"content_available": true,
"priority": "high",
"data" : {
"key1" : "abc",
"key2" : 123
}
}