gpt4 book ai didi

ios - 快速多重通知

转载 作者:行者123 更新时间:2023-11-30 13:25:33 25 4
gpt4 key购买 nike

    let dateComponents1 = calendar!.components([NSCalendarUnit.Day, NSCalendarUnit.WeekOfMonth, NSCalendarUnit.Month,NSCalendarUnit.Year,NSCalendarUnit.Hour,NSCalendarUnit.Minute], fromDate:date)
dateComponents1.month = 5
dateComponents1.day = 12
dateComponents1.hour = 20
dateComponents1.minute = 00
let notification1 = UILocalNotification()
notification1.alertAction = "notification1"
notification1.alertBody = "notification1"
notification1.repeatInterval = NSCalendarUnit.WeekOfYear
notification1.fireDate = calendar!.dateFromComponents(dateComponents1)
UIApplication.sharedApplication().scheduleLocalNotification(notification1)




let dateComponents2 = calendar!.components([NSCalendarUnit.Day, NSCalendarUnit.WeekOfMonth, NSCalendarUnit.Month,NSCalendarUnit.Year,NSCalendarUnit.Hour,NSCalendarUnit.Minute], fromDate:date)
dateComponents2.month = 5
dateComponents2.day = 13
dateComponents2.hour = 14
dateComponents2.minute = 00
let notification2 = UILocalNotification()
notification2.alertAction = "notification2"
notification2.alertBody = "notification2"
notification2.repeatInterval = NSCalendarUnit.WeekOfYear
notification2.fireDate = calendar!.dateFromComponents(dateComponents2)
UIApplication.sharedApplication().scheduleLocalNotification(notification2)

我尝试每周在特定时间触发 2 个通知。这就是我实现通知的方式。每周周四下午 8 点和周五下午 2 点触发 2 个通知。我做对了吗?有时我会在火灾发生时收到重复的通知。

最佳答案

好的,所以在评论中您提到您在 didBecomeActive 中调用了这段代码。问题是每次您的应用程序变得活跃时,您都会安排一个您不想要的重复通知。因此,您应该使用以下代码:

let dateComponents1 = calendar!.components([NSCalendarUnit.Day, NSCalendarUnit.WeekOfMonth, NSCalendarUnit.Month,NSCalendarUnit.Year,NSCalendarUnit.Hour,NSCalendarUnit.Minute], fromDate:date)
dateComponents1.month = 5
dateComponents1.day = 12
dateComponents1.hour = 20
dateComponents1.minute = 00
let notification1 = UILocalNotification()
notification1.alertAction = "notification1"
notification1.alertBody = "notification1"
notification1.repeatInterval = NSCalendarUnit.WeekOfYear
notification1.fireDate = calendar!.dateFromComponents(dateComponents1)
UIApplication.sharedApplication().cancelLocalNotification(notification1)
UIApplication.sharedApplication().scheduleLocalNotification(notification1)




let dateComponents2 = calendar!.components([NSCalendarUnit.Day, NSCalendarUnit.WeekOfMonth, NSCalendarUnit.Month,NSCalendarUnit.Year,NSCalendarUnit.Hour,NSCalendarUnit.Minute], fromDate:date)
dateComponents2.month = 5
dateComponents2.day = 13
dateComponents2.hour = 14
dateComponents2.minute = 00
let notification2 = UILocalNotification()
notification2.alertAction = "notification2"
notification2.alertBody = "notification2"
notification2.repeatInterval = NSCalendarUnit.WeekOfYear
notification2.fireDate = calendar!.dateFromComponents(dateComponents2)
UIApplication.sharedApplication().cancelLocalNotification(notification2)
UIApplication.sharedApplication().scheduleLocalNotification(notification2)

我在这里所做的是取消先前的通知(上次应用程序激活时安排的通知),然后再次安排它,这样您就不会收到太多重复的通知。此方法的替代方法是将代码放置在仅被调用一次的位置,这样您就不会收到重复的通知。

关于ios - 快速多重通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37245418/

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