1

In my app I am using a daily silent push to trigger some calculations inside my app and based on the calculation result I am triggering a local notification.

Say for example: A daily goal.

Once the silent notification reaches my app, I trigger a background method to calculate the user data and compare whether his/ her data achieved a goal and if yes I trigger a local notification so that user can open the app and check.

It is not working consistently, sometimes I get it and sometimes don't. When I debugged, what I saw was that the execution get paused in between and execution gets resumed only if the user opens the app again or I trigger a silent push again. Not sure why the background execution (the calculation) gets paused, and if I trigger a push or if i manually try to open the app, I can see the breakpoint appearing back and it continues from the place it was paused.

I am wondering whether it is because of some time limit??

UPDATE:

I am using UrbanAirshipSDK and they have some handlers overridden and I am using the below method to handle the notification. This is written in the appdelegate and gets called when I get a notification when app is in background.

/**
 * Called when a push notification is received while the app is running in the background
 * for applications with the "remote-notification" background mode.  
 * Overridden by receivedBackgroundNotification:fetchCompletionHandler.
 *
 * @param notification The notification dictionary.
 */
- (void)receivedBackgroundNotification:(NSDictionary *)notification;

I first check for content-available in the payload and treat as silent and do the calculations. This works fine intermittently but not consistently. I am closing towards release dates and I really worried.

anoop4real
  • 7,598
  • 4
  • 53
  • 56
  • what does this calculation involve? – Wain Apr 06 '16 at 12:28
  • Coredata fetch and comparison of values fetched with the threshold goal values. Size of records data is very small – anoop4real Apr 06 '16 at 12:30
  • 1
    show the code you're using, you should have ample time for a fetch and local comparison – Wain Apr 06 '16 at 12:34
  • Due to some NDA issues, I wont be able to paste the code, I will try to write a generic version add it here, but what I am trying to figure out is why it works sometimes and sometimes dont – anoop4real Apr 06 '16 at 12:41
  • 1
    Yes, show some code. You probably need to call `beginBackgroundTaskWithExpirationHandler` to get enough background time. – Paulw11 Apr 06 '16 at 12:56

1 Answers1

0

I assume that you use:

- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler

In this case you should not forget to call completion handler and according to Apple docs you have only 30 seconds to do so:

As soon as you finish processing the notification, you must call the block in the handler parameter or your app will be terminated. Your app has up to 30 seconds of wall-clock time to process the notification and call the specified completion handler block. In practice, you should call the handler block as soon as you are done processing the notification. The system tracks the elapsed time, power usage, and data costs for your app’s background downloads. Apps that use significant amounts of power when processing remote notifications may not always be woken up early to process future notifications.

s0urcer
  • 312
  • 2
  • 7