0
0
iOS Swiftmobile~20 mins

Local notifications in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Local Notifications Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What happens when this local notification code runs?
Consider this Swift code snippet scheduling a local notification. What will the user see after 5 seconds?
iOS Swift
import UserNotifications

let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = "Reminder"
content.body = "Time to check the app!"
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: "reminder1", content: content, trigger: trigger)
center.add(request) { _ in }
AThe notification appears immediately without waiting 5 seconds.
BThe app crashes because the notification content is missing a sound.
CNothing happens because the notification trigger time is too short.
DA notification with title "Reminder" and body "Time to check the app!" appears after 5 seconds.
Attempts:
2 left
💡 Hint
Think about what UNTimeIntervalNotificationTrigger does with the timeInterval parameter.
lifecycle
intermediate
2:00remaining
When should you request permission for local notifications?
In an iOS app, when is the best time to ask the user for permission to send local notifications?
ARight when the app launches for the first time.
BOnly when the user performs an action that requires notifications.
CAfter the user has seen the app’s main screen for 10 seconds.
DNever, because local notifications do not require permission.
Attempts:
2 left
💡 Hint
Consider user experience and when the user expects notifications.
🔧 Debug
advanced
2:00remaining
Why does this local notification never appear?
This Swift code schedules a notification but it never shows. What is the cause?
iOS Swift
let content = UNMutableNotificationContent()
content.title = "Alert"
content.body = "Check this out!"
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.1, repeats: false)
let request = UNNotificationRequest(identifier: "alert1", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { _ in }
AThe timeInterval cannot be zero; it must be greater than zero.
BThe notification identifier is invalid and must be unique.
CThe notification content is missing a badge number.
DThe notification center is not authorized to send notifications.
Attempts:
2 left
💡 Hint
Check the trigger’s timeInterval value requirements.
🧠 Conceptual
advanced
2:00remaining
What is the role of UNNotificationRequest in local notifications?
Which best describes the purpose of UNNotificationRequest in iOS local notifications?
AIt combines the notification content and trigger into a single request to schedule.
BIt handles user permission requests for notifications.
CIt defines the sound and badge number for the notification only.
DIt manages the app’s notification settings in the system preferences.
Attempts:
2 left
💡 Hint
Think about what you need to schedule a notification.
navigation
expert
2:00remaining
How to handle user tapping a local notification to navigate?
When a user taps a local notification, how can your app navigate to a specific screen?
ASet the notification’s content category to the target screen’s name to auto-navigate.
BUse UIApplicationDelegate’s applicationDidBecomeActive(_:) to detect notification taps and navigate.
CImplement UNUserNotificationCenterDelegate’s userNotificationCenter(_:didReceive:withCompletionHandler:) to handle the tap and navigate.
DLocal notifications cannot trigger navigation; the user must open the app manually.
Attempts:
2 left
💡 Hint
Look for delegate methods related to notification responses.