How to perform an action on a ViewController when pushing a notification and retrieve its data

Clash Royale CLAN TAG#URR8PPP
How to perform an action on a ViewController when pushing a notification and retrieve its data
I would like to know how to change the value of a UITextField on a ViewController whenever a notification arrives and the user taps on it. The notification contains the String that I will be putting on that UITextField.
This is how my app looks
I can currently retrieve the notification data on AppDelegate and decide which tab must be selected when the user taps on the notification. This is how I do it:
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()
What I would like to do know is to pass the data from the notification to that specific Tab bar ViewController and change the value of the UITextField based on that data and then perform an action when that TextField changes its value.
I hope I explained myself well, otherwise please ask me whatever questions you have. Thank you so much
1 Answer
1
The NotificationCenter is potentially your simplest solution. Define a custom string to use as a common name for a a NotificationCenter notification that will be used to pass the information from the AppDelegate to whoever is listening. You can attach the string as the notification object.
When you instantiate that label, either via a custom class or from your view controller, add your notification listener to the NotificationCenter and upon receiving the notification retrieve the object attached to the notification, double check its a string then if so, use it to update your label.
For example, in 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)
In your view controller with the label:
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
Please note you probably should keep a reference to the NSObjectProtocol returned from the NotificationCenter when you add the observer, so you can remove it when your view controller is deinit().
look into the parameters passed in applicationDidFinishLaunching; open another question if you need more help :)
– ekscrypto
Aug 12 at 22:30
Hello again, I posted a new question. I would appreciate if you could help me. Thank you so much in advance :)
– Carmen Gordillo
Aug 15 at 5:18
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Hey there, thank you so much for your response! I just have an issue now. I can get the data when my app is in the background or foreground. But when I close it and I open it through tapping onto the notification, the data is not sent from appdelegate to ViewController. I imagine that's because "conferenciaNotificacion" is not being created until the app is launched.
– Carmen Gordillo
Aug 12 at 22:05