Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to request permission for local notifications.
iOS Swift
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
if granted {
print("Permission [1]")
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing 'granted' with 'denied'.
Using words unrelated to permission status.
✗ Incorrect
The completion handler returns 'granted' as true when permission is allowed.
2fill in blank
mediumComplete the code to create a notification content with a title.
iOS Swift
let content = UNMutableNotificationContent() content.[1] = "Hello!"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'body' instead of 'title' for the main heading.
Confusing 'subtitle' with 'title'.
✗ Incorrect
The 'title' property sets the main title of the notification.
3fill in blank
hardFix the error in the code to schedule a notification after 5 seconds.
iOS Swift
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: [1], repeats: false) let request = UNNotificationRequest(identifier: "test", content: content, trigger: trigger) UNUserNotificationCenter.current().add(request)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using zero or negative values causes errors.
Using too large a number delays notification too long.
✗ Incorrect
The timeInterval must be a positive number; 5 seconds schedules the notification correctly.
4fill in blank
hardFill both blanks to create a notification with a badge number and sound.
iOS Swift
content.[1] = 1 content.[2] = UNNotificationSound.default
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing 'title' or 'body' with badge or sound properties.
Using wrong property names for badge or sound.
✗ Incorrect
The 'badge' property sets the app icon badge number, and 'sound' sets the notification sound.
5fill in blank
hardFill all three blanks to create and schedule a notification with a 10-second delay and a custom identifier.
iOS Swift
let content = UNMutableNotificationContent() content.title = "Reminder" let trigger = UNTimeIntervalNotificationTrigger(timeInterval: [1], repeats: [2]) let request = UNNotificationRequest(identifier: [3], content: content, trigger: trigger) UNUserNotificationCenter.current().add(request)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting repeats to true causes repeated notifications.
Using a number instead of a string for identifier.
Using wrong time interval values.
✗ Incorrect
The time interval is 10 seconds, repeats is false to avoid repeating, and the identifier is a string to identify the notification.