Receiving Push Notifications while in background
Asked Answered
J

4

38

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?

Joacima answered 31/1, 2013 at 0:9 Comment(0)
S
18

From the APNS programming guide :

Let’s review the possible scenarios when the operating delivers a local notification or a remote notification for an application.

The notification is delivered when the application isn’t running in the foreground. In this case, the system presents the notification, displaying an alert, badging an icon, perhaps playing a sound.

As a result of the presented notification, the user taps the action button of the alert or taps (or clicks) the application icon. If the action button is tapped (on a device running iOS), the system launches the application and the application calls its delegate’s application:didFinishLaunchingWithOptions: method (if implemented); it passes in the notification payload (for remote notifications) or the local-notification object (for local notifications).

If the application icon is tapped on a device running iOS, the application calls the same method, but furnishes no information about the notification.

I believe the last sentence describes your case, and explains why your application gets no information about the notification.

Sedulity answered 31/1, 2013 at 4:1 Comment(3)
Yep - I missed that. Well - that sure sounds like a real shortcoming. I suppose there are ways around it - but would be really straight forward for iOS to provide this. Thanks for the insight.Joacima
Is there other way to take information for push notification,when click application icon?I need one value,that is in payload.Musteline
@Sedulity , do you mean when app is background running, then taps the notification alert item, the app will not call the didReceiveRemoteNotification?Chanda
T
50

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

Tray answered 1/6, 2015 at 14:2 Comment(6)
Also, per the documentation, the "Remote notifications" option needs to be enabled in Background modes under Capabilities in the target settings.Tu
hmhm I use Google Cloud Messaging, they also implement content-available, on receiving (when clicked notification) the output tells me that content-available is outside the aps object. And ofcourse it ain't working for me. :( Apple why so difficult...Enervated
This answer must be marked as the right answer indeed.Danaedanaher
i am also working with the same scenario but i am using FCM . i am also stuck like you that i could not open push notification until i tap on the notification message . do you have any idea regarding how to solve this with FCM ?Teagan
May be following link is helpful. firebase.google.com/docs/cloud-messaging/concept-optionsTray
"content-available": "1" is actually very important.Recap
S
18

From the APNS programming guide :

Let’s review the possible scenarios when the operating delivers a local notification or a remote notification for an application.

The notification is delivered when the application isn’t running in the foreground. In this case, the system presents the notification, displaying an alert, badging an icon, perhaps playing a sound.

As a result of the presented notification, the user taps the action button of the alert or taps (or clicks) the application icon. If the action button is tapped (on a device running iOS), the system launches the application and the application calls its delegate’s application:didFinishLaunchingWithOptions: method (if implemented); it passes in the notification payload (for remote notifications) or the local-notification object (for local notifications).

If the application icon is tapped on a device running iOS, the application calls the same method, but furnishes no information about the notification.

I believe the last sentence describes your case, and explains why your application gets no information about the notification.

Sedulity answered 31/1, 2013 at 4:1 Comment(3)
Yep - I missed that. Well - that sure sounds like a real shortcoming. I suppose there are ways around it - but would be really straight forward for iOS to provide this. Thanks for the insight.Joacima
Is there other way to take information for push notification,when click application icon?I need one value,that is in payload.Musteline
@Sedulity , do you mean when app is background running, then taps the notification alert item, the app will not call the didReceiveRemoteNotification?Chanda
C
6

didReceiveRemoteNotification is calling ONLY if app in foreground or if app is just launched or is bought from background to foreground

link in Apple and some question

Chelseachelsey answered 31/1, 2013 at 0:35 Comment(0)
N
0

Method didFinishLaunchingWithOptions:(NSDictionary *)launchOptions parameter launchOptions one of the dictionary keys are UIApplicationLaunchOptionsRemoteNotificationKey which holds the pressed push notification info.

You can push received info after tour main root controller is initialised. I save it to some property and then push it after view is initialised.

if (launchOptions) {
    if ([launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]) {
        self.notificationToMakeAction = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    }
}
Nolpros answered 10/11, 2016 at 14:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.