为什么只有我的最后一个本地通知函数被调用? [英] Why only my last local notification function is getting called?

查看:49
本文介绍了为什么只有我的最后一个本地通知函数被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 swift 还很陌生,我正在尝试调用多个函数,这些函数在 UISwitch IBAction 中请求本地通知.我想在某个日期发送通知 - 一年中的每个季度的第 4、7、10、1 个月.只有第四季度函数被调用.如何让所有四个函数都被调用?这是我的代码:

I'm fairly new to swift and am trying to call multiple functions that request a local notification inside a UISwitch IBAction. I want to send a notification on a certain date - each quarter of the year on months 4, 7, 10, 1. Only the fourth quarter function is getting called. How can I get all four functions to be called? Here is my code:

//季度通知的 UISwitch

// UISwitch for quarterly notifications

@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)
}

推荐答案

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

I think you're on +iOS10 and you're using UserNotifications framework right?

很可能您的标识符具有相同并且每个都是更新之前的通知.

Very likely your identifiers have the same and each are UPDATING the previous notification.

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

The reason identifier exists is to assist you with updated a previous notification that has the same identifier. For example you send a notification to update the score from 0-0 to 0-1. And instead of having 2 notifications on the screen you now have only one.

您的解决方法是为每个使用不同标识符.

Your fix is to use different identifiers for each.

有关详情,请参阅 WWDC 的时刻视频

For more see this moment for the WWDC video

编辑后更新:就像我说的……你所有的通知都有 "Quarterly" 作为它们的标识符.给他们一个单独的名字.

UPDATE after your edit: just as I said...all your notifications have "Quarterly" as their identifier. Give them each a separate name.

这篇关于为什么只有我的最后一个本地通知函数被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆