无法从 Notification 获取变量?
Can't get variable from Notification?
我想写一个项目有一个通知可以打印存储在主viewcontroller中的数据,但是当通知被触发时,总是得到空数据。
在ViewController
arrPerson = [String]()
override func viewDidLoad() {
super.viewDidLoad()
arrPerson.append("Peter")
arrPerson.append("Jack")
setNotification()
}
func printTheName() {
print("In printTheName")
print("arrPerson:\(arrPerson.count)")
for x in arrPerson {
print(x)
}
}
func setNotification() {
let notification = UNMutableNotificationContent()
notification.title = "title"
notification.subtitle = ""
notification.body = "body"
notification.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
let request = UNNotificationRequest(identifier: "check", content: notification, trigger: trigger)
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.add(request) { (error) in
if error != nil {
print("notificationCenter.add ERROR:\(error)")
// Handle any errors.
}
}
}
在 Appdelegate 中
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("In userNotificationCenter")
ViewController().printTheName()
}
代码会打印出"userNotificationCenter","In printTheName","arrPerson:0".
我需要从通知中获取数据。为什么从通知中调用 printTheName 时 arrPerson.count 为 0?
尝试在您的 appdelegate 中使用 window 以编程方式实例化 viewController。
由于您的 viewController class 是登录屏幕,因此您将能够在通知 center.Or 中获取计数,请在主 Class 中调用 userNotifationCenter 方法,而不是在此处。
我想写一个项目有一个通知可以打印存储在主viewcontroller中的数据,但是当通知被触发时,总是得到空数据。
在ViewController
arrPerson = [String]()
override func viewDidLoad() {
super.viewDidLoad()
arrPerson.append("Peter")
arrPerson.append("Jack")
setNotification()
}
func printTheName() {
print("In printTheName")
print("arrPerson:\(arrPerson.count)")
for x in arrPerson {
print(x)
}
}
func setNotification() {
let notification = UNMutableNotificationContent()
notification.title = "title"
notification.subtitle = ""
notification.body = "body"
notification.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
let request = UNNotificationRequest(identifier: "check", content: notification, trigger: trigger)
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.add(request) { (error) in
if error != nil {
print("notificationCenter.add ERROR:\(error)")
// Handle any errors.
}
}
}
在 Appdelegate 中
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("In userNotificationCenter")
ViewController().printTheName()
}
代码会打印出"userNotificationCenter","In printTheName","arrPerson:0".
我需要从通知中获取数据。为什么从通知中调用 printTheName 时 arrPerson.count 为 0?
尝试在您的 appdelegate 中使用 window 以编程方式实例化 viewController。 由于您的 viewController class 是登录屏幕,因此您将能够在通知 center.Or 中获取计数,请在主 Class 中调用 userNotifationCenter 方法,而不是在此处。