Send silent push notifications to the app, update location and send to server in background

I want to send a quiet push notification to an application that is in the background, then select the current location of the user and send it to the web service.

I implemented push notification methods, as well as those two:

- (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
{

}

I also created a method that sends the location to the server:

- (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);
        }

    }];
}

I just can’t understand how they all work together, whether Apple will approve it, whether it is possible and where the background image of the location is extracted.

Thanks in advance.

+4
source share
1 answer

, , . , , , .

// 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: , " ".

iOS 9 (requestLocation iOS 9), . - . , . , , , " ", , .

, iOS9 , , , iOS8, .

+1

Source: https://habr.com/ru/post/1607873/


All Articles