gpt4 book ai didi

ios - 在 swift 3 的定时通知中更新角标(Badge)?

转载 作者:行者123 更新时间:2023-11-28 15:21:01 28 4
gpt4 key购买 nike

我一直在尝试通知编码,只是为了能够向我的应用程序添加一些通知,但我可以说,最大的问题是,

something.badge = 3

等等,但我不能说

something.badge +=1

something.badge = badge +1

到目前为止,我的代码是这样的

@IBAction func sendNotification(_ sender: Any) {// in this button you make a notification

UIApplication.shared.applicationIconBadgeNumber = 1
let asnwer1 = UNNotificationAction(identifier: "asnwer1", title: "You are", options: UNNotificationActionOptions.foreground)
let asnwer2 = UNNotificationAction(identifier: "asnwer2", title: "Obviously you fucking are!" , options: UNNotificationActionOptions.foreground)
let category = UNNotificationCategory(identifier: "myCategory", actions: [asnwer1, asnwer2], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([category])
// created nitification

let content = UNMutableNotificationContent()
content.title = "Are you pathetic for not working out??"
content.subtitle = "are you?? "
content.body = "Are you sure?"
content.categoryIdentifier = "myCategory"
/////// THIS NEEDS IMPROVEMENT
if UIApplication.shared.applicationIconBadgeNumber == 1{
content.badge = 2
}
if UIApplication.shared.applicationIconBadgeNumber == 2{
content.badge = 3
}
if UIApplication.shared.applicationIconBadgeNumber == 3{
content.badge = 4
}
if UIApplication.shared.applicationIconBadgeNumber == 4{
content.badge = 5
}
if UIApplication.shared.applicationIconBadgeNumber == 5{
content.badge = 6
}
if UIApplication.shared.applicationIconBadgeNumber == 6{
content.badge = 7
}
if UIApplication.shared.applicationIconBadgeNumber == 7{
content.badge = 8
}
if UIApplication.shared.applicationIconBadgeNumber >= 8{
content.badge = 9
}


let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 10, repeats: false)
let request = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)

UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)// the previous lne created a let named request...youre using it here
}

这里的问题是,我一直在说,如果角标(Badge)号码是那个特定的号码,那就把它变成另一个特定的号码......还有其他解决方法吗?

最佳答案

我假设您想简化所有这些条件的编写方式。

怎么样:

content.badge = Swift.min(9, UIApplication.shared.applicationIconBadgeNumber + 1)

那么就不需要写if子句了。

关于ios - 在 swift 3 的定时通知中更新角标(Badge)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46007909/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com