Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

APNS: didReceiveRemoteNotification:fetchCompletionHandler did not run when app is in background (unless xcode is debugging)

I have this problem regarding APNS in my iPad 2 running iOS 8.1 . I searched on Google for a few days and tried every solution that I can find. However I cant seem to solve this problem.

What I did:

  1. Enabled capabilities: remote-notification, background fetch
  2. I have "content-available = 1" in the aps payload.
  3. I used NSLog() in didReceiveRemoteNotification:fetchCompletionHandler so I can see in device log clearly if the method is triggered

What I want to do:

I want to perform network request when I receive the APNS in background. I believe I can handle that part well using beginBackgroundTaskWithExpirationHandler and stuff. However, going down the investigation I found that the problem is due to didReceiveRemoteNotification:fetchCompletionHandler not been run when app is in background state.

Problem:

didReceiveRemoteNotification:fetchCompletionHandler can trigger when app is in foreground. But it is ONLY triggered in background when xcode is still debugging the app. Which means if I unplug the cable (the debugging session is over), it cannot trigger anymore. Same thing if I replug it back in. No response.

However, at the same time, I can still receive APNS with alert body and sound (although the method is still not triggered) as I see in the Device Log.

I tried every combination of "silent" push notification: sound = "", alert = "", etc. But nothing seems to work. Any help from the pros here?

Thank you.

like image 423
user3162662 Avatar asked Oct 31 '22 15:10

user3162662


1 Answers

Double check that Background App Refresh is turned on. I ran into this for a while after turning it off while traveling.

Also, you shouldn't need to use a background task inside of the didReceiveRemoteNotification:fetchCompletionHandler as it is already running in the background. Just make sure to call the appropriate completion handler when you're done with your network request.

Finally, didReceiveRemoteNotification:fetchCompletionHandler will not be called on a regular (non-silent) notification because regular notifications do not wake your app and execute code.


EDIT: Another weird obscure thing to check is the priority in the notification frame data - link to Apple Documentation - command F for 'priority' for the details. When I was doing this my Ruby gem that built the notification was using 10 by default, which is supposedly not allowed if you are only using 'content-available' and nothing else. Things got busy at work and I never got to test this using a priority of 5, but it may help.

like image 162
cuomo456 Avatar answered Nov 11 '22 13:11

cuomo456