Receiving Push Notifications while in background

Brandon picture Brandon · Jan 31, 2013 · Viewed 49.4k times · Source

I know this is covered in a lot of places, but I cannot figure this out. I use Urban Airship for push notifications. Everything seems to be fine except that when my app is in the background - didReceiveRemoteNotification is not getting called. It works when in the foreground - I am able to properly process messages. And I am able to get to messages from launch options if message is tapped from notifications center. But when in the background, a message it send - iOS displays the alert - didReceiveRemoteNotification is not called. When I tap on my Application icon (not from within notification center) the app comes to the foreground and I have no idea the notification is present. Any ideas?

Answer

Muhammad Ali Yousaf picture Muhammad Ali Yousaf · Jun 1, 2015

application:didReceiveRemoteNotification: will call in the background only when you have added content-available key with value 1 into the notification payload. In case of the Urban Airship, you can send Test Push under the Setting tab. Sample Payload for Push Notifications:

{
  "aps": {
    "alert": "aaaa",
    "badge": "+1",
    "content-available": "1"
  },
  "device_tokens": [
    "86BA71E361B849E8312A7B943BA6B26A74AB436381CF3FEE3CD9EB436A12A292"
  ]
}

Apple has clearly mentioned in his documentation....

For a push notification to trigger a download operation, the notification’s payload must include the content-available key with its value set to 1. When that key is present, the system wakes the app in the background (or launches it into the background) and calls the app delegate’s application:didReceiveRemoteNotification:fetchCompletionHandler: method. Your implementation of that method should download the relevant content and integrate it into your app. https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html