Ios 设置从日期开始重复本地通知
我想设置从日期开始的重复本地通知。例如: 开始日期:2018年6月25日 今天日期:2018年6月21日 我被困在这里了。以下代码正在运行,但从今天起,而不是从2018年6月25日起,将发出本地通知 请查看我的本地通知功能: 它应该每天重复,但从6月25日开始。 提前感谢试试这个Ios 设置从日期开始重复本地通知,ios,swift,uilocalnotification,Ios,Swift,Uilocalnotification,我想设置从日期开始的重复本地通知。例如: 开始日期:2018年6月25日 今天日期:2018年6月21日 我被困在这里了。以下代码正在运行,但从今天起,而不是从2018年6月25日起,将发出本地通知 请查看我的本地通知功能: 它应该每天重复,但从6月25日开始。 提前感谢试试这个 let notification = UNMutableNotificationContent() notification.subtitle = "" notification.sound = UNNotificat
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:00送达,
您将在设备设置中将日期修改为2018年6月25日下午6:59
或者将设备设置中的日期修改为2018年6月25日下午7:01
预定的通知将在该时间到达
这只是一个示例,它将在每个特定的工作日重复通知。如果要重复通知,应设置“任何人”顺序,如每日、每周、每周一等。。否则,应使用唯一id注册特定日期(无序日期)的多个通知
请参阅—
如果您希望每天重复通知,但希望跳过第一次。我认为那是不可能的。还有一个类似的问题
祝你好运 尝试以下代码行:
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)
到
更新答案:
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()
}
}
快乐编码…您在函数中传递的参数是什么-scheduleDosageLocalNotification?日期:2018年6月25日@KeyurTailor@DheerajD你找到解决办法了吗?如果是,你能分享吗?我将如何获得日价值?dayComponent.weekday=日期为?Int@ramjust随机获得1到7之间的值,如果我们得到第1天,每天1个用户都会在预定时间收到通知。感谢您的帮助。。。但我想你还没有明白我的意思@拉米不想跳过任何东西。我只想在n天后开始重复通知。能做到吗?谢谢你的帮助@ram,但我仍然没有得到我所需要的。谢谢你的回答@Payal Maniyar。但它不是每天重复,而是每三天重复一次。你必须发出本地通知。如果你想实施本地通知,请检查更新的答案。这是一种传统方式,不推荐感谢你的帮助@Payal Maniyar,但它仍然不适用于我。
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()