Registering for Push Notifications
Request permission and obtain a device token.
Registering for Push Notifications is a free Swift Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
How Push Works End to End
A push notification travels from your server, through Apple Push Notification service (APNs), to a specific device. The device proves its identity with a device token APNs issued. Before any of this, the user must grant permission and the app must register.
// Flow: App registers -> APNs returns device token ->
// you send token to your server -> server pushes via APNsThe Notification Center
UNUserNotificationCenter is the single object you use for everything local and remote: requesting authorization, setting the delegate, scheduling local notifications, and reading settings. Get the shared instance.
import UserNotifications
let center = UNUserNotificationCenter.current()
// All notification configuration goes through centerRequesting Authorization
You must ask the user before showing notifications. requestAuthorization(options:) presents the system prompt and returns whether the user agreed. Request only the options you need: .alert, .sound, .badge.
import UserNotifications
func requestAuth() async throws -> Bool {
let center = UNUserNotificationCenter.current()
return try await center.requestAuthorization(
options: [.alert, .sound, .badge])
}Registering With APNs
Authorization is separate from registration. After (or alongside) getting permission, call registerForRemoteNotifications() on the main actor. This asks APNs for a device token. It must run on the main thread.
import UIKit
@MainActor
func registerForPush() {
UIApplication.shared.registerForRemoteNotifications()
}Receiving the Device Token
On success, the app delegate's didRegisterForRemoteNotificationsWithDeviceToken fires with a Data token. You convert it to a hex string and send it to your server, which uses it to target this device.
import UIKit
func application(
_ app: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken
deviceToken: Data) {
let token = deviceToken.map {
String(format: "%02x", $0) }.joined()
print("APNs token:", token)
// send token to your backend
}Handling Registration Failure
Registration can fail — no network, no APNs entitlement, or running in an unsupported environment. Implement didFailToRegisterForRemoteNotificationsWithError to log and recover gracefully rather than crash.
import UIKit
func application(
_ app: UIApplication,
didFailToRegisterForRemoteNotificationsWithError
error: Error) {
print("Push registration failed:", error)
}Tokens Can Change
A device token is not permanent. It changes when the user restores from backup, reinstalls, or moves to a new device. Always register on every launch and upload the latest token, replacing the old one server-side.
// Best practice: register on each launch,
// always upload the freshly received token,
// never cache a token as permanent.
let rule = "re-register every launch"
_ = ruleChecking Current Settings
The user may revoke permission later in Settings. Before relying on notifications, query getNotificationSettings to read the current authorization status and which features are allowed.
import UserNotifications
func currentStatus() async -> UNAuthorizationStatus {
let settings = await UNUserNotificationCenter
.current().notificationSettings()
return settings.authorizationStatus
}Provisional Authorization
The .provisional option lets notifications arrive quietly to the Notification Center without an upfront prompt. This is great for letting users try notifications before committing, with an option to keep or turn them off.
import UserNotifications
func requestQuiet() async throws -> Bool {
try await UNUserNotificationCenter.current()
.requestAuthorization(
options: [.alert, .sound, .provisional])
}The Required Capability
None of this works without enabling the Push Notifications capability in your Xcode target, which adds the aps-environment entitlement. For background delivery you also enable the Background Modes capability with Remote notifications.
// Xcode target > Signing & Capabilities:
// + Push Notifications
// + Background Modes > Remote notifications
let caps = "entitlements required"
_ = capsPutting Registration Together
A clean startup flow: request authorization, and if granted, register for remote notifications on the main actor. The token then arrives in the delegate callback for upload.
import UIKit
import UserNotifications
func setupPush() async {
let center = UNUserNotificationCenter.current()
let granted = (try? await center.requestAuthorization(
options: [.alert, .sound, .badge])) ?? false
if granted {
await MainActor.run {
UIApplication.shared.registerForRemoteNotifications()
}
}
}Quick Check
Recall the relationship between authorization and registration.
Recap
You learned how to register for push:
- Use
UNUserNotificationCenterto request.alert/.sound/.badgeauthorization. - Authorization and registration are separate; call
registerForRemoteNotifications()on the main actor. - Convert the
Datatoken to hex and upload it; re-register every launch because tokens change. - Enable the Push Notifications capability and consider
.provisionalauthorization.
Frequently asked questions
Is the “Registering for Push Notifications” lesson free?
Yes — the full text of “Registering for Push Notifications” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
What will I learn in “Registering for Push Notifications”?
Request permission and obtain a device token. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Registering for Push Notifications” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Registering for Push Notifications
- Handling Notification Payloads
- Background Tasks and Refresh
- Notification Actions and Categories