didReceiveRemoteNotification inconsistent problems
I'm using the app delegate didReceiveRemoteNotification to fetch content using AFNetworking / NSURLSession with standard data tasks:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
//Get content here
}
I thought this was working fine but have found various cases where the app delegate method is never called. These are the circumstances I've mapped out:
- When the app is open it is always called
- When the app is run from the debugger it is always called
- When monitoring the device logs (via idevicesyslog) it is always called
- When plugged into a lightning cable it is mostly called
- When the device is unplugged it is sometimes (rarely?) called. In particular once it stops working it doesn't seem to start again.
- When the app is killed it is never called (As expected from Apple's documentation)
My app still works correctly either way but it's a much nicer experience if the content is already downloaded as opposed to having to wait after the app opens. Has anybody else encountered this issue and found a way to fix it? Is there anything my code could be doing which causes it to stop working?
Posted Mar 25, 2015, 12:52 AM UTC
Are you calling the completion handler within 30 seconds? Also how much computation are you doing during that 30 seconds? If you are slow and/or use too much battery then the OS may restrict calling you.
See docs
I'm calling the completion handler when my download completes and have a timer which fires after 27s just in case it can't complete. I'm doing a fair bit of work after the download completes so I'll see if I cutting that down makes a difference.
Thanks for the help.
Just did some tests. Even if I immediately call the completion handler without doing any processing the problem still persists. I found this post which seems to indicate that if you're handling a lot of push notifications then you're going to get throttled. Guess I'll just have to do what I can to reduce battery usage and accept that sometimes people will have to wait for data to load.