Swift Push Notifications
Distinguish notification permission, device registration and provider delivery. Requires Mac/Xcode; this Apple-only source was not SDK-compiled or run here.
How it works
Remote notifications require an appropriate signed target, Push Notifications capability, APNs environment and a trusted provider. This source requests visible-notification permission only after a button action, then asks the system to register. A registration callback is not proof that an alert was delivered. No provider credentials or notification service are included.
Apple-only setup and review status
Create a separate iOS SwiftUI App learning project in a suitable Xcode version, choose an appropriate simulator/device and replace the generated app/view Swift source with this complete file. Keep one @main entry point. Widget and App Clip lessons explicitly require their own extension/clip targets instead; capability, purpose-string, entitlement and account steps in the lesson must be completed separately. No Xcode project, signing certificate, account or service was provisioned here.
Bhylu has no Swift browser runner. The JavaScript/Python editors do not run these sources; Apache, PHP and MySQL are not required. Downloads are source text, never executable teaching endpoints.
Download the complete push-notifications.swift · Download all source files with hash checks
For the optional collection, save download-examples.mjs in a new folder and run node download-examples.mjs. A local preview uses node download-examples.mjs http://127.0.0.1:8000/examples/swift/. It refuses an existing bhylu-swift-examples folder and installs or executes no Swift automatically. Apple-only files remain separate Xcode learning examples, not a portable Swift package target.
Example
// Apple-only learning file: iOS 17+ target with a suitable Xcode SDK.
// Not compiled or run in this Windows/Linux review.
import SwiftUI
import UIKit
import UserNotifications
final class PushDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("APNs registration callback received; token not logged")
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("APNs registration failed")
}
}
struct DemoView: View {
@State private var notice = "No request made"
var body: some View {
VStack(spacing: 20) {
Button("Request notification permission") {
Task {
do {
let allowed = try await UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound])
notice = allowed ? "Permission granted; no push sent" : "Permission not granted"
if allowed { UIApplication.shared.registerForRemoteNotifications() }
} catch { notice = "Permission request failed" }
}
}
Text(notice)
}.padding()
}
}
@main
struct WorkshopDemoApp: App {
@UIApplicationDelegateAdaptor(PushDelegate.self) private var delegate
var body: some Scene { WindowGroup { DemoView() } }
}
Expected behaviour and manual checks
Manual check on a properly configured Apple target: allow and refuse permission, handle registration failure and inspect environment-specific registration. Use a controlled provider to test delivery separately. No APNs registration, token transfer or push delivery was performed here.
The delegate reports only that a callback occurred and never prints the actual device token. A real provider needs authenticated registration, token rotation handling, account ownership and deletion policies. Foreground presentation and background data delivery have their own delegate and system rules.
Primary references: The Swift Programming Language and Apple framework documentation. Use the selected platform’s current requirements. Fictional workshop examples do not establish production safety or deployment approval.