Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receiving Push Notifications while in background

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?

like image 757
Brandon Avatar asked Jan 31 '13 00:01

Brandon


People also ask

How do I get notification data when an app is in the background?

When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default. This includes messages that contain both notification and data payload (and all messages sent from the Notifications console).

Does an app need to be open for push notifications?

For app publishers, push notifications are a way to speak directly to the user. They don't get caught in spam filters or forgotten in an inbox: as a result, push click-through rates can be twice as high as email. They can remind users to use an app, even if it isn't open.

Do push notifications work when app is closed iOS?

Apple does not offer a way to handle a notification that arrives when your app is closed (i.e. when the user has fully quit the application or the OS had decided to kill it while it is in the background). If this happens, the only way to handle the notification is to wait until it is opened by the user.

What is background push service?

It wakes your app in the background and gives it time to initiate downloads from your server and update its content. Important. The system treats background notifications as low priority: you can use them to refresh your app's content, but the system doesn't guarantee their delivery.


2 Answers

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

like image 65
Muhammad Ali Yousaf Avatar answered Oct 20 '22 01:10

Muhammad Ali Yousaf


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.

like image 21
Eran Avatar answered Oct 20 '22 00:10

Eran