简体   繁体   English

用户从 SwiftUI 中的日期选择器自定义通知时间

[英]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.

相关问题 iOS 基于用户在 ToDo DatePicker (Swift & SwiftUI) 中设置的日期时间的本地通知 - iOS Local Notification based on date time set by user in ToDo DatePicker (Swift & SwiftUI) 日期选择器按钮 / Swiftui - Date Picker Button / Swiftui 如何为Swift设置本地通知时间到日期选择器的时间 - How to set local notification time to time on date picker for Swift 将 FetchedRequest/核心数据中的 Date() 加载到日期选择器 SwiftUI - Load Date() from FetchedRequest/Core Data into Date Picker SwiftUI UIActionsheet选择器中的日期和时间 - Date and Time in UIActionsheet Picker SwiftUI iOS 14 日期选择器奇怪的行为 - SwiftUI iOS 14 Date Picker Weird Behavior 从Swift 3中的日期选择器安排使用火灾日期的每周可重复本地通知 - Scheduling weekly repeatable local notification with fire date from date picker in Swift 3 尝试将日期格式化程序类型(从时间选择器)转换为 Integer - Trying to convert Date formatter type (from time picker) to Integer 在 Swift/SwiftUI 中仅显示日期 object 的时间 - Displaying only time from a Date object in Swift/SwiftUI 如何将日期时间选择器中的日期和时间限制从现在设置为现在的1个月? - how to set date and time limitation in Date Time Picker from Now to 1 Month from Now?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM