Swift - UNUsernotification 检查 BadgeNumberIcon 中的更改 [英] Swift - UNUsernotification checking changes in BadgeNumberIcon
问题描述
我想在标签中显示徽章编号的值.
I Would like to display the value of the badge number in a label.
到目前为止,我已经把所有东西都放在了我的 viewWillAppear 中.因此,每次加载控制器时,都会分配变量.代码如下:
So far i've put everything in my viewWillAppear. So every time the controller is loaded the variable is assigned. Here's the code:
var deliveredNotif = Int()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
deliveredNotif = UIApplication.shared.applicationIconBadgeNumber
myBadgeLabel.text = "\(deliveredNotif)"
}
我的问题是:如果控制器处于活动状态并且 viewWillAppear 已被调用,我如何更新 DeliverNotif?意思是如果我在控制器中,有没有办法触发一个函数,它会在每次 applicationIconBadgeNumber 的值改变时更新 DeliverNotif 的值?
My question is: How can i update deliveredNotif if the controller is active and so viewWillAppear is already been called? Meaning if I am in the controller is there a way to trigger a func which will update the value of deliveredNotif every time the value of applicationIconBadgeNumber is changed?
谢谢!
-------更新----我的解决方案我创建了一个常量变量:var iconBadgeNumber = NSNumber()
-------UPDATE----MY SOLUTION I created a constant variable: var iconBadgeNumber = NSNumber()
在我的 Appdelegate 中:
in my Appdelegate i have:
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .badge])
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "applicationIconBadgeNumber"), object: nil)
iconBadgeNumber = UIApplication.shared.applicationIconBadgeNumber + 1 as NSNumber
}
然后在我的控制器中:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(updateIconBadgeNumber), name: NSNotification.Name(rawValue: "applicationIconBadgeNumber"), object: nil)
}
@objc func updateIconBadgeNumber() {
if iconBadgeNumber == 0 {
print("zero")
} else {
print("there unread notification")
}
}
推荐答案
您可以将您的 UIViewController
设置为关键路径applicationIconBadgeNumber"的观察者:
You can set your UIViewController
as an observer for the key path "applicationIconBadgeNumber":
首先注册远程通知.在您的 didFinishLaunchingWithOptions 函数中,添加:
First register for remote notifications. In your didFinishLaunchingWithOptions function, add:
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .badge, .sound]) { _, _ in
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
}
不要忘记在文件顶部添加:import UserNotifications
don't forget to add also: import UserNotifications
on the top of your file
然后在您的 viewDidLoad 中添加:
Then add in your viewDidLoad:
UIApplication.shared.addObserver(self, forKeyPath: "applicationIconBadgeNumber", options: .new, context: nil)
然后,覆盖observeValue函数:
Then, override the observeValue function:
override func observeValue(forKeyPath keyPath: String?,
of object: Any?,
change: [NSKeyValueChangeKey : Any]?,
context: UnsafeMutableRawPointer?) {
if keyPath == "applicationIconBadgeNumber" {
deliveredNotif = UIApplication.shared.applicationIconBadgeNumber
myBadgeLabel.text = "\(deliveredNotif)"
}
}
这篇关于Swift - UNUsernotification 检查 BadgeNumberIcon 中的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!