我想从日期开始设置重复的本地通知。例如:
开始日期: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日开始。 预先感谢!
答案 0 :(得分:1)
尝试一下,
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点到达, 您将设备设置中的日期修改为6.59 pm和2018年6月25日 或将设备设置中的日期修改为7.01 pm和2018年6月25日
预定的通知将在那个时间到达
仅是示例,它将在每个特定的工作日重复通知。如果要重复通知,则应设置任何人的订单,例如每天,每周,每个星期一等。否则,您应在特定日期(无订单日)注册具有唯一ID的多个通知。
请参阅-How to show multiple local notifications?
如果您希望您的通知每天重复,但您想跳过第一次出现。我认为那是不可能的。还有一个类似的问题
Scheduling local notifications to repeat daily from tomorrow in Swift
祝你好运!
答案 1 :(得分:1)
请尝试以下代码行:
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([.day,.month,.year,.hour, .minute], from: date!)//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()
}
}
我已经改变
var components = calendar.dateComponents([.hour, .minute], from: date)
到
var components = calendar.dateComponents([.day,.month,.year,.hour, .minute], from: date!)
更新后的答案:
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([.day, .month, .year, .hour, .minute], from: date!) //calendar.dateComponents([.hour, .minute], from: date)
components.hour = 08
components.minute = 00
var notification: UILocalNotification = UILocalNotification()
notification.category = "Dosage_Day"
notification.alertBody = "Local notification"
notification.fireDate = newDate
notification.repeatInterval = .day
print(notification)
print(notification.fireDate)
UIApplication.sharedApplication().scheduleLocalNotification(notification)
Utilities.saveContextForAppInfo()
} }
快乐编码。...