SwiftUI 中日期选择器的用户自定义通知时间
Posted
技术标签:
【中文标题】SwiftUI 中日期选择器的用户自定义通知时间【英文标题】:user customized notification time from date picker in SwiftUI 【发布时间】:2020-09-24 02:11:28 【问题描述】:这就是我到目前为止所拥有的,一个显示小时和分钟的选择器,以及一个每天下午 2 点发出的通知,我正在尝试绑定它们,以便用户可以手动选择他们想要通知的时间出发,将时间转换为 24 小时格式并放入 ->
dateComponents.hour = 14
dateComponents.minute = 00
这是我只显示时间的选择器代码:
import UserNotifications
import SwiftUI
struct Calendar: View
@State private var didTap:Bool = false
@State var alert = false
@State private var currentDate = Date()
@Environment(\.presentationMode) var presentationMode
@State private var isdateshowing = false
@State private var textfield = ""
var body: some View
List
Section
TextField("Choose at when you would like to be reminded", text: $textfield, onEditingChanged:
edit in
if edit
self.isdateshowing = true
else
self.isdateshowing = false
)
if isdateshowing
DatePicker(selection: .constant(Date()), displayedComponents: .hourAndMinute, label: Text("Time") )
这是一个按钮,既可以请求发送每日通知的权限,也可以启用它,
Section
Button(action:
UNUserNotificationCenter.current()
.requestAuthorization(options: [.alert, .sound, .badge]) (status, _) in
if status
let content = UNMutableNotificationContent()
content.title = "It Is Time!"
content.body = "LETS DO THIS!"
content.sound = UNNotificationSound.default
var dateComponents = DateComponents()
dateComponents.hour = 14
dateComponents.minute = 00
// let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: "noti", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
return
self.alert.toggle()
)
Text("Press to Enable Daily Notifications")
.alert(isPresented: $alert)
return Alert(title: Text("Please Enable Notification Access from Settings Pannel !!"))
struct Calendar_Previews: PreviewProvider
static var previews: some View
Calendar()
【问题讨论】:
【参考方案1】:尝试这样的事情(未经测试):
在您的日历视图中添加:
@State var selectedTime = Date()
并改变:
DatePicker(selection: self.$selectedTime), displayedComponents: .hourAndMinute, label: Text("Time") )
编辑
在不知道您真正想要达到的目标的情况下,我的建议是;尝试将您的代码分解为单个元素,例如:
func sendNotification()
let content = UNMutableNotificationContent()
content.title = "It Is Time!"
content.body = "LETS DO THIS!"
content.sound = UNNotificationSound.default
if let thisHour = Calendar.current.dateComponents([.hour], from: Date()).hour,
let selectedHour = Calendar.current.dateComponents([.hour], from: self.selectedTime).hour
// Note, this sends the notification at the selectedHour or later
if (thisHour >= selectedHour)
var dateComponents = DateComponents()
dateComponents.hour = selectedHour
dateComponents.minute = 0
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: "noti", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
然后在您请求许可的部分:
Section
Button(action:
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) (status, _) in
if status
self.sendNotification()
else
self.alert.toggle()
)
Text("Press to Enable Daily Notifications")
.alert(isPresented: $alert)
Alert(title: Text("Please Enable Notification Access from Settings Pannel !!"))
【讨论】:
非常感谢您的建议,很抱歉打扰您,但我对 SwiftUI 很陌生。我真的不确定将最后一部分放在通知部分的哪个位置,它不断给我错误(类型 hour 和 current 在代码中没有成员)。你会怎么做才能解决它? 我已经更新了我的答案。我不知道你为什么在小时和电流方面有错误。您需要出示您的代码。以上是关于SwiftUI 中日期选择器的用户自定义通知时间的主要内容,如果未能解决你的问题,请参考以下文章
如何从 WatchOS 3 中的自定义通知操作中打开特定的视图控制器