SwiftUI:UNUserNotificationCenter
程序员文章站
2024-03-24 13:13:04
...
import SwiftUI
import UserNotifications
struct ContentView: View {
@State var backgroundColor = Color.red
var body: some View {
VStack {
Button("Request permission") {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, error in
if success {
print("All set!")
} else if let error = error {
print(error.localizedDescription)
}
}
}
Button("Schedule Notification") {
let content = UNMutableNotificationContent()
content.title = "Feed the cat"
content.subtitle = "It looks hungry"
content.sound = UNNotificationSound.default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
}
}
}
}
SwiftUI: