You must ask the user for permission before sending notifications.
Triggers decide when the notification appears (time, location, calendar).
Examples
Set the notification title and message.
iOS Swift
content.title = "Reminder"
content.body = "Don't forget to drink water!"
Trigger notification after 60 seconds, only once.
iOS Swift
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: false)
Trigger notification every day at 8:00 AM.
iOS Swift
let trigger = UNCalendarNotificationTrigger(dateMatching: DateComponents(hour: 8, minute: 0), repeats: true)
Sample App
This app asks permission to send notifications when it starts. If allowed, it schedules a notification to appear after 10 seconds with a simple message.
iOS Swift
import UIKit
import UserNotifications
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) { granted, error inif granted {
self.scheduleNotification()
}
}
}
func scheduleNotification() {
let content = UNMutableNotificationContent()
content.title = "Hello!"
content.body = "This is your local notification."
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 10, repeats: false)
let request = UNNotificationRequest(identifier: "testNotification", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { error inif let error = error {
print("Error adding notification: \(error)")
}
}
}
}
OutputSuccess
Important Notes
Notifications only appear if the user grants permission.
Test notifications on a real device for best results.
Notifications can include sounds, badges, and custom actions.
Summary
Local notifications alert users even when the app is closed.
Always request permission before scheduling notifications.
Use triggers to control when notifications appear.
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
Step 1: Understand user permission requirement
iOS requires apps to ask users for permission before sending notifications to respect privacy.
Step 2: Request permission before scheduling
You must call the requestAuthorization method to get user consent before scheduling any notifications.
Final Answer:
Request permission from the user to send notifications -> Option A
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
Step 1: Identify the correct class for notification content
UNMutableNotificationContent is the class used to create and customize notification content.
Step 2: Differentiate from other classes
UNNotificationContent is abstract and cannot be instantiated; UNNotificationRequest is for scheduling, not content.
Final Answer:
let content = UNMutableNotificationContent() -> Option C
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
Step 1: Analyze the trigger setup
The trigger is set with timeInterval 5 seconds and repeats false, so it fires once after 5 seconds.
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.
Final Answer:
A notification appears after 5 seconds with the given title and body -> Option A
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
Step 1: Check the trigger timeInterval value
The timeInterval is set to -10, which is invalid because it must be positive.
Step 2: Understand trigger constraints
UNTimeIntervalNotificationTrigger requires a positive timeInterval to schedule the notification correctly.
Final Answer:
Negative timeInterval causes an error; timeInterval must be positive -> Option B
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)
To trigger at a specific time daily, use UNCalendarNotificationTrigger with repeats true and matching hour and minute.
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.
Final Answer:
UNCalendarNotificationTrigger(dateMatching: DateComponents(hour: 8, minute: 0), repeats: true) -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.