gpt4 book ai didi

ios - 在同一天 swift 安排 UILocalNotification

转载 作者:可可西里 更新时间:2023-11-01 01:36:42 26 4
gpt4 key购买 nike

我正在尝试安排 UILocalNotifications。我想要实现的功能是在特定的一天重复通知,比如(每个星期一),这将由用户选择。有什么方法可以安排 UILocalNotification,以便仅在那天触发通知。

let notification = UILocalNotification()

let dict:NSDictionary = ["ID" : "your ID goes here"]
notification.userInfo = dict as! [String : String]
notification.alertBody = "\(title)"
notification.alertAction = "Open"
notification.fireDate = dateToFire
notification.repeatInterval = .Day // Can be used to repeat the notification
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().scheduleLocalNotification(notification)

最佳答案

我不确定这对你有帮助,但我以前用过这个。我创建了 7 个按钮来选择一周中的 7 天。这是我的旧代码:

class RemindNotification {

var timerNotification = NSTimer()

func notification(story: Story) {
let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
let date = NSDate()

let dateComponents = calendar!.components([NSCalendarUnit.Day, NSCalendarUnit.WeekOfMonth, NSCalendarUnit.Month,NSCalendarUnit.Year,NSCalendarUnit.Hour,NSCalendarUnit.Minute], fromDate:date)
dateComponents.hour = story.remindeAtHour
dateComponents.minute = story.remindeAtMinute
for index in 0..<story.remindeAtDaysOfWeek.count {
if story.remindeAtDaysOfWeek[index] == true {
if index != 6{
dateComponents.weekday = index + 2
fireNotification(calendar!, dateComponents: dateComponents)
} else {
dateComponents.weekday = 1
fireNotification(calendar!, dateComponents: dateComponents)
}
} else {
dateComponents.weekday = 0
}
}
}

func fireNotification(calendar: NSCalendar, dateComponents: NSDateComponents) {
let notification = UILocalNotification()
notification.alertAction = "Title"
notification.alertBody = "It's time to take a photo"
notification.repeatInterval = NSCalendarUnit.WeekOfYear
notification.fireDate = calendar.dateFromComponents(dateComponents)
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
}

这是我的应用程序,我可以选择日期,然后它可以在我设置的每一天的特定时间触发 LocalNotification。

你可以引用一下。希望对您有所帮助。

关于ios - 在同一天 swift 安排 UILocalNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36563707/

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