gpt4 book ai didi

ios - 制作仅在特定日期重复的本地通知

转载 作者:行者123 更新时间:2023-11-28 06:47:25 31 4
gpt4 key购买 nike

我正在为一个只应在特定日期触发并在那天重复直到用户决定将其删除的应用程序发送本地通知。一个示例通知在每周三上午 11:00 触发。

函数 - scheduleLocalNotification 在用户按下时间选择器上的完成时触发。

enter image description here

enter image description here

    var sundayNotification : UILocalNotification!
var mondayNotification : UILocalNotification!
var tuesdayNotification : UILocalNotification!
var wednesdayNotification : UILocalNotification!
var thursdayNotification : UILocalNotification!
var fridayNotification : UILocalNotification!
var saturdayNotification : UILocalNotification!


func scheduleLocalNotification() {

//Check the Dayname and match fireDate
if dayName == "Sunday" {

sundayNotification = UILocalNotification()

//timeSelected comes from the timePicker i.e. timeSelected = timePicker.date
sundayNotification.fireDate = fixNotificationDate(timeSelected)
sundayNotification.alertTitle = "Sunday's Workout"
sundayNotification.alertBody = "This is the message from Sunday's workout"
sundayNotification.alertAction = "Snooze"
sundayNotification.category = "workoutReminderID"
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.saveContext()
UIApplication.sharedApplication().scheduleLocalNotification(sundayNotification)


} else if dayName == "Monday" {

mondayNotification = UILocalNotification()
mondayNotification.fireDate = fixNotificationDate(timeSelected)
mondayNotification.alertTitle = "Monday's Workout"
mondayNotification.alertBody = "This is the message from Monday's workout"
mondayNotification.alertAction = "Snooze"
mondayNotification.category = "workoutReminderID"
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.saveContext()
UIApplication.sharedApplication().scheduleLocalNotification(mondayNotification)

} else if ..... }

}


func fixNotificationDate(dateToFix: NSDate) -> NSDate {


let dateComponets: NSDateComponents = NSCalendar.currentCalendar().components([NSCalendarUnit.Hour, NSCalendarUnit.Minute], fromDate: dateToFix)

dateComponets.second = 0

if dayName == "Sunday" {

dateComponets.weekday = 1 //n = 7

} else if dayName == "Monday" {

dateComponets.weekday = 2

} else if dayName == "Tuesday" {

dateComponets.weekday = 3

} else if dayName == "Wednesday" {

dateComponets.weekday = 4

} else if dayName == "Thursday" {

dateComponets.weekday = 5

} else if dayName == "Friday" {

dateComponets.weekday = 6

} else if dayName == "Saturday" {

dateComponets.weekday = 7

}


let fixedDate: NSDate! = NSCalendar.currentCalendar().dateFromComponents(dateComponets)

return fixedDate
}

在检查当天以分配正确的 UILocalNotification 通知之后,奇怪的是,这仍然不起作用,例如在不同的日期(星期日上午 10:10 和星期一上午 10:15)设置不同的通知时间不会改变任何内容。通知仍然在我没有选择的那一天触发,即今天在这两个时间而不是那些日子。

关于我可能做错或遗漏了什么的任何线索?提前致谢。 :)

最佳答案

经过一些工作,我想到了这个。我们首先将日期更改为用户选择的第一个触发日期。

func setNotificationDay(today: NSDate, selectedDay: Int) -> NSDate {
let daysToAdd: Int!
let calendar = NSCalendar.currentCalendar()
let weekday = calendar.component(.Weekday, fromDate: today)

if weekday > selectedDay {
daysToAdd = (7 - weekday) + selectedDay
} else {
daysToAdd = (selectedDay - weekday)
}

let newDate = calendar.dateByAddingUnit(.Weekday, value: daysToAdd, toDate: today, options: [])

return newDate! //if you don't have a date it'll crash
}

您可以只使用 NSDate() 而不是要求它作为参数。我会把它留给你。

现在,我们需要设置通知。我没有对您的 fixNotificationDate() 做任何事情,但您可以轻松地添加它。这里的关键点是设置 notification.repeatInterval = .Weekday 否则它不会重复。我还添加了一个字典来设置日期名称。这比像你那样重复代码要好。

它确实创建了对 NSCalendar 的第二次调用,但您可能会找到一种方法来避免在您的代码中这样做。

func scheduleLocalNotification(date: NSDate) {
let dayDic = [1:"Sunday",
2:"Monday",
3:"Tuesday",
4:"Wednesday",
5:"Thursday",
6:"Friday",
7:"Saturday"]

let calendar = NSCalendar.currentCalendar()
let weekday = calendar.component(.Weekday, fromDate: date)

let notification = UILocalNotification()
notification.fireDate = date
notification.repeatInterval = .Weekday
notification.alertTitle = String(format: "%@'s Workout", dayDic[weekday]!)
notification.alertBody = String(format: "This is the message from %@'s workout", dayDic[weekday]!)
notification.alertAction = "Snooze"
notification.category = "workoutReminderID"
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}

这应该可以解决您的问题。我没有测试它,因为它至少需要一个星期! ;)

希望对您有所帮助!

关于ios - 制作仅在特定日期重复的本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35912198/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com