How to add push notifications to an iOS app
What push notifications do for an iOS app
Push notifications re-engage users with messages your app did not have to be open to receive: a reminder, a new message, a price drop. Remote pushes originate from your server and travel through APNs to the device. The app's part is to ask permission, get a device token, and react when a notification arrives. There is also a local notification path that schedules alerts on the device with no server, which is enough for reminders.
Adding it with AppFlight, and manually
In AppFlight you describe the notifications you want, and the AI adds the capability, the permission request, and token registration. For sending remote pushes you connect a backend. If you use a connector like OneSignal, AppFlight stores the OneSignal app ID as a client value and keeps any REST API key or APNs auth key server-side, so no secret ships in the binary.
Manually, you enable the Push Notifications capability, call UNUserNotificationCenter to request authorization, call registerForRemoteNotifications, implement the app delegate callback that returns the device token, and send that token to a server that holds your APNs auth key.
A SwiftUI example
Request permission and register for remote notifications at launch:
import SwiftUI
import UserNotifications
@main
struct MyApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
var body: some Scene {
WindowGroup { ContentView() }
}
}
final class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ app: UIApplication,
didFinishLaunchingWithOptions opts: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Task {
let granted = try? await UNUserNotificationCenter.current()
.requestAuthorization(options: [.alert, .badge, .sound])
if granted == true {
await MainActor.run { app.registerForRemoteNotifications() }
}
}
return true
}
func application(_ app: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken token: Data) {
let hex = token.map { String(format: "%02x", $0) }.joined()
// Send hex token to your server so it can target this device.
}
}
Common pitfalls
Asking for permission on first launch with no context lowers opt-in rates, so explain the value first. Forgetting that device tokens can change means stale tokens pile up server-side. Testing pushes only on the simulator does not work for remote notifications, since APNs needs a real device or recent simulator support. Putting the APNs auth key in the app is a security mistake, since it can send pushes to every user. If you would rather not run APNs yourself, OneSignal and Firebase Cloud Messaging both provide the sending backend.
FAQ
Do I need a server to send push notifications?
Yes, for remote pushes. Apple Push Notification service requires a server with an APNs auth key or certificate to send. Local notifications, which fire on a schedule from the device, need no server. Services like OneSignal provide that server for you.
What is a device token?
A device token is a unique identifier APNs gives your app for a specific install. You collect it after the user grants permission and send it to your backend so it can target that device. Tokens can change, so update them when iOS provides a new one.
Where do the push credentials live?
The APNs auth key is a secret. It belongs on your server, never in the app binary. With AppFlight, backend secrets stay server-side in your own Supabase, and only client-safe keys ship in the app.