0

I am working an application in which I am fetching steps from Apple Health application. I need to update these steps on my server when the application is terminated or in the background. I implemented silent push notification and it's working fine. I am getting sound when notification is received(for testing purpose) when the application is terminated or in the background. When the application is in background steps are updating on the server but if the application is terminated only sound plays but steps not updating on the server.

    NSString *leastJoinedGameDate = [[NSUserDefaults standardUserDefaults] valueForKey:kLeastJoinedDate];

    [[GSHealthKitManager sharedManager] fetchStepsHistoryWithStartDate:leastJoinedGameDate andCompletionHandler:^(NSMutableArray *results, NSError *error) {
        NSLog(@"%@",results);
        if (error) {
            if (_completionHandler) {
                _completionHandler(UIBackgroundFetchResultFailed);
            }
        }
        else{
            [self uploadStepsHistoryToServerWithStepsHistory:results];
        }
        NSLog(@"%@",error);
    }];

 -(void)uploadStepsHistoryToServerWithStepsHistory:(NSArray *)stepsArray{

    if (![AppHelper isNetworkAvailableWithoutAlert]) {
        return;
    }

    NSMutableDictionary *requestDict = [NSMutableDictionary new];
    requestDict[kOption] = @"update_steps";
    requestDict[kUserID] = [[UserDetails sharedInstance] userID];
    requestDict[@"progress"] = stepsArray;
    [WebServicesHandler performPOSTRequestWithURL:kServerURL andParameters:requestDict andAcessToken:[[UserDetails sharedInstance] accessToken] completion:^(id result, NSError *error) {
        if (_completionHandler) {
            _completionHandler(UIBackgroundFetchResultNoData);
        }

        NSString *currentDateStr = [AppHelper getCurrentDateInStringWithoutTime];
        [[NSUserDefaults standardUserDefaults] setObject:currentDateStr forKey:kPreviousDayDate];
        [[NSUserDefaults standardUserDefaults] synchronize];
        NSLog(@"%@",result);
    }];
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Vikash Rajput
  • 445
  • 5
  • 21
  • Silent notifications only work when the application is in the background not when it's terminated. You should use background fetch instead `application.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)` and then implement this method in the AppDelegate class `func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void)` I can't give a detailed answer so I'll just comment it instead – DatForis May 29 '17 at 12:31
  • It works. I implemented with sound. when notifications received sound plays when the application is terminated. – Vikash Rajput May 29 '17 at 12:48
  • While the app is terminated receiving the notification doesn't mean that the didReceiveNotification method runs even with the content-available set to 1 – DatForis May 29 '17 at 13:01
  • But I am getting sound when notification is received. This means OS just play notification sound and not called didReceiveNotification? – Vikash Rajput May 30 '17 at 09:35
  • yes, it won't get called when the app is terminated. It will only get called if the application is in the background, or foreground. When the app is terminated the only way I know of is the method I mentioned above and using location-based services like significant location change, or using CLRegions. It's a part of Apple's restrictions to make the phone have a better battery life. If apps were to constantly run in the background whenever the developer wants them to then it would drain the battery significantly – DatForis May 30 '17 at 13:05
  • check out this [answer](https://stackoverflow.com/questions/19068762/will-ios-launch-my-app-into-the-background-if-it-was-force-quit-by-the-user/29231395#29231395), it can do what you want, but it might get your app rejected – DatForis May 30 '17 at 13:14

0 Answers0