Push Notifications
Implement, review, and debug local and remote notifications with UserNotifications and APNs. Target the project's deployment range; examples assume modern Swift concurrency and note availability when using newer APIs.
Keep adjacent domains separate: Live Activity content-state pushes belong to activitykit; PushKit/VoIP calls to callkit; App Clip ephemeral setup to app-clips; long-running or scheduled background work to background-processing.
Contents
Route by task
Read only the references needed for the request:
- For notification authorization, APNs registration, token lifecycle, provider headers, visible/background payloads, or delivery diagnosis, read APNs lifecycle and remote delivery.
- For time, calendar, or location reminders scheduled on-device, read local notifications.
- For complete application-delegate wiring, foreground/tap handling, deep-link routing, and categories, read notification runtime and routing.
- For
.apns, simctl, APNs Sandbox Simulator coverage, provider/device matrices, or delivery diagnosis, read notification delivery testing.
- For service-extension mutation and exactly-once completion, read service extensions.
- For custom expanded UI, read content extensions; for Messages-style presentation, read communication notifications.
- For the focused reference index, read notification patterns or rich notifications. Use complete notification patterns and complete rich-notification recipes only for broad end-to-end examples or migration.
Do not read every reference for a narrow task. Keep the response scoped to the failed or requested delivery path.
Core workflow
- Classify the notification as local, visible remote, background remote, Live Activity, VoIP, or extension-modified.
- Inspect entitlements, capabilities, bundle/topic, delegate installation, categories, provider ownership, and target platform.
- Separate visible authorization from APNs registration and server token binding.
- Define payload keys, APNs headers, expiry/collapse behavior, and sensitive-data policy.
- Implement foreground presentation, response/action routing, and bounded background or extension work.
- Verify each boundary independently: scheduling/provider acceptance, device receipt, extension execution, presentation, and response routing.
Authorization and APNs registration
Notification authorization controls user-visible alerts, sounds, and badges. Request it in context and check notificationSettings() because the user can change settings later.
APNs registration is a separate path. Call registerForRemoteNotifications() whenever a device token is needed, including server binding or background delivery. Do not gate it on .authorized.
Receive tokens through application-delegate callbacks. Treat token data as opaque, convert it to hex for transport, and upload on every successful callback. Do not assume a fixed length or treat a locally cached token as provider truth.
func application(
_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
) {
let token = deviceToken.map { String(format: "%02x", $0) }.joined()
Task { await tokenService.upload(token) }
}
On supported current Xcode/OS hosts, iOS Simulator can register with the APNs Sandbox and receives a simulator-specific, variable-length token. Provider delivery to that Simulator is useful for sandbox end-to-end checks. .apns files and simctl push simulate delivery without exercising the provider path; host/CI support varies. Verify production entitlements, signing, and hardware-specific behavior on a physical device.
Payload and delivery contracts
| Path |
Required contract |
Key limitation |
| Visible remote |
aps.alert, apns-push-type: alert |
Presentation depends on authorization, app state, Focus, and delegate policy |
| Background remote |
content-available: 1 only in aps, push type background, priority 5 |
Low priority, throttled, coalesced, and not guaranteed |
| Service extension |
Alert payload plus mutable-content: 1 |
Silent-only, sound-only, or badge-only pushes don't launch it |
| Local |
UNNotificationRequest with time/calendar/location trigger |
Device scheduling and current authorization determine presentation |
Put Apple keys inside aps and minimal app routing identifiers beside it. Treat payload data as untrusted: validate identity/authorization against current app or server state before navigation or mutation. Avoid sensitive plaintext.
Background pushes are hints to fetch current state, not a timer. Enable Background Modes > Remote notifications, perform bounded work, and return the correct UIBackgroundFetchResult. Route BGTaskScheduler design to background-processing.
Runtime handling and routing
Set UNUserNotificationCenter.current().delegate during launch, before responses can arrive.
@MainActor
final class NotificationDelegate: NSObject, UNUserNotificationCenterDelegate {
func userNotificationCenter(
_ center: UNUserNotificationCenter,
willPresent notification: UNNotification
) async -> UNNotificationPresentationOptions {
[.banner, .list, .sound, .badge]
}
func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse
) async {
await notificationRouter.handle(response)
}
}
Keep payload parsing in a testable boundary. Hand a validated destination or intent to the app's existing navigation/state owner; notification delegates should not create a competing navigation architecture.
Foreground receipt does not automatically show UI. Return only the presentation options the product wants. Handle body taps, dismiss callbacks when registered, and custom action identifiers deliberately.
Categories and actions
Register categories during launch. Payload category and local categoryIdentifier values must exactly match the registered identifier.
Choose action options from behavior:
.foreground launches the app for UI work;
.authenticationRequired protects sensitive actions until unlock;
.destructive communicates irreversible intent;
UNTextInputNotificationAction supports inline text response.
Validate identifiers in userInfo again before calling services. Define idempotency for actions the system or user may invoke more than once.
Correction reviews
For flawed designs, name the violated contract rather than only showing replacement code:
- Registration gated by alert permission: separate authorization and token registration.
- Token cached to skip provider upload: upload on every callback and let the provider reconcile.
- Frequent priority-10 silent pushes: use background payload/headers and state that delivery is throttled and not guaranteed.
- Service extension triggered by silent push: require alert content plus
mutable-content: 1.
- Extension secrets in App Group defaults: use Keychain Sharing for secrets; App Groups for shared files/defaults.
- Attachment from arbitrary remote URL: download a supported file to disk, then construct
UNNotificationAttachment.
- Extension missing fallback: call the content handler exactly once on success, failure, and
serviceExtensionTimeWillExpire().
Common Mistakes
- Setting the center delegate after launch or inside a transient SwiftUI view.
- Treating a missing banner as proof that APNs didn't deliver.
- Assuming background delivery is immediate or periodic.
- Converting device token data as UTF-8 or assuming token length.
- Doing unbounded network work in a background callback or extension.
- Putting authorization state, secrets, or trusted navigation decisions in payload data.
- Using
removeAll… for a feature that doesn't own every app notification.
- Mixing Live Activity, VoIP, and ordinary alert payload rules.
Review Checklist
References
1---2name: push-notifications3description: Implement or debug local and APNs notifications, permissions, payloads, categories, actions, silent pushes, and notification extensions. Use for alerts, badges, sounds, background delivery, rich content, registration, or delivery diagnosis; route Live Activity updates to activitykit.4---56# Push Notifications78Implement, review, and debug local and remote notifications with `UserNotifications` and APNs. Target the project's deployment range; examples assume modern Swift concurrency and note availability when using newer APIs.910Keep adjacent domains separate: Live Activity `content-state` pushes belong to `activitykit`; PushKit/VoIP calls to `callkit`; App Clip ephemeral setup to `app-clips`; long-running or scheduled background work to `background-processing`.1112## Contents1314- [Route by task](#route-by-task)15- [Core workflow](#core-workflow)16- [Authorization and APNs registration](#authorization-and-apns-registration)17- [Payload and delivery contracts](#payload-and-delivery-contracts)18- [Runtime handling and routing](#runtime-handling-and-routing)19- [Categories and actions](#categories-and-actions)20- [Correction reviews](#correction-reviews)21- [Common mistakes](#common-mistakes)22- [Review checklist](#review-checklist)2324## Route by task2526Read only the references needed for the request:2728- For notification authorization, APNs registration, token lifecycle, provider headers, visible/background payloads, or delivery diagnosis, read [APNs lifecycle and remote delivery](references/apns-lifecycle.md).29- For time, calendar, or location reminders scheduled on-device, read [local notifications](references/local-notifications.md).30- For complete application-delegate wiring, foreground/tap handling, deep-link routing, and categories, read [notification runtime and routing](references/notification-runtime.md).31- For `.apns`, `simctl`, APNs Sandbox Simulator coverage, provider/device matrices, or delivery diagnosis, read [notification delivery testing](references/notification-testing.md).32- For service-extension mutation and exactly-once completion, read [service extensions](references/service-extension.md).33- For custom expanded UI, read [content extensions](references/content-extension.md); for Messages-style presentation, read [communication notifications](references/communication-notifications.md).34- For the focused reference index, read [notification patterns](references/notification-patterns.md) or [rich notifications](references/rich-notifications.md). Use [complete notification patterns](references/notification-patterns-complete.md) and [complete rich-notification recipes](references/rich-notifications-complete.md) only for broad end-to-end examples or migration.3536Do not read every reference for a narrow task. Keep the response scoped to the failed or requested delivery path.3738## Core workflow39401. Classify the notification as local, visible remote, background remote, Live Activity, VoIP, or extension-modified.412. Inspect entitlements, capabilities, bundle/topic, delegate installation, categories, provider ownership, and target platform.423. Separate visible authorization from APNs registration and server token binding.434. Define payload keys, APNs headers, expiry/collapse behavior, and sensitive-data policy.445. Implement foreground presentation, response/action routing, and bounded background or extension work.456. Verify each boundary independently: scheduling/provider acceptance, device receipt, extension execution, presentation, and response routing.4647## Authorization and APNs registration4849Notification authorization controls user-visible alerts, sounds, and badges. Request it in context and check `notificationSettings()` because the user can change settings later.5051APNs registration is a separate path. Call `registerForRemoteNotifications()` whenever a device token is needed, including server binding or background delivery. Do not gate it on `.authorized`.5253Receive tokens through application-delegate callbacks. Treat token data as opaque, convert it to hex for transport, and upload on every successful callback. Do not assume a fixed length or treat a locally cached token as provider truth.5455```swift56func application(57 _ application: UIApplication,58 didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data59) {60 let token = deviceToken.map { String(format: "%02x", $0) }.joined()61 Task { await tokenService.upload(token) }62}63```6465On supported current Xcode/OS hosts, iOS Simulator can register with the APNs Sandbox and receives a simulator-specific, variable-length token. Provider delivery to that Simulator is useful for sandbox end-to-end checks. `.apns` files and `simctl push` simulate delivery without exercising the provider path; host/CI support varies. Verify production entitlements, signing, and hardware-specific behavior on a physical device.6667## Payload and delivery contracts6869| Path | Required contract | Key limitation |70|---|---|---|71| Visible remote | `aps.alert`, `apns-push-type: alert` | Presentation depends on authorization, app state, Focus, and delegate policy |72| Background remote | `content-available: 1` only in `aps`, push type `background`, priority `5` | Low priority, throttled, coalesced, and not guaranteed |73| Service extension | Alert payload plus `mutable-content: 1` | Silent-only, sound-only, or badge-only pushes don't launch it |74| Local | `UNNotificationRequest` with time/calendar/location trigger | Device scheduling and current authorization determine presentation |7576Put Apple keys inside `aps` and minimal app routing identifiers beside it. Treat payload data as untrusted: validate identity/authorization against current app or server state before navigation or mutation. Avoid sensitive plaintext.7778Background pushes are hints to fetch current state, not a timer. Enable Background Modes > Remote notifications, perform bounded work, and return the correct `UIBackgroundFetchResult`. Route `BGTaskScheduler` design to `background-processing`.7980## Runtime handling and routing8182Set `UNUserNotificationCenter.current().delegate` during launch, before responses can arrive.8384```swift85@MainActor86final class NotificationDelegate: NSObject, UNUserNotificationCenterDelegate {87 func userNotificationCenter(88 _ center: UNUserNotificationCenter,89 willPresent notification: UNNotification90 ) async -> UNNotificationPresentationOptions {91 [.banner, .list, .sound, .badge]92 }9394 func userNotificationCenter(95 _ center: UNUserNotificationCenter,96 didReceive response: UNNotificationResponse97 ) async {98 await notificationRouter.handle(response)99 }100}101```102103Keep payload parsing in a testable boundary. Hand a validated destination or intent to the app's existing navigation/state owner; notification delegates should not create a competing navigation architecture.104105Foreground receipt does not automatically show UI. Return only the presentation options the product wants. Handle body taps, dismiss callbacks when registered, and custom action identifiers deliberately.106107## Categories and actions108109Register categories during launch. Payload `category` and local `categoryIdentifier` values must exactly match the registered identifier.110111Choose action options from behavior:112113- `.foreground` launches the app for UI work;114- `.authenticationRequired` protects sensitive actions until unlock;115- `.destructive` communicates irreversible intent;116- `UNTextInputNotificationAction` supports inline text response.117118Validate identifiers in `userInfo` again before calling services. Define idempotency for actions the system or user may invoke more than once.119120## Correction reviews121122For flawed designs, name the violated contract rather than only showing replacement code:123124- Registration gated by alert permission: separate authorization and token registration.125- Token cached to skip provider upload: upload on every callback and let the provider reconcile.126- Frequent priority-10 silent pushes: use background payload/headers and state that delivery is throttled and not guaranteed.127- Service extension triggered by silent push: require alert content plus `mutable-content: 1`.128- Extension secrets in App Group defaults: use Keychain Sharing for secrets; App Groups for shared files/defaults.129- Attachment from arbitrary remote URL: download a supported file to disk, then construct `UNNotificationAttachment`.130- Extension missing fallback: call the content handler exactly once on success, failure, and `serviceExtensionTimeWillExpire()`.131132## Common Mistakes133134- Setting the center delegate after launch or inside a transient SwiftUI view.135- Treating a missing banner as proof that APNs didn't deliver.136- Assuming background delivery is immediate or periodic.137- Converting device token data as UTF-8 or assuming token length.138- Doing unbounded network work in a background callback or extension.139- Putting authorization state, secrets, or trusted navigation decisions in payload data.140- Using `removeAll…` for a feature that doesn't own every app notification.141- Mixing Live Activity, VoIP, and ordinary alert payload rules.142143## Review Checklist144145- [ ] Notification path and sibling-skill boundary are explicit.146- [ ] Visible authorization is requested in context and current settings are respected.147- [ ] APNs registration is not incorrectly gated by visible authorization.148- [ ] Token is treated as opaque and uploaded on every registration callback.149- [ ] Provider push type, priority, topic, expiration, and payload match the path.150- [ ] Delegate and categories are installed during launch.151- [ ] Foreground presentation, taps, dismissals, and custom actions follow product policy.152- [ ] Background or extension work is bounded and completes through every path.153- [ ] Payload identifiers are validated before navigation or mutation.154- [ ] Local requests have stable ownership, update, and cancellation semantics.155- [ ] Simulator limitations and physical-device verification are distinguished.156- [ ] Delivery evidence covers the actual provider/device/app boundary that changed.157158## References159160- [UserNotifications](https://sosumi.ai/documentation/usernotifications)161- [Registering your app with APNs](https://sosumi.ai/documentation/usernotifications/registering-your-app-with-apns)162- [Generating a remote notification](https://sosumi.ai/documentation/usernotifications/generating-a-remote-notification)163- [UNUserNotificationCenterDelegate](https://sosumi.ai/documentation/usernotifications/unusernotificationcenterdelegate)