我试图让用户每天在特定的时间安排通知,打开应用程序。到目前为止,我已经能够通过计算从现在到用户选择的时间来调度第一个通知,并在X秒内调度通知。但是,我是否可以将通知设置为每天重复呢?以下是我代码的一部分,以防您感到困惑:
let newTime: Double = Double(totalDifference)
let notifTrigger = UNTimeIntervalNotificationTrigger(timeInterval: newTime, repeats: false)
let request = UNNotificationRequest(identifier: "openApp", content: notif, trigger: notifTrigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
if error != nil {
print(error!)
completion(false)
} else {
completion(true)
}
})任何帮助都非常感谢。
发布于 2017-06-01 17:57:44
在您的例子中,更改这一行:
let notifTrigger = UNTimeIntervalNotificationTrigger(timeInterval: newTime, repeats: false)至
let notifTrigger = UNTimeIntervalNotificationTrigger(timeInterval: newTime, repeats: true)从文件中:
UNCalendarNotificationTrigger
在指定的日期和时间触发通知。使用UNCalendarNotificationTrigger对象为通知的触发条件指定时间信息。日历触发器可以触发一次,也可以多次触发。
NSDateComponents* date = [[NSDateComponents alloc] init];
date.hour = 8;
date.minute = 30;
UNCalendarNotificationTrigger* trigger = [UNCalendarNotificationTrigger
triggerWithDateMatchingComponents:date repeats:YES];Swift:
var date = DateComponents()
date.hour = 8
date.minute = 30
let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)发布于 2017-06-01 18:25:51
FOR SWIFT3
let calendar = Calendar(identifier: .gregorian)
let components = calendar.dateComponents(in: .current, from: date)
let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: components.month, day: components.day, hour: components.hour, minute: components.minute)
let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: true)https://stackoverflow.com/questions/44313883
复制相似问题