Bird
Raised Fist0
iOS Swiftmobile~20 mins

Local notifications in iOS Swift - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. What is the first step you must do before scheduling local notifications in an iOS app?
easy
A. Request permission from the user to send notifications
B. Create the notification content
C. Set up the notification trigger
D. Register the notification category

Solution

  1. Step 1: Understand user permission requirement

    iOS requires apps to ask users for permission before sending notifications to respect privacy.
  2. Step 2: Request permission before scheduling

    You must call the requestAuthorization method to get user consent before scheduling any notifications.
  3. Final Answer:

    Request permission from the user to send notifications -> Option A
  4. Quick Check:

    Permission request = Request permission from the user to send notifications [OK]
Hint: Always ask user permission first before notifications [OK]
Common Mistakes:
  • Scheduling notifications without permission
  • Assuming notifications work without user consent
  • Skipping the requestAuthorization call
2. Which of the following is the correct way to create a notification content in Swift?
easy
A. let content = UNNotificationContent()
B. let content = NotificationContent()
C. let content = UNMutableNotificationContent()
D. let content = UNNotificationRequest()

Solution

  1. Step 1: Identify the correct class for notification content

    UNMutableNotificationContent is the class used to create and customize notification content.
  2. Step 2: Differentiate from other classes

    UNNotificationContent is abstract and cannot be instantiated; UNNotificationRequest is for scheduling, not content.
  3. Final Answer:

    let content = UNMutableNotificationContent() -> Option C
  4. Quick Check:

    Use UNMutableNotificationContent() to create content [OK]
Hint: Use UNMutableNotificationContent() to create notification content [OK]
Common Mistakes:
  • Trying to instantiate UNNotificationContent directly
  • Confusing UNNotificationRequest with content
  • Using wrong class names
3. What will happen when this Swift code runs?
let content = UNMutableNotificationContent()
content.title = "Reminder"
content.body = "Time to drink water"
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: "waterReminder", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
medium
A. A notification appears after 5 seconds with the given title and body
B. A notification titled 'Reminder' appears immediately
C. The notification repeats every 5 seconds
D. No notification appears because repeats is false

Solution

  1. Step 1: Analyze the trigger setup

    The trigger is set with timeInterval 5 seconds and repeats false, so it fires once after 5 seconds.
  2. Step 2: Understand notification scheduling

    The request is added to the notification center, so the notification will appear after the delay with the specified title and body.
  3. Final Answer:

    A notification appears after 5 seconds with the given title and body -> Option A
  4. Quick Check:

    TimeInterval trigger fires once after delay = A notification appears after 5 seconds with the given title and body [OK]
Hint: TimeInterval triggers fire after delay, repeats false means one-time [OK]
Common Mistakes:
  • Thinking repeats false means no notification
  • Assuming notification appears immediately
  • Confusing repeats true with repeats false behavior
4. Identify the error in this Swift code for scheduling a local notification:
let content = UNMutableNotificationContent()
content.title = "Alert"
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: -10, repeats: false)
let request = UNNotificationRequest(identifier: "alert1", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
medium
A. Missing requestAuthorization call before scheduling
B. Negative timeInterval causes an error; timeInterval must be positive
C. content.title should be content.body
D. UNNotificationRequest identifier must be unique

Solution

  1. Step 1: Check the trigger timeInterval value

    The timeInterval is set to -10, which is invalid because it must be positive.
  2. Step 2: Understand trigger constraints

    UNTimeIntervalNotificationTrigger requires a positive timeInterval to schedule the notification correctly.
  3. Final Answer:

    Negative timeInterval causes an error; timeInterval must be positive -> Option B
  4. Quick Check:

    timeInterval > 0 required = Negative timeInterval causes an error; timeInterval must be positive [OK]
Hint: timeInterval must be positive for triggers [OK]
Common Mistakes:
  • Using negative or zero timeInterval
  • Ignoring permission request (though important, not error here)
  • Confusing title and body properties
5. You want to schedule a daily local notification at 8:00 AM that repeats every day. Which trigger setup is correct in Swift?
hard
A. UNTimeIntervalNotificationTrigger(timeInterval: 86400, repeats: true)
B. UNTimeIntervalNotificationTrigger(timeInterval: 3600, repeats: true)
C. UNCalendarNotificationTrigger(dateMatching: DateComponents(hour: 8, minute: 0), repeats: false)
D. UNCalendarNotificationTrigger(dateMatching: DateComponents(hour: 8, minute: 0), repeats: true)

Solution

  1. Step 1: Understand daily repeating notification needs

    To trigger at a specific time daily, use UNCalendarNotificationTrigger with repeats true and matching hour and minute.
  2. Step 2: Compare options

    UNTimeIntervalNotificationTrigger(timeInterval: 86400, repeats: true) repeats every 86400 seconds but may drift; UNCalendarNotificationTrigger(dateMatching: DateComponents(hour: 8, minute: 0), repeats: true) correctly uses calendar components for 8:00 AM daily; UNCalendarNotificationTrigger(dateMatching: DateComponents(hour: 8, minute: 0), repeats: false) does not repeat; UNTimeIntervalNotificationTrigger(timeInterval: 3600, repeats: true) repeats every hour, not daily.
  3. Final Answer:

    UNCalendarNotificationTrigger(dateMatching: DateComponents(hour: 8, minute: 0), repeats: true) -> Option D
  4. Quick Check:

    Use calendar trigger with repeats true for daily time [OK]
Hint: Use calendar trigger with repeats true for daily notifications [OK]
Common Mistakes:
  • Using time interval triggers for exact daily time
  • Setting repeats false for repeating notifications
  • Confusing seconds with hours in timeInterval
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