我正在使用苹果 Watch 应用程序,我需要提醒用户通知唤醒用户 .
我可以用make repeat : true
设置 UserNotification
,它会每天提醒用户 .
但我需要的是在单日应用程序将根据用户选择通知,如下所示 .
如果用户选择3分钟理想时间,每3分钟用户就会收到振动通知 .
5分钟,10分钟和15分钟相同 .
我正在使用下面的代码 UserNotification
.
func scheduleNotification(at date: Date) {
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)
let content = UNMutableNotificationContent()
content.title = "Test Reminder"
content.body = "Show More detail in Body!"
content.sound = UNNotificationSound.default()
content.categoryIdentifier = "myCategory"
if let path = Bundle.main.path(forResource: "logo", ofType: "png") {
let url = URL(fileURLWithPath: path)
do {
let attachment = try UNNotificationAttachment(identifier: "logo", url: url, options: nil)
content.attachments = [attachment]
} catch {
print("The attachment was not loaded.")
}
}
let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print("Uh oh! We had an error: \(error)")
}
}
}
使用 repeats: true 下面的触发器
let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: true)
它在第二天同时重复 .
是否有任何方法或方法根据用户选择在特定间隔后设置重复?
任何帮助将不胜感激!
提前致谢 .
1 回答
尝试使用UNTimeIntervalNotificationTrigger而不是
UNCalendarNotificationTrigger
.例如:
这将创建一个每2分钟触发一次的触发器 .
timeInterval
是触发每个通知之间的秒数 .