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

210
skoll•

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

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.

2
askoruliOP•

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.

1
askoruliOP•

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.

1
ukraineftw•

When the device is unplugged it is sometimes (rarely?) called. In particular once it stops working it doesn't seem to start again.

I submitted a bugreport to Apple and their answer was 'it's meant to be like that'. So from their words it's not a bug but a feature =) The funniest thing is that on iOS 7.0 - 8.0.1 it worked like a charm and the method was called every time even when unplugged.

You can play with the priority key of the push payload, but it doesn't always help as well.

By the way, in the end that method is usually called. But with a 20-60 min delay in my experience.

Hope that helps

1
askoruliOP•

Are you saying it doesn't matter what you do or how little battery you use, it's always going to work like that?

1
ukraineftw•

On iOS 8.1.2 and 8.1.3 that was the case - it wouldn't matter of you had full charge or not, the method could be executed either right away or after 20-60 minutes. On iOS 8.2 for my couple of tests yesterday the method executed immediately 3-4 times in a row. But I haven't tested more than that.

1
criosist•

Why are you relying on remote notifications for data downloads?

Have you tried using background fetching ?

1
askoruliOP•

It's a messaging application so when I receive the push notification I want to download the data before the user can open the app so it feels instant.

I use background fetch as well to make sure data is up to date if any notifications got missed.

1
criosist•

the only thing is push notifications are not 100% apple states that some might just get lost or not sent so relying is probably not the best idea.

1
askoruliOP•

I know this and I'm not relying on them, the app works perfectly fine without them. If they did what I expect 95% of the time then I wouldn't be bothered.

All I'm trying to do is start the download before the user actions the push notification so that the data is available when the app opens without the user having the wait. Imagine receiving a notification for an SMS and when you open iMessage you have to wait ~3s for the message to show. It becomes a really jarring experience.

I think the only solution is to try and put as much data as possible into the push payload so in some circumstances I don't have to make a network call to get it.

1