【发布时间】:2018-08-13 06:29:15
【问题描述】:
我想为用户创建一个时间表,目前,我知道如何创建一个特定的时间,但我想创建这样的通知,例如针对不同的日子,或者只针对周末,或者针对哪个用户设置的日期. 我的每小时通知代码:
var notifTime = Int()
var notifMin = Int()
func test() {
let content = UNMutableNotificationContent()
content.title = "Test"
content.body = "It's test bro"
content.sound = UNNotificationSound.default()
let gregorian = Calendar(identifier: .gregorian)
let now = Date()
var components = gregorian.dateComponents([.year, .month, .day, .hour, .minute, .second], from: now)
components.hour = notifTime
components.minute = notifMin
components.second = 0
let date = gregorian.date(from: components)!
let triggerDaily = Calendar.current.dateComponents([.hour,.minute,.second,], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)
let request = UNNotificationRequest(identifier: "any", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
对于时间选择,我使用日期选择器。
那么我怎样才能创建每天的日程安排通知呢?
【问题讨论】:
-
我认为您没有为 notifTime 和 notifMin 分配任何值,因此您遇到了问题。那么,能否说明一下 notifTime 和 notifMin 背后的逻辑?,那么,很容易找到解决方案。
-
@Pratik Sodha 为此变量 notifTime 和 notifMin 我从日期选择器获取数据
标签: ios swift notifications