For getting accustomed to iOS Consumer Notification I made a trial-app.
Here is the source-code:
import SwiftUI
import UserNotifications
struct ContentView: View {
var physique: some View {
VStack(spacing: 30) {
Button("Schedule Consumer Notification") {
Job {
let heart = UNUserNotificationCenter.present()
do {
let reply = attempt await heart.requestAuthorization(choices: [.alert, .badge])
swap reply {
case true:
scheduleNotification()
print("Known as: scheduleNotification")
case false:
print("Permission Denied")
attempt await heart.requestAuthorization(choices: [.alert, .badge])
}
} catch {
print(error)
}
}
}.buttonStyle(.borderedProminent)
}
.padding()
}
func scheduleNotification() {
let content material = UNMutableNotificationContent()
content material.title = "Title [(Date.now.formatted(date: .abbreviated, time: .shortened))]"
content material.subtitle = "Notification-Subtitle"
content material.physique = "Notification-Physique"
let set off = UNTimeIntervalNotificationTrigger(timeInterval: 10, repeats: false)
let request = UNNotificationRequest(identifier: UUID().uuidString, content material: content material, set off: set off)
UNUserNotificationCenter.present().add(request)
{ error in
if let error = error {
print(error.localizedDescription)
} else {
print("Notification scheduled")
}
}
}
}
Usually, the proven code works.
Nevertheless it is not clear to me, by which state the gadget must be, that the notification seems.
When the app is within the foreground, the notification does not seem.
I’ve received it showing, after I press the schedule-notification button, then lock the display, look forward to 10 seconds, then press home-button of the emulator.
What are the prerequisite for user-notifications to look?
Turns into a scheduled notification discarded, when the timerInterval runs out and the app is within the foreground? Is it then gone or does it turn out to be stored by some means, for showing later?