0
0
iOS Swiftmobile~20 mins

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

Choose your learning style9 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.