Push notifications let apps send messages to users even when the app is closed. They help keep users informed and engaged.
Push notifications (APNs + FCM) in iOS Swift
import UserNotifications // Request permission UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in if granted { DispatchQueue.main.async { UIApplication.shared.registerForRemoteNotifications() } } } // AppDelegate methods func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { // Send deviceToken to your server or Firebase } func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { print("Failed to register: \(error)") }
APNs (Apple Push Notification service) is Apple's system to send notifications to iOS devices.
FCM (Firebase Cloud Messaging) is a service by Google that helps send notifications to multiple platforms including iOS.
// Request permission for notifications UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { granted, error in print("Permission granted: \(granted)") }
// Register for remote notifications
UIApplication.shared.registerForRemoteNotifications()func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let tokenString = deviceToken.map { String(format: "%02hhx", $0) }.joined()
print("Device Token: \(tokenString)")
}This example shows how to ask permission, register for push notifications, and print the device token. It also handles showing notifications when the app is open.
import UIKit import UserNotifications @main class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { UNUserNotificationCenter.current().delegate = self UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in if granted { DispatchQueue.main.async { UIApplication.shared.registerForRemoteNotifications() } } else { print("Notification permission denied") } } return true } func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { let tokenParts = deviceToken.map { String(format: "%02hhx", $0) } let token = tokenParts.joined() print("Device Token: \(token)") } func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { print("Failed to register: \(error.localizedDescription)") } // Handle notification when app is in foreground func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { completionHandler([.banner, .sound]) } }
Always test push notifications on a real device; the simulator does not support them.
Make sure your app's bundle ID matches the one in your Apple Developer account for push notifications.
FCM requires setting up Firebase in your project and uploading APNs certificates or keys.
Push notifications keep users updated even when the app is closed.
Use APNs to get device tokens and send notifications on iOS.
FCM helps manage and send notifications across platforms including iOS.
Practice
Solution
Step 1: Understand APNs role
APNs stands for Apple Push Notification service and is designed to send notifications to iOS devices.Step 2: Differentiate from other services
It does not handle authentication, data storage, or UI creation, only notification delivery.Final Answer:
To deliver push notifications to iOS devices -> Option CQuick Check:
APNs = notification delivery [OK]
- Confusing APNs with authentication services
- Thinking APNs manages app data
- Assuming APNs creates UI elements
Solution
Step 1: Identify correct class and method
The correct class is UNUserNotificationCenter and the method is requestAuthorization with options and completion handler.Step 2: Check syntax correctness
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { granted, error in } uses the correct syntax with options array and closure parameters.Final Answer:
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { granted, error in } -> Option AQuick Check:
Correct class and method = UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { granted, error in } [OK]
- Using wrong class names like UNNotificationCenter
- Omitting the completion handler closure
- Incorrect method names like requestPermission
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
let token = tokenParts.joined()
print("Device Token: \(token)")
}Solution
Step 1: Understand deviceToken processing
The deviceToken is converted byte-by-byte to a hexadecimal string representation.Step 2: Analyze print output
The print statement outputs "Device Token: " followed by the joined hex string, not the raw data or nil.Final Answer:
Device Token: <hexadecimal string> -> Option BQuick Check:
Device token converted to hex string = Device Token: <hexadecimal string> [OK]
- Expecting raw Data object printed
- Assuming deviceToken is nil
- Printing deviceToken without conversion
func registerForPushNotifications() {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
if granted {
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
}
}Solution
Step 1: Verify permission request syntax
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in ... } uses correct class, method, options, and completion handler.Step 2: Confirm registration call
DispatchQueue.main.async ensures UIApplication.shared.registerForRemoteNotifications() runs on the main thread, which is correct practice.Final Answer:
No error, code is correct -> Option DQuick Check:
Standard registration flow followed correctly [OK]
- Thinking UNUserNotificationCenter delegate is required inside this registration function
- Claiming incorrect options array syntax
- Believing registerForRemoteNotifications should avoid DispatchQueue.main.async
Solution
Step 1: Understand FCM and APNs integration
FCM requires APNs credentials to send notifications to iOS devices through Apple's service.Step 2: Identify required configuration
You must upload the APNs authentication key or certificates to Firebase Console to enable this link.Final Answer:
Upload the APNs authentication key or certificates to the Firebase Console -> Option AQuick Check:
APNs key upload to Firebase = Upload the APNs authentication key or certificates to the Firebase Console [OK]
- Skipping APNs key upload to Firebase
- Relying only on Background Modes without APNs
- Ignoring FCM device token registration
Ready to prove it?
Pass the test and this pattern moves unknown
