ios - 在 SwiftUI 中切换通知
问题描述
我希望我的应用程序的用户打开或关闭他们是否想在中午收到通知。我设法让它发挥作用,但我遇到了一些问题。
- 通知有时会被触发,有时不会
- 通知适用于按钮,但不适用于切换。
我尝试寻找解决方案,但没有运气。如果有人可以提供帮助,那就太好了。
struct Alert: View {
@State var noon = false
var body: some View {
VStack {
Toggle(isOn: $noon) {
Text("noon")
}
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 = "Meds"
content.subtitle = "Take your meds"
content.sound = UNNotificationSound.default
var dateComponents = DateComponents()
dateComponents.hour = 12
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
// choose a random identifier
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
// add our notification request
UNUserNotificationCenter.current().add(request)
}
}
}
}
解决方案
推荐阅读
- spring - 扩展 Spring 安全用户类
- visual-studio-2019 - 我无法签署 bin\Debug\Mainpaint.exe
- ios - UICollectionView 的宽度在 UITableViewCell 内没有改变
- python - 使用 Python 和 Gmail API 将电子邮件标记为已读
- twitter-bootstrap - Bootstrap 3 虚线进度条
- python - 将“24OCT2020:00:00:00”格式转换为日期时间
- word-embedding - 如何在词嵌入模型 BERT 上使用我自己的语料库
- spring-boot - 如何检测 RSocket 连接是否成功?
- bash - 如果在一个命令行中有多个重定向运算符,它们是如何工作的?(重击)
- python - CVXPY 优化问题。DCPError:问题不遵循 DCP 规则