UILocalNotification 不断推送多条消息

UILocalNotification keeps pushes multiple messages

我有一个 Struct ArrayBook,其中包含一个 数组 和一个名为 randomList 的函数,其中 return 是 数组 并通过例如return arrayList[randomNumber]。所以基本上,当我调用 ArrayBook.randomList 时,它会在每次执行时 return 数组中的一个随机项 - 我已经在操场上测试过这个,并使用 Xcode 和 Unbutton 函数。

现在很清楚了,我有一个功能正常的 UILocalNotification,它每小时安排一次本地推送通知(用于测试),消息为 ArrayBook.randomList。但是当收到通知时,应用程序一次推送多条消息,每小时增加 1 条。我不明白这是为什么,有什么想法吗?

    var dateComp:NSDateComponents = NSDateComponents()
    dateComp.year = 2015;
    dateComp.month = 06;
    dateComp.day = 03;
    dateComp.hour = 01;
    dateComp.minute = 00;
    dateComp.timeZone = NSTimeZone.systemTimeZone()

    var calender:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
    var date:NSDate = calender.dateFromComponents(dateComp)!

    var notification:UILocalNotification = UILocalNotification()
    notification.category = "Daily"
    notification.alertBody = arrayBook.randomList()
    notification.fireDate = date
    notification.repeatInterval = NSCalendarUnit.CalendarUnitHour
    notification.soundName = UILocalNotificationDefaultSoundName

大概您正在调用代码来重复安排通知 - 您必须这样做,否则您将始终获得相同的 alertBody 因为它是在安排通知时设置的,而不是在发送通知时设置的。

由于您指定 notification.repeatInterval = NSCalendarUnit.CalendarUnitHour,此通知将与您安排的所有其他通知一起每小时发送一次 - 每次增加一个。您应该从通知中删除 repeatInterval