为啥只有我最后一个本地通知功能被调用?
Posted
技术标签:
【中文标题】为啥只有我最后一个本地通知功能被调用?【英文标题】:Why only my last local notification function is getting called?为什么只有我最后一个本地通知功能被调用? 【发布时间】:2017-03-12 02:06:21 【问题描述】:我对 swift 还很陌生,我试图在 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)
【问题讨论】:
你怎么知道只有第四个被调用?这段代码没有任何问题。 这个firstQuarter(); secondQuarter(); thirdQuarter(); fourthQuarter()
执行所有 4...没有错。也许您的函数本身工作不正确
为了测试这一点,我将每个季度的函数设置为相隔一分钟并运行模拟器。只有第四个通知在模拟器中运行。
没有。使用该行代码调用所有 4 个函数。你还有其他问题。
【参考方案1】:
我认为您使用的是 +ios10 并且您正在使用 UserNotifications 框架,对吗?
很可能您的identifier
s 具有相同,并且每个都更新之前的通知。
identifier
存在的原因是为了帮助您更新具有相同标识符的先前通知。例如,您发送通知将分数从0-0
更新为0-1
。而不是在屏幕上显示 2 个通知,您现在只有一个。
您的解决方法是为每个标识符使用 不同 标识符。
有关更多信息,请参阅moment 的 WWDC 视频
修改后更新:
就像我说的...您的所有通知都以"Quarterly"
作为其标识符。给他们一个单独的名字。
【讨论】:
@nsd32 因为你是新来的。一旦你得到你喜欢的答案,建议 1. 点赞 2. 点击勾选mark it as accepted answer 感谢您的提示。我只是单击了复选标记,但是由于我的声誉仍然很低,因此没有显示赞成票,但显然它仍然被记录在某个地方。再次感谢所有帮助。【参考方案2】:一点帮助
您可以添加 UUID().uuidString 作为标识符。
感谢亲爱的
【讨论】:
以上是关于为啥只有我最后一个本地通知功能被调用?的主要内容,如果未能解决你的问题,请参考以下文章