如何在推送通知时对 ViewController 执行操作并检索其数据
How to perform an action on a ViewController when pushing a notification and retrieve its data
我想知道如何在通知到达并且用户点击它时更改 ViewController 上 UITextField 的值。通知包含我将放在该 UITextField 上的字符串。
This is how my app looks
我目前可以检索 AppDelegate 上的通知数据,并决定当用户点击通知时必须选择哪个选项卡。我是这样做的:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
let fragmento = response.notification.request.content.userInfo["fragmento"] as? String //Keeps the notification String "Fragmento" on a local variable
if fragmento == "anuncios"{ // Condition to select tab
if let tabbarController = self.window!.rootViewController as? UITabBarController {
tabbarController.selectedViewController = tabbarController.viewControllers?[1]
}
} else if fragmento == "registro"{
if let tabbarController = self.window!.rootViewController as? UITabBarController {
tabbarController.selectedViewController = tabbarController.viewControllers?[0]
}
}
completionHandler()
}
我想知道的是将数据从通知传递到特定的标签栏 ViewController 并根据该数据更改 UITextField 的值,然后在该 TextField 更改时执行操作它的价值。
我希望我解释得很好,否则请问我任何问题。非常感谢
NotificationCenter 可能是最简单的解决方案。定义一个自定义字符串用作 NotificationCenter 通知的通用名称,该通知将用于将信息从 AppDelegate 传递给正在收听的任何人。您可以附加字符串作为通知对象。
当您通过自定义 class 或您的视图控制器实例化该标签时,将您的通知侦听器添加到 NotificationCenter 并在收到通知后检索附加到通知的对象,仔细检查其string 然后如果是,用它来更新你的标签。
例如,在AppDelegate.swift中:
static let conferenciaNotification = NSNotification.Name(rawValue: "conferenciaNotification")
...
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
let updatedTextFromReceivedPushNotification = "Hello world"
NotificationCenter.default.post(name: AppDelegate.conferenciaNotification, object: updatedTextFromReceivedPushNotification)
}
在带有标签的视图控制器中:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(forName: AppDelegate.conferenciaNotification, object: nil, queue: OperationQueue.main) { (conferenciaNotification) in
if let conferenciaText = conferenciaNotification.object as? String {
myTextLabel.text = conferenciaText
}
}
}
请注意,当您添加观察者时,您可能应该保留对从 NotificationCenter 返回的 NSObjectProtocol 的引用,这样您就可以在视图控制器为 deinit() 时删除它。
我想知道如何在通知到达并且用户点击它时更改 ViewController 上 UITextField 的值。通知包含我将放在该 UITextField 上的字符串。
This is how my app looks
我目前可以检索 AppDelegate 上的通知数据,并决定当用户点击通知时必须选择哪个选项卡。我是这样做的:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
let fragmento = response.notification.request.content.userInfo["fragmento"] as? String //Keeps the notification String "Fragmento" on a local variable
if fragmento == "anuncios"{ // Condition to select tab
if let tabbarController = self.window!.rootViewController as? UITabBarController {
tabbarController.selectedViewController = tabbarController.viewControllers?[1]
}
} else if fragmento == "registro"{
if let tabbarController = self.window!.rootViewController as? UITabBarController {
tabbarController.selectedViewController = tabbarController.viewControllers?[0]
}
}
completionHandler()
}
我想知道的是将数据从通知传递到特定的标签栏 ViewController 并根据该数据更改 UITextField 的值,然后在该 TextField 更改时执行操作它的价值。
我希望我解释得很好,否则请问我任何问题。非常感谢
NotificationCenter 可能是最简单的解决方案。定义一个自定义字符串用作 NotificationCenter 通知的通用名称,该通知将用于将信息从 AppDelegate 传递给正在收听的任何人。您可以附加字符串作为通知对象。
当您通过自定义 class 或您的视图控制器实例化该标签时,将您的通知侦听器添加到 NotificationCenter 并在收到通知后检索附加到通知的对象,仔细检查其string 然后如果是,用它来更新你的标签。
例如,在AppDelegate.swift中:
static let conferenciaNotification = NSNotification.Name(rawValue: "conferenciaNotification")
...
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
let updatedTextFromReceivedPushNotification = "Hello world"
NotificationCenter.default.post(name: AppDelegate.conferenciaNotification, object: updatedTextFromReceivedPushNotification)
}
在带有标签的视图控制器中:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(forName: AppDelegate.conferenciaNotification, object: nil, queue: OperationQueue.main) { (conferenciaNotification) in
if let conferenciaText = conferenciaNotification.object as? String {
myTextLabel.text = conferenciaText
}
}
}
请注意,当您添加观察者时,您可能应该保留对从 NotificationCenter 返回的 NSObjectProtocol 的引用,这样您就可以在视图控制器为 deinit() 时删除它。