Does HKObserverQuery can receive notification when the application not running(Killed)?
Asked Answered
B

0

1

My requirement is to register for any one of the Health data like steps, weight, heart rate, etc., for background delivery using enableBackgroundDeliveryForType: method. And then create a Observer query for the same Health data to check.

  1. In this scenario, if my application is killed by the user forcely and did changes to Health data using Health app. By this stage is it possible to get the notification to My application?

  2. Is it mandatory to register for any of the background modes, to get notified for health data modifications through HKObserverQuery?

EDIT 1:

- (void)requestAuthorizationToShareTypes:(NSSet *)typesToShare readTypes:(NSSet *)typesToRead completion:(void (^)(BOOL, NSError *))completion
{
    if ([HKHealthStore isHealthDataAvailable]) {

        [self.healthStore requestAuthorizationToShareTypes:typesToShare readTypes:typesToRead completion:^(BOOL success, NSError *error) {

            if(success)
            {
                self.authorizationSuccess = YES;
                completion(YES, nil);
            }
            else
            {
                [MyUtilities showAlertWithTitle:@"Authorization fail!" message:@"You didn't allow to access Health data."];
                completion(NO, error);
                return;
            }
        }];
    }
    else
    {
        [MyUtilities showAlertWithTitle:@"Sorry!" message:@"Your device does not support Health data."];
        NSDictionary *errorDictionary = @{ NSLocalizedDescriptionKey : @"Your device doesn't support Health Kit."};
        NSError *error = [[NSError alloc] initWithDomain:@"" code:2 userInfo:errorDictionary];
        completion(NO, error);
        return;
    }
}
- (void)authorizeConsumeHealth:(void (^)(BOOL, NSError *))completion
{
    NSSet *readObjectTypes  = [NSSet setWithObjects:
                               [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCaffeine],
                               [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryCalcium],
                               [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryChloride],
                               [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryIron],
                               [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount],
                               [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBloodPressureSystolic],
                               [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBloodPressureDiastolic],                               [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount],
                               nil];

    [self requestAuthorizationToShareTypes:nil readTypes:readObjectTypes completion:^(BOOL success, NSError *error) {
        if(completion)
        {
            completion(success, error);
            [self observeStepCountChanges];
        }

    }];

}

- (void)observeStepCountChanges
{
    HKSampleType *sampleType = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];

    [self.healthStore enableBackgroundDeliveryForType:sampleType frequency:HKUpdateFrequencyImmediate withCompletion:^(BOOL success, NSError *error) {}];


    HKObserverQuery *query = [[HKObserverQuery alloc] initWithSampleType:sampleType predicate:nil updateHandler:^(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError *error) {
        if(error)
        {

            NSLog(@"Steps count observer completion block. Error: %@", error);
        }
        else
        {
            NSLog(@"Steps count observer completion block");

        }
    }];
    [self.healthStore executeQuery:query];


}

Please help me. Thanks.

Bibb answered 19/3, 2015 at 10:40 Comment(6)
@tshortli, I have applied HKObserverQuery in my application and then enabled background delivery too.Bibb
Wantedly enabled location services in background mode though I don't have a requirement for location services, for the sake of HealthKit I enabled.Bibb
Please add the snippet of code you've used. It's hard to say what might be wrong without seeing what you have tried already.Joppa
Bhanu - were you able to figure out if your app get woken up via background delivery even if it is explicitly killed by the user?Oday
@PratikStephen - I did not worked on it after that words. I did this work as a part of R&D.Bibb
@PratikStephen look at my answer here: https://mcmap.net/q/363332/-healthkit-background-delivery-when-app-is-not-running. /cc BhanuPrakashBartender

© 2022 - 2024 — McMap. All rights reserved.