向应用程序发送静默推送通知,更新位置并在后台发送到服务器
Send silent push notification to app, update location and send to server in background
我想向后台应用程序发送静默推送通知,然后获取当前用户位置并将其发送到 Web 服务。
我实现了推送通知方法以及这两个:
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSDate *fetchStart = [NSDate date];
[self sendLocationToServerWithCompletionHandler:^(UIBackgroundFetchResult result) {
completionHandler(result);
NSDate *fetchEnd = [NSDate date];
NSTimeInterval timeElapsed = [fetchEnd timeIntervalSinceDate:fetchStart];
NSLog(@"Background Fetch Duration: %f seconds", timeElapsed);
}];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
}
我还创建了一个将位置发送到服务器的方法:
- (void)sendLocationToServerWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSDictionary *params = @{
@"UserId" : self.userId,
@"Latitude" : self.latitude,
@"Longitude" : self.longitude
}
ServerManager *manager = [ServerManager sharedManager];
[manager sendLocationToServerWithCompletion:^(BOOL success) {
if (success)
{
completionHandler(UIBackgroundFetchResultNewData);
}
else
{
completionHandler(UIBackgroundFetchResultFailed);
}
}];
}
我只是不明白它们是如何协同工作的,Apple 会批准吗,这是否可能以及位置背景提取的位置。
提前致谢。
这里简要介绍了您可以做些什么来给您一个想法。它假设有一个模型 class 作为单例实现并且有一些伪代码。
// App delegate
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
completionHandler(UIBackgroundFetchResultNewData);
[[YourModel singleton] pushNotificationReceived: userInfo];
}
// Model
- (void) pushNotificationReceived:(NSDictionary *) userInfo
{
[self registerBackgroundTaskHandler];
get the location here, or start getting the location
[self sendLocationToServerWithCompletionHandler: your completion handler];
}
- (void) registerBackgroundTaskHandler
{
__block UIApplication *app = [UIApplication sharedApplication];
self.backgroundTaskId = [app beginBackgroundTaskWithExpirationHandler:^{
DDLogInfo(@"BACKGROUND Background task expiration handler called");
[app endBackgroundTask:self.backgroundTaskId];
self.backgroundTaskId = 0;
}];
}
- (void) endBackgroundTask
{
if (self.backgroundTaskId)
{
UIApplication *app = [UIApplication sharedApplication];
[app endBackgroundTask:self.backgroundTaskId];
self.backgroundTaskId = 0;
}
}
您需要先获取位置,然后才能发送。如果你只是得到一个位置并且你正在使用 iOS9 你可以使用 CLLocationManager:requestLocation: 你可以相对容易地将它放入我所说的 "get the location here".
如果您不使用 iOS 9(requestLocation 是 iOS 9 的新功能),它会稍微复杂一些。
如何使用位置管理器本身就是一个主题,这里的代码太多 post。在合并之前,您需要阅读和研究有关使用位置管理器的所有信息。
如果您需要位置更新流,它会变得更加复杂,并且它说 "or start getting the location" 涉及更多,然后在伪代码中隐含。
我的建议是,从 iOS9 开始并获得该位置的一个实例,然后在可行时添加更多功能或 iOS8 支持(如果需要)。
我想向后台应用程序发送静默推送通知,然后获取当前用户位置并将其发送到 Web 服务。
我实现了推送通知方法以及这两个:
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSDate *fetchStart = [NSDate date];
[self sendLocationToServerWithCompletionHandler:^(UIBackgroundFetchResult result) {
completionHandler(result);
NSDate *fetchEnd = [NSDate date];
NSTimeInterval timeElapsed = [fetchEnd timeIntervalSinceDate:fetchStart];
NSLog(@"Background Fetch Duration: %f seconds", timeElapsed);
}];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
}
我还创建了一个将位置发送到服务器的方法:
- (void)sendLocationToServerWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSDictionary *params = @{
@"UserId" : self.userId,
@"Latitude" : self.latitude,
@"Longitude" : self.longitude
}
ServerManager *manager = [ServerManager sharedManager];
[manager sendLocationToServerWithCompletion:^(BOOL success) {
if (success)
{
completionHandler(UIBackgroundFetchResultNewData);
}
else
{
completionHandler(UIBackgroundFetchResultFailed);
}
}];
}
我只是不明白它们是如何协同工作的,Apple 会批准吗,这是否可能以及位置背景提取的位置。
提前致谢。
这里简要介绍了您可以做些什么来给您一个想法。它假设有一个模型 class 作为单例实现并且有一些伪代码。
// App delegate
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
completionHandler(UIBackgroundFetchResultNewData);
[[YourModel singleton] pushNotificationReceived: userInfo];
}
// Model
- (void) pushNotificationReceived:(NSDictionary *) userInfo
{
[self registerBackgroundTaskHandler];
get the location here, or start getting the location
[self sendLocationToServerWithCompletionHandler: your completion handler];
}
- (void) registerBackgroundTaskHandler
{
__block UIApplication *app = [UIApplication sharedApplication];
self.backgroundTaskId = [app beginBackgroundTaskWithExpirationHandler:^{
DDLogInfo(@"BACKGROUND Background task expiration handler called");
[app endBackgroundTask:self.backgroundTaskId];
self.backgroundTaskId = 0;
}];
}
- (void) endBackgroundTask
{
if (self.backgroundTaskId)
{
UIApplication *app = [UIApplication sharedApplication];
[app endBackgroundTask:self.backgroundTaskId];
self.backgroundTaskId = 0;
}
}
您需要先获取位置,然后才能发送。如果你只是得到一个位置并且你正在使用 iOS9 你可以使用 CLLocationManager:requestLocation: 你可以相对容易地将它放入我所说的 "get the location here".
如果您不使用 iOS 9(requestLocation 是 iOS 9 的新功能),它会稍微复杂一些。 如何使用位置管理器本身就是一个主题,这里的代码太多 post。在合并之前,您需要阅读和研究有关使用位置管理器的所有信息。 如果您需要位置更新流,它会变得更加复杂,并且它说 "or start getting the location" 涉及更多,然后在伪代码中隐含。
我的建议是,从 iOS9 开始并获得该位置的一个实例,然后在可行时添加更多功能或 iOS8 支持(如果需要)。