我想在收到静默通知时将用户位置更新到服务器。我已启用推送通知和后台模式作为远程通知和位置。但是
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
当应用程序处于终止或未运行状态时,不会调用 my 。我正在通过将位置数据发送到 pubnub 来对此进行测试。主要问题是,当我开始编写这部分代码时,它工作得很好,但过了一段时间它就停止了工作,在那之后直到现在我都在努力解决这个问题。任何帮助将不胜感激。
这是我的代码:
// 在应用程序委托中
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler{
handler(UIBackgroundFetchResultNewData);
[[RemoteNotification sharedInstance] pushNotificationReceived:userInfo];
}
//远程通知模式
- (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
}
}