When a push notification arrives, it plays audio in the background with application:didReceiveRemoteNotification:fetchCompletionHandler:.
To get it started, I press the home button, then lock screen button, then the screen is off, then send a push notification. This works when app state is UIApplicationStateBackground and I see the state by logging it to a file. Sometimes however when the app is backgrounded it mysteriously goes into the foreground right before application:didReceiveRemoteNotification:fetchCompletionHandler: and the app state is UIApplicationStateInactive in application:didReceiveRemoteNotification:fetchCompletionHandler:. Here's the log:
AppDelegate applicationDidEnterBackground: called
AppDelegate applicationWillEnterForeground: called
AppDelegate application:didReceiveRemoteNotification:fetchCompletionHandler: called. currentThread 0x174261900 isMainThread 1
AppDelegate application:didReceiveRemoteNotification:fetchCompletionHandler: applicationState == UIApplicationStateInactive
On some phones in application:didReceiveRemoteNotification:fetchCompletionHandler: it consistently shows UIApplicationStateBackground and on others it is UIApplicationStateInactive. Any idea why this happens?
In modern operating systems, background processes are either "suspended" so they can be pulled up quicker (state is
UIApplicationStateBackground
) or their memory is deallocated to give memory to a more frontal process. In this case, it becomes yourUIApplicationStateInactive
. This happens when your device has an intense process up and needs the RAM for it.