快速多次通知

时间:2016-05-16 01:17:17

标签: ios swift

    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个通知。我做得对吗?有时我会在开火时收到重复的通知。

1 个答案:

答案 0 :(得分:0)

好的,所以在你提到的评论中,你在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)

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