跪拜 Guibai
← All articles
iOS · App

iOS Push: Why Your DeviceToken Isn't the Same as Notification Permission

By kango ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

The cost of conflating authorization and APNs registration is a production outage where users see push enabled but nothing arrives. For any iOS app with an account system, the deviceToken-to-uid binding is the piece that can leak messages between accounts if the backend doesn't enforce uniqueness.

Summary

iOS remote push splits into two independent systems that many tutorials merge into one. `requestAuthorization` on UNUserNotificationCenter controls whether alerts, sounds, and badges appear; it never talks to APNs and never yields a deviceToken. `registerForRemoteNotifications` on UIApplication is the only call that reaches APNs, and it works even when the user denied notification permission. The result is that a denied user still produces a valid token, and push can still be delivered silently, but a normal notification is dropped outright when permission is off.

The article maps the four combinations of authorization and registration, then walks the lifecycle of the token: it changes on reinstall, device restore, and APNs rotation, so every `didRegister` callback must upload the current value. It also details the two payload paths: normal remote notifications require authorization and fire the UN delegate callbacks, while silent push (`content-available:1`) bypasses authorization but is rate-limited and often can't wake a force-quit app. The binding rules for a multi-account IM app follow one line: one deviceToken belongs to exactly one uid at a time. The backend must enforce that with a unique index and a new binding that overrides the old uid, because client-side unbind calls can fail. The article ends with a checklist for testing push before release.

Takeaways
— `requestAuthorization` controls display only; it never talks to APNs and never returns a deviceToken.
— `registerForRemoteNotifications` is the only call that triggers the `didRegister` callback and yields a token, regardless of whether the user authorized notifications.
— The system authorization prompt appears only once per app install lifecycle; subsequent calls just return the saved state.
— The APNs callbacks are asynchronous and off the main thread; UI updates require dispatching to the main queue.
— Apple recommends calling `registerForRemoteNotifications` at every launch; the system caches the token so the callback is fast.
— deviceToken changes on reinstall, device restore, or APNs rotation; every `didRegister` callback must upload the current token.
— Normal remote notifications are dropped at the system level when the user denied authorization; none of the app's callbacks fire.
— Silent push (`content-available:1`) does not require authorization but is rate-limited and often fails to wake a force-quit app.
— Backend binding must enforce a unique deviceToken per uid; a new binding must automatically override the old uid to prevent cross-account messages.
— Use server-side unbind and not `unregisterForRemoteNotifications`, which kills the APNs registration entirely.
Conclusions

The most common iOS push tutorial error, merging `requestAuthorization` and `registerForRemoteNotifications` into one step, is the root cause of most subsequent confusion; this article's separation makes every callback condition a logical consequence rather than a memorized rule.

The article's pitfall list reveals that the biggest production risk isn't Apple's behavior but developers deleting `registerForRemoteNotifications` from startup logic while leaving permissions on; the failure mode is silent and hard to detect.

Because client-side unbind calls are unreliable, the backend must own the token-to-uid mapping with a unique constraint and automatic override; otherwise a device switching accounts can receive the former account's messages.

Concepts & terms
deviceToken
A unique identifier issued by Apple's APNs for a specific device-app-environment combination. It changes under certain conditions and is required to send remote notifications to that device.
APNs
Apple Push Notification service: the system that delivers remote notifications from a server to iOS devices; registration with APNs yields the deviceToken.
Silent push
A remote notification with `content-available:1` in the payload that wakes the app in the background without any visible user interface. It's subject to system throttling and is not guaranteed after the user force-quits the app.
Provisional authorization
An iOS 12+ permission option that delivers notifications quietly to Notification Center without showing the system prompt; lets the user decide later whether to keep or deliver immediately. Useful to avoid burning the one-time authorization dialog.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗