Adam
Adam

Reputation: 1486

HealthKit: getting a notification triggered by physical activity, background and terminated state (Obj-C, iOS 10.3)

Let's say we're using step count as the trigger- if the user takes 10 steps, I want a notification to pop up saying so.

This question has been answered here: Healthkit background delivery when app is not running

However, I haven't been able to get it to work. If I walk around with the app in the foreground the step count will get updated. If I leave the app in the background the step count only seems to be updated after I reopen the app. So the background query doesn't seem to be working.

I've tried enabling Background Modes, no effect; I've tried leaving the app in the background for several hours thinking it might take a while for the data to be sent- nothing.

Someone elsewhere was saying to try StatisticsCollectionQuery because StatisticsQuery was broken- any truth to that? StatisticsCollectionQuery is not really ideal for what I'm doing.

Here's my query code:

[self.healthStore enableBackgroundDeliveryForType:[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]
                                        frequency:HKUpdateFrequencyImmediate
                                   withCompletion:^(BOOL success, NSError * _Nullable error) {
                                       if (error) {

                                           // Perform Proper Error Handling Here...
                                           NSLog(@"*** An error occured while setting up background updates. %@ ***",
                                                 error.localizedDescription);
                                       }
                                       if (success == YES) {
                                           backgroundEnabled = YES;
                                       }
                                   }];


// set up running observer

HKSampleType *sampleType =
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];

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

     if (error) {

         // Perform Proper Error Handling Here...
         NSLog(@"*** An error occured while setting up the stepCount observer. %@ ***",
               error.localizedDescription);
         // Error popup

         return;
     }

     // Take whatever steps are necessary to update your app's data and UI
     // This may involve executing other queries
     [self updateDailyStepCount];

     // If you have subscribed for background updates you must call the completion handler here.
     completionHandler();

 }];

[self.healthStore executeQuery:query];

Upvotes: 1

Views: 1049

Answers (1)

Rob Napier
Rob Napier

Reputation: 299565

The fact that you requested HKUpdateFrequencyImmediate suggests you may not have carefully studied the docs for enableBackgroundDeliveryForType:frequency:withCompletion:. The docs have a large "Note" box explaining that step count will never be provided more often then hourly.

If that's the case, I suggest closely reading the Discussion in that section, because HK is tricky and non-obvious. For example, are you calling this query in application:didFinishLaunchingWithOptions:? Have you tried reinstalling your app (I would delete and reinstall from scratch). If you don't call the completionHandler() three times, iOS will stop notifying you. I recommend building a small, simple app that does nothing but log the step count; use that to work through all the details.

See also HealthKit (iOS) wont deliver data in background (objC). It's unclear if you've already implemented these points from your discussion.

Upvotes: 2

Related Questions