Describe the process for handling push notifications in iOS.
Instruction: Explain how an iOS application can register, receive, and handle push notifications.
Updated
Official answer
Separate permission to present alerts/sounds/badges from registration with APNs. Use UNUserNotificationCenter to request presentation authorization in a relevant context and inspect current settings, including denial or provisional authorization. APNs registration obtains the routing token; it is not inherently conditional on alert permission, because background updates have a different presentation contract.
Enable the required push capability and correct signing environment. Register with UIApplication and implement the app delegate's successful-token and registration-error callbacks. Request the current token at launch and securely send it to the provider with the correct app/environment/account association. Do not assume a fixed token length or cache a token permanently. Handle rotation, invalid-token responses, logout and account switching so one person's notifications are not sent under another person's association.
Assign a retained UNUserNotificationCenter delegate early enough for delivery. Its willPresent callback selects permitted foreground presentation options. Its didReceive response callback handles a person's action on a notification; call completion handlers as required. Validate notification payloads and route only after application state and current authorization are ready. A notification's record ID must not bypass the server's access checks.
Background notifications require the appropriate capability and payload, provide limited execution time and may be delayed, coalesced, throttled or dropped. They are a refresh hint, not a reliable scheduler or exactly-once command channel. Make refresh processing idempotent and fetch authoritative state when the app becomes active.
Test foreground, background, cold launch, permission changes, duplicate/stale actions, registration failure and account changes on supported devices. Track provider acceptance, delivery evidence where available, user action and opt-out separately. A before/after DAU change does not establish that notification changes caused better outcomes.
APNs registration and tokens, Presentation authorization, Background delivery limitations
Related Questions
-
easy
-
hard
-
hard