1
votes

I am trying to receive a notification when in background mode. I don't want the user to launch my app on receiving an alert in the notification. But it seems like iOS doesn't send the notification to the callback function didReceiveRemoteNotification: Any inputs?

Is there something special I need to perform in order to receive a push in the background mode.

FYI : I tried using voip background mode also and still nothing!!!

Basically I want my app to receive a notification without launching my application and perform some action while application is running in background.

2

2 Answers

4
votes

But it seems like iOS doesn't send the notification to the callback function didReceiveRemoteNotification:

That's true. If your app is in the background, you will not get notified about an incoming push notification unless the user actually launches the app by interacting with the notification alert.

Basically I want my app to receive a notification without user having to launch my app/ user intervention and perform some action while running in background.

That's not possible.

1
votes

It is possible to get remote notifications and be woken up. But it is important to understand the current implementation and how it is affected by application behavior. All things are subject to change under Apple. As of this writing, here is what you need to know (the first 2 most people obviously grok):

  1. First and foremost, your application has to include "Remote notifications" as a Required Background Mode.
  2. Your application has to register for remote notifications as described in https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html
  3. You MUST be aware of which callback gets executed in your application (UIApplicationDelegate for Swift) based on your current application state (foreground, background, suspended, etc)
    • Foreground -- will call application:didReceiveRemoteNotification or more preferably application:didReceiveRemoteNotification:fetchCompletionHandler
    • Background/Suspended -- will ONLY (potentially) call application:didReceiveRemoteNotification:fetchCompletionHandler and you better call that fetchCompletionHandler within 30 seconds or your app gets terminated. If you didn't implement the method with the fetchCompletionHandler, then you will probably only be able to receive a badge/alert from the System IF you put those in the content of the APN message. Otherwise, you will see nothing.

Now, here is the catch. The current iOS profiles your application and THEN decides whether or not to actually wake up your app. If you spend a lot of time testing APN, it will eventually assume your app to be a bad citizen and it won't wake up your app (unless you are plugged into power at the very least). It will just wait until your app is subsequently launched (which you can encourage the user to do via the System badge/alert in your APN message), and then put that remote notification into the did/willFinishLaunchingWithOptions launchOptions under a key UIApplicationLaunchOptionsRemoteNotificationKey