What happens when the following Swift code runs in an iOS app regarding push notification permissions?
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
if granted {
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
}Think about what requestAuthorization and registerForRemoteNotifications do.
The code first asks the user for permission to show notifications with alerts, sounds, and badges. If the user grants permission, the app then registers with APNs to receive remote notifications.
Which statement correctly describes the relationship between APNs and Firebase Cloud Messaging (FCM) in iOS push notifications?
Consider which service is Apple's and which is Google's, and how they work together.
Firebase Cloud Messaging (FCM) acts as a middleman that sends notifications to Apple Push Notification service (APNs), which then delivers them to iOS devices.
Given this delegate method in an iOS app, what will the user see when a push notification arrives while the app is open?
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.banner, .sound])
}Think about what completionHandler controls in this method.
This delegate method lets the app decide how to present a notification when the app is active. Passing [.banner, .sound] shows a banner and plays a sound.
Consider this code snippet in an iOS app delegate. The app never receives a device token. What is the most likely cause?
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 launchCheck if the app requested remote notifications registration.
Without calling registerForRemoteNotifications(), the app does not ask APNs for a device token, so the delegate method is never called.
Which code snippet correctly navigates to a "MessagesViewController" when the user taps a push notification while the app is in background?
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()
}Consider how to access the navigation controller from the root view controller.
To navigate properly, you must cast the root view controller to UINavigationController and then push the new view controller onto its stack.