[英]user customized notification time from date picker in SwiftUI
So this is what I have so far, a picker displaying hours and minutes, and a notification that goes out every day at 2pm, I am trying to bind them so that the user can manually choose the time they would like the notification to go off, with time translated into 24 hours format and placed into ->所以这就是我到目前为止所拥有的,一个显示小时和分钟的选择器,以及一个每天下午 2 点发出的通知,我正在尝试绑定它们,以便用户可以手动选择他们希望通知 go 关闭的时间, 将时间转换为 24 小时格式并放入 ->
dateComponents.hour = 14
dateComponents.minute = 00
this is my picker code where I display time only:这是我的选择器代码,我只显示时间:
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") })
}
}
and this is a button that both request the permission to send daily notifications and enables it,这是一个按钮,既请求发送每日通知的权限并启用它,
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()
}
}
try something like this (untested):尝试这样的事情(未经测试):
in your Calendar View add:在您的日历视图中添加:
@State var selectedTime = Date()
and change:并改变:
DatePicker(selection: self.$selectedTime), displayedComponents: .hourAndMinute, label: { Text("Time") })
Edit编辑
Without knowing what your are really trying to achieve, my suggestion is;在不知道您真正想要实现的目标的情况下,我的建议是; try to break your code into individual elements, such as:尝试将您的代码分解为单个元素,例如:
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)
}
}
}
then in the section where you request the permission:然后在您请求许可的部分:
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 !!"))
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.