An iOS concert app's silent push to refresh set times never reaches application(_:didReceiveRemoteNotification:fetchCompletionHandler:) on testers' iPhones; what do you check?
answer
- a capability, a payload, a handler, the system
- Background Modes: Remote notification
- content-available alone, no alert keys
- low priority, throttled, newest held only
- force-quit apps and the 30-second handler
basics
~20 sCheck that Background Modes has Remote notification enabled, that aps holds only content-available: 1 with no alert, sound or badge, and that the handler calls its completion handler on every path; then remember iOS throttles silent pushes and skips force-quit apps.
solid answer
~50 sFirst the app: the Background Modes capability needs Remote notification ticked; that background mode is what lets the system launch or wake the app for the push, and the method has to live on the app delegate. Then the payload: the `aps` dictionary carries only `"content-available": 1`, with custom keys such as a set-times version beside it, and the server sends it as a background push at low priority. Then the handler: it has up to 30 seconds and must call `completionHandler` with `.newData`, `.noData` or `.failed` on every path, or the app is terminated, and apps that use a lot of power there may not be woken early next time. Finally the system: background notifications are low priority and not guaranteed, more than two or three an hour risks throttling, only the newest held one survives, and a tester who force-quit the app gets nothing until they relaunch it.
code
swift · 23 linesimport UIKit
enum SetTimes {
static func refresh(version: Int, done: @escaping (Result<Bool, Error>) -> Void) { done(.success(true)) }
}
final class AppDelegate: UIResponder, UIApplicationDelegate {
// Runs only with Background Modes > Remote notifications enabled.
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
guard let version = userInfo["setTimesVersion"] as? Int else {
completionHandler(.noData)
return
}
SetTimes.refresh(version: version) { result in
switch result {
case .success(let changed): completionHandler(changed ? .newData : .noData)
case .failure: completionHandler(.failed)
}
}
}
}go deeper
Recall that a silent push uses content-available: 1, needs the Remote notification background mode, and lands in the app delegate's fetch-completion method.
Explain the 30-second budget, the three fetch results, and why custom keys sit beside aps rather than inside it.
Work through the layers in order, capability, payload, handler, system, and name throttling, held-newest-only delivery and force-quit as the usual production surprises.
Treat silent push as a hint, never a sync guarantee, and set a team rule for how often the server may send them.
## What a silent push is A **background notification**, often called a silent push, is a remote notification that shows no alert, plays no sound and sets no badge. Its only job is to wake the app so it can fetch fresh data, here the festival's updated set times. On iOS the system delivers it by launching or waking the app in the background and calling the app delegate's `application(_:didReceiveRemoteNotification:fetchCompletionHandler:)`. Because it never interrupts the person, the system treats it as **low priority** and promises nothing about delivery. ## Where it breaks When the method never runs, the cause sits in one of four layers: | Layer | Requirement | Symptom when missing | |---|---|---| | App capability | Background Modes with **Remote notification** enabled | The app is never launched or woken for the push | | Payload | `aps` contains `content-available: 1` and nothing user-facing | The push is handled as a user-facing notification, not a background one | | Handler | The completion handler is called on every path within about 30 seconds | The app is terminated, and later wakes may come less often | | System | Low priority, throttling, force-quit rules | Pushes arrive late, merged, or not at all | ## The payload Apple's documentation says a background notification's `aps` dictionary includes only the `content-available` key with the value `1`, and must not contain keys that trigger user interaction (`alert`, `sound`, `badge`). App data goes in custom keys that sit beside `aps`, never inside it, because APNs ignores custom keys inside `aps`: ```json { "aps": { "content-available": 1 }, "setTimesVersion": 42 } ``` On the sending side, the POST request to APNs should carry the `apns-push-type` header set to `background` and `apns-priority` set to `5`; the details of that request belong to the server and its provider API. ## The handler and its 30 seconds The handler's contract is short and unforgiving: 1. The system gives the app **up to 30 seconds** of wall-clock time. 2. The app must call `completionHandler` with `.newData`, `.noData` or `.failed`, as soon as it is done. Apple's documentation says that if it does not, the app is terminated. 3. The system tracks elapsed time, power and data use; apps that use a lot of power processing remote notifications may not always be woken early for later ones. So every branch, including the guard that finds no version key and the error path, calls the handler exactly once. Work that needs longer than 30 seconds, such as a large media download, belongs to a background transfer or a scheduled task, which are separate subjects. ## What the system does with them Even a perfect setup is best-effort. The documentation lists the behaviours to design around: - Background notifications are low priority and their delivery is **not guaranteed**. - The system may **throttle** them when there are too many; Apple suggests not sending more than two or three per hour. - The system may **hold** a notification and deliver it later. If a new one arrives, it discards the older held one and keeps only the newest. - If the app is **force-quit or killed**, the held notification is discarded, and the system does not launch a force-quit app for background pushes until the person relaunches it or restarts the device. - If the person launches the app, a held notification is delivered right away. That is why the push should carry a version or a hint, not the data itself, and why the app also refreshes set times whenever it comes to the foreground. ## Testing it honestly - Test on a device, not only with a simulated push: `xcrun simctl push <device> <bundle id> payload.json` sends a payload to the Simulator, which helps with the payload's shape, but throttling and power decisions only show on real hardware under real conditions. - Do not swipe the app away between tests; a force-quit tester sees exactly the reported bug. - Log entry and exit of the handler with the result passed, so a missing completion call is visible. - Send a handful an hour, not one per minute, or the throttle will look like a bug. Why a data-only push cannot be the app's only source of truth, and how priorities should be set across a whole product, are covered with push delivery design.
- What happens if the server adds a sound key to the silent push so testers can hear it arrive?It stops being a background notification. Apple's rule is that a background notification's `aps` contains `content-available` and no keys that interact with the person, such as `alert`, `sound` or `badge`. Testers should watch logs from the handler instead of turning the push into a user-facing one.
- In what form does the set-times version reach the handler, and what can it hold?As a key in the `userInfo` dictionary, which mirrors the JSON payload: the `aps` dictionary plus the custom keys beside it. UIKit converts the JSON into property-list objects plus NSNull, so the handler casts values such as `as? Int` and treats a missing or wrongly typed key as no data. The whole payload must fit in 4 KB, so it carries a version or hint, not the set times.
- What should the handler do if the refresh might take longer than 30 seconds?Fetch only what the alert-free update needs, such as the changed set times, and call the completion handler with the result as soon as that is done. Larger work goes to a background transfer or a scheduled background task, and if the deadline nears, report `.failed` or `.noData` rather than letting the system end the app.
- Any push with content-available wakes the app even without the background mode.
- Adding an alert or sound to a silent push makes it more reliable.
- Silent pushes are guaranteed as long as the payload is correct.
- The system still wakes an app the person force-quit.
- Forgetting the completion handler only costs a warning in the console.