为什么只有我的最后一个本地通知函数被调用?

问题描述 投票:4回答:2

我是新手,现在正在尝试在UISwitch IBAction中调用多个请求本地通知的函数。我想在某个日期(一年中的每个季度第4、7、10、1个)发送通知。仅第四个季度函数被调用。如何获得全部四个功能?这是我的代码:

// UISwitch用于季度通知

@IBAction func quarterlyFrequencyTapped(_ sender: UISwitch) {

if quarterlyFrequency.isOn == true {

    firstQuarter(); secondQuarter(); thirdQuarter(); fourthQuarter()

    print("quarterly frequency is \(quarterlyFrequency.isOn)")

} else {

    removeQuarterlyNotification()

    print("quaterly frequency is \(monthlyFrequency.isOn)")

       }

}

//四个季度的功能

func firstQuarter() {
    let firstQuarterContent = UNMutableNotificationContent()
    firstQuarterContent.title = "First Quarter"
    firstQuarterContent.subtitle = "Some string"
    firstQuarterContent.body = "Some other string"

    var firstQuarterDate = DateComponents()
    firstQuarterDate.month = 3
    firstQuarterDate.day = 11
    firstQuarterDate.hour = 19
    firstQuarterDate.minute = 20

    let firstQuarterTrigger = UNCalendarNotificationTrigger(dateMatching: firstQuarterDate, repeats: true)
    let firstQuarterRequestIdentifier = "Quarterly"
    let firstQuarterRequest = UNNotificationRequest(identifier: firstQuarterRequestIdentifier, content: firstQuarterContent, trigger: firstQuarterTrigger)
    UNUserNotificationCenter.current().add(firstQuarterRequest, withCompletionHandler: nil)
}

func secondQuarter() {
    let secondQuarterContent = UNMutableNotificationContent()
    secondQuarterContent.title = "Second Quarter"
    secondQuarterContent.subtitle = "Some string"
    secondQuarterContent.body = "Some other string"

    var secondQuarterDate = DateComponents()
    secondQuarterDate.month = 3
    secondQuarterDate.day = 11
    secondQuarterDate.hour = 19
    secondQuarterDate.minute = 21

    let secondQuarterTrigger = UNCalendarNotificationTrigger(dateMatching: secondQuarterDate, repeats: true)
    let secondQuarterRequestIdentifier = "Quarterly"
    let secondQuarterRequest = UNNotificationRequest(identifier: secondQuarterRequestIdentifier, content: secondQuarterContent, trigger: secondQuarterTrigger)
    UNUserNotificationCenter.current().add(secondQuarterRequest, withCompletionHandler: nil)
}

func thirdQuarter() {
    let thirdQuarterContent = UNMutableNotificationContent()
    thirdQuarterContent.title = "Third Quarter"
    thirdQuarterContent.subtitle = "Some string"
    thirdQuarterContent.body = "Some other string"

    var thirdQuarterDate = DateComponents()
    thirdQuarterDate.month = 3
    thirdQuarterDate.day = 11
    thirdQuarterDate.hour = 19
    thirdQuarterDate.minute = 22

    let thirdQuarterTrigger = UNCalendarNotificationTrigger(dateMatching: thirdQuarterDate, repeats: true)
    let thirdQuarterRequestIdentifier = "Quarterly"
    let thirdQuarterRequest = UNNotificationRequest(identifier: thirdQuarterRequestIdentifier, content: thirdQuarterContent, trigger: thirdQuarterTrigger)
    UNUserNotificationCenter.current().add(thirdQuarterRequest, withCompletionHandler: nil)
}

func fourthQuarter() {
    let fourthQuarterContent = UNMutableNotificationContent()
    fourthQuarterContent.title = "Fourth Quarter"
    fourthQuarterContent.subtitle = "Some string"
    fourthQuarterContent.body = "Some other string"

    var fourthQuarterDate = DateComponents()
    fourthQuarterDate.month = 3
    fourthQuarterDate.day = 11
    fourthQuarterDate.hour = 19
    fourthQuarterDate.minute = 23

    let fourthQuarterTrigger = UNCalendarNotificationTrigger(dateMatching: fourthQuarterDate, repeats: true)
    //let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
    let fourthQuarterRequestIdentifier = "Quarterly"
    let fourthQuarterRequest = UNNotificationRequest(identifier: fourthQuarterRequestIdentifier, content: fourthQuarterContent, trigger: fourthQuarterTrigger)
    UNUserNotificationCenter.current().add(fourthQuarterRequest, withCompletionHandler: nil)
}
swift notifications usernotifications
2个回答
6
投票

我认为您使用的是+ iOS10,并且您正在使用UserNotifications框架,对吗?

[您的identifier很可能具有相同,并且每个都是UPDATING先前的通知。

存在identifier的原因是为了帮助您更新具有相同标识符的先前通知。例如,您发送一条通知,将乐谱从0-0更新为0-1。现在,屏幕上只有2条通知,而不是2条通知。

您的解决方法是为每个使用different标识符。

有关更多信息,请参阅WWDC视频的moment

编辑后更新:就像我说的...您的所有通知都以"Quarterly"作为标识符。为他们分别命名。


0
投票

只需一点帮助

您可以仅添加UUID()。uuidString作为标识符。

荣誉蜂蜜

© www.soinside.com 2019 - 2024. All rights reserved.