Bird
Raised Fist0
iOS Swiftmobile~20 mins

Push notifications (APNs + FCM) in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Push Notification Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate
2:00remaining
Handling Notification Permissions Prompt

What happens when the following Swift code runs in an iOS app regarding push notification permissions?

iOS Swift
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
  if granted {
    DispatchQueue.main.async {
      UIApplication.shared.registerForRemoteNotifications()
    }
  }
}
AThe app asks the user for permission to show alerts, play sounds, and update the badge, then registers for remote notifications if granted.
BThe app immediately registers for remote notifications without asking the user for permission.
CThe app only registers for local notifications and never asks for remote notification permission.
DThe app requests permission but does not register for remote notifications even if permission is granted.
Attempts:
2 left
💡 Hint

Think about what requestAuthorization and registerForRemoteNotifications do.

🧠 Conceptual
intermediate
2:00remaining
Difference Between APNs and FCM

Which statement correctly describes the relationship between APNs and Firebase Cloud Messaging (FCM) in iOS push notifications?

AFCM directly delivers notifications to iOS devices without using APNs.
BAPNs is a Google service that replaces FCM for iOS devices.
CFCM is a service that sends notifications to APNs, which then delivers them to iOS devices.
DAPNs and FCM are two independent services that do not interact.
Attempts:
2 left
💡 Hint

Consider which service is Apple's and which is Google's, and how they work together.

❓ lifecycle
advanced
2:00remaining
App Behavior on Receiving Push Notification in Foreground

Given this delegate method in an iOS app, what will the user see when a push notification arrives while the app is open?

iOS Swift
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
  completionHandler([.banner, .sound])
}
AThe notification is silently received without any UI or sound.
BThe notification banner and sound appear even though the app is in the foreground.
COnly the sound plays but no banner is shown.
DNo notification is shown because the app is active.
Attempts:
2 left
💡 Hint

Think about what completionHandler controls in this method.

🔧 Debug
advanced
2:00remaining
Why is the Device Token Not Received?

Consider this code snippet in an iOS app delegate. The app never receives a device token. What is the most likely cause?

iOS Swift
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  print("Device token received")
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
  print("Failed to register: \(error.localizedDescription)")
}

// Missing call to registerForRemoteNotifications() in app launch
AThe app never called registerForRemoteNotifications(), so it never asked APNs for a device token.
BThe device token is received but the print statement is incorrect.
CThe app delegate methods are deprecated and no longer called.
DThe app has no internet connection, so the token cannot be received.
Attempts:
2 left
💡 Hint

Check if the app requested remote notifications registration.

❓ navigation
expert
3:00remaining
Navigating to a Specific Screen from a Push Notification

Which code snippet correctly navigates to a "MessagesViewController" when the user taps a push notification while the app is in background?

iOS Swift
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
  let userInfo = response.notification.request.content.userInfo
  if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
     let rootVC = windowScene.windows.first?.rootViewController {
    // Navigation code here
  }
  completionHandler()
}
A
let messagesVC = MessagesViewController()
rootVC.navigationController?.pushViewController(messagesVC, animated: true)
B
let messagesVC = MessagesViewController()
rootVC.present(messagesVC, animated: true)
C
let messagesVC = MessagesViewController()
UIApplication.shared.keyWindow?.rootViewController?.present(messagesVC, animated: true)
D
let messagesVC = MessagesViewController()
if let nav = rootVC as? UINavigationController {
  nav.pushViewController(messagesVC, animated: true)
}
Attempts:
2 left
💡 Hint

Consider how to access the navigation controller from the root view controller.

Practice

(1/5)
1. What is the primary purpose of APNs in iOS push notifications?
easy
A. To store app data locally
B. To manage user authentication
C. To deliver push notifications to iOS devices
D. To create user interface elements

Solution

  1. Step 1: Understand APNs role

    APNs stands for Apple Push Notification service and is designed to send notifications to iOS devices.
  2. Step 2: Differentiate from other services

    It does not handle authentication, data storage, or UI creation, only notification delivery.
  3. Final Answer:

    To deliver push notifications to iOS devices -> Option C
  4. Quick Check:

    APNs = notification delivery [OK]
Hint: APNs is Apple's service for sending notifications [OK]
Common Mistakes:
  • Confusing APNs with authentication services
  • Thinking APNs manages app data
  • Assuming APNs creates UI elements
2. Which Swift code snippet correctly requests permission for push notifications?
easy
A. UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { granted, error in }
B. UNUserNotificationCenter.requestAuthorization(options: [.alert])
C. UNNotificationCenter.current().requestPermission()
D. UserNotificationCenter.requestAuthorization(options: [.badge])

Solution

  1. Step 1: Identify correct class and method

    The correct class is UNUserNotificationCenter and the method is requestAuthorization with options and completion handler.
  2. Step 2: Check syntax correctness

    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { granted, error in } uses the correct syntax with options array and closure parameters.
  3. Final Answer:

    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { granted, error in } -> Option A
  4. Quick Check:

    Correct class and method = UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { granted, error in } [OK]
Hint: Use UNUserNotificationCenter.current().requestAuthorization with options [OK]
Common Mistakes:
  • Using wrong class names like UNNotificationCenter
  • Omitting the completion handler closure
  • Incorrect method names like requestPermission
3. Given this Swift code snippet, what will be printed if the device token is successfully received?
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)")
}
medium
A. Device Token: nil
B. Device Token:
C. Device Token: deviceToken
D. Device Token: [data]

Solution

  1. Step 1: Understand deviceToken processing

    The deviceToken is converted byte-by-byte to a hexadecimal string representation.
  2. Step 2: Analyze print output

    The print statement outputs "Device Token: " followed by the joined hex string, not the raw data or nil.
  3. Final Answer:

    Device Token: <hexadecimal string> -> Option B
  4. Quick Check:

    Device token converted to hex string = Device Token: <hexadecimal string> [OK]
Hint: Device token is hex string printed, not raw data [OK]
Common Mistakes:
  • Expecting raw Data object printed
  • Assuming deviceToken is nil
  • Printing deviceToken without conversion
4. Identify the error in this Swift code snippet for registering remote notifications:
func registerForPushNotifications() {
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
        if granted {
            DispatchQueue.main.async {
                UIApplication.shared.registerForRemoteNotifications()
            }
        }
    }
}
medium
A. Incorrect options array syntax
B. Missing call to UNUserNotificationCenter.current().delegate assignment
C. registerForRemoteNotifications() should be called outside DispatchQueue.main.async
D. No error, code is correct

Solution

  1. Step 1: Verify permission request syntax

    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in ... } uses correct class, method, options, and completion handler.
  2. Step 2: Confirm registration call

    DispatchQueue.main.async ensures UIApplication.shared.registerForRemoteNotifications() runs on the main thread, which is correct practice.
  3. Final Answer:

    No error, code is correct -> Option D
  4. Quick Check:

    Standard registration flow followed correctly [OK]
Hint: This code correctly requests permission and registers for remote notifications [OK]
Common Mistakes:
  • Thinking UNUserNotificationCenter delegate is required inside this registration function
  • Claiming incorrect options array syntax
  • Believing registerForRemoteNotifications should avoid DispatchQueue.main.async
5. You want to send a push notification to an iOS app using Firebase Cloud Messaging (FCM). Which step is essential to link APNs with FCM for this to work correctly?
hard
A. Upload the APNs authentication key or certificates to the Firebase Console
B. Enable Background Modes in Xcode without any APNs setup
C. Register the device token only with APNs, ignoring FCM
D. Use only APNs without configuring Firebase

Solution

  1. Step 1: Understand FCM and APNs integration

    FCM requires APNs credentials to send notifications to iOS devices through Apple's service.
  2. Step 2: Identify required configuration

    You must upload the APNs authentication key or certificates to Firebase Console to enable this link.
  3. Final Answer:

    Upload the APNs authentication key or certificates to the Firebase Console -> Option A
  4. Quick Check:

    APNs key upload to Firebase = Upload the APNs authentication key or certificates to the Firebase Console [OK]
Hint: Upload APNs key to Firebase to enable iOS push via FCM [OK]
Common Mistakes:
  • Skipping APNs key upload to Firebase
  • Relying only on Background Modes without APNs
  • Ignoring FCM device token registration
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard