我想设置从日期开始重复的本地通知。例如:
开始日期:2018 年 6 月 25 日
今天日期:2018 年 6 月 21 日
我被困在这里了。下面的代码可以工作,但它从今天开始而不是从 2018 年 6 月 25 日开始触发本地通知。
请看一下我的本地通知功能:
func scheduleDosageLocalNotification(date: Date) {
reminder.dosageIdentifier = "Dosage_Day"
var calendar = Calendar.current
calendar.timeZone = TimeZone.current
let notificationContent = UNMutableNotificationContent()
// Configure Notification Content
notificationContent.title = "DOSAGE REMINDER"
notificationContent.body = "Remember to take your TEST tablet dialy."
// Set Category Identifier
notificationContent.categoryIdentifier = Notification.Category.First
var components = calendar.dateComponents([.hour, .minute], from: date)
components.hour = 08
components.minute = 00
let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)
// let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: interval!, repeats: true)
// Create Notification Request
let identifier = "Dosage_Day"
let notificationRequest = UNNotificationRequest(identifier: identifier, content: notificationContent, trigger: notificationTrigger)
// Add Request to User Notification Center
UNUserNotificationCenter.current().add(notificationRequest) { (error) in
if let error = error {
print("Unable to Add Notification Request (\(error), \(error.localizedDescription))")
}
Utilities.saveContextForAppInfo()
}
}
它应该每天重复一次,但从 6 月 25 日开始。提前致谢!!
尝试这个,
let notification = UNMutableNotificationContent()
notification.subtitle = ""
notification.sound = UNNotificationSound.default()
notification.userInfo = userInfo
notification.title = Title
notification.body = Message
let timeStr = time
let splitTime:[String] = timeStr.components(separatedBy: ":")
var dayComponent = DateComponents()
dayComponent.weekday = day as? Int //[1 to 7 get randomly]
dayComponent.hour = Int(splitTime[0])
dayComponent.minute = Int(splitTime[1])
let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: dayComponent, repeats: true)
let lnMessageId:String = message
let dayRequest = UNNotificationRequest(identifier: lnMessageId , content: notification, trigger: notificationTrigger)
UNUserNotificationCenter.current().add(dayRequest, withCompletionHandler: {(_ error: Error?) -> Void in
if error == nil
{
//print("success")
}
else
{
//print("UNUserNotificationCenter Error : \(String(describing: error?.localizedDescription))")
}
})
如果您想在设备中测试通知,
假设下一次通知于 2018 年 6 月 25 日晚上 7 点到达,
您将设备设置中的日期修改为 2018 年 6 月 25 日下午 6 点 59 分
或者您将设备设置中的日期修改为晚上 7 点和 2018 年 6 月 25 日
预定的通知将在那时到达,
这只是示例,它会在每个特定工作日重复通知。如果您想重复通知,您应该设置任何人的顺序,例如每天、每周、每周一等。否则,您应该使用唯一的 ID 注册特定日期(无序天)的多个通知。
参考这个-如何显示多个本地通知? https://stackoverflow.com/questions/50947393/how-to-show-multiple-local-notifications/50947657#50947657
如果您希望通知每天重复,但您想跳过第一次出现。我认为这是不可能的。有一个类似的问题
在 Swift 中安排本地通知从明天开始每天重复 https://stackoverflow.com/questions/44872429/scheduling-local-notifications-to-repeat-daily-from-tomorrow-in-swift
祝你好运!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)