推送通知并检索其数据时如何在 ViewController 上执行操作

Posted

技术标签:

【中文标题】推送通知并检索其数据时如何在 ViewController 上执行操作【英文标题】:How to perform an action on a ViewController when pushing a notification and retrieve its data 【发布时间】:2018-08-10 18:15:59 【问题描述】:

我想知道当通知到达并且用户点击它时如何更改 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 更改其值时执行操作。

我希望我能很好地解释自己,否则请问我任何问题。非常感谢

【问题讨论】:

【参考方案1】:

NotificationCenter 可能是您最简单的解决方案。定义一个自定义字符串以用作 NotificationCenter 通知的通用名称,该通知将用于将信息从 AppDelegate 传递给正在收听的任何人。您可以将字符串附加为通知对象。

当您通过自定义类或视图控制器实例化该标签时,将通知侦听器添加到 NotificationCenter 并在收到通知后检索附加到通知的对象,仔细检查其字符串,如果是,请使用它来更新你的标签。

例如,在 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() 时将其删除。

【讨论】:

您好,非常感谢您的回复!我现在只是有一个问题。当我的应用程序在后台或前台时,我可以获取数据。但是当我关闭它并通过点击通知打开它时,数据不会从 appdelegate 发送到 ViewController。我想这是因为在应用启动之前不会创建“conferenciaNotificacion”。 查看applicationDidFinishLaunching传入的参数;如果您需要更多帮助,请打开另一个问题 :) 你好,我发布了一个新问题。如果您能帮助我,我将不胜感激。提前非常感谢你:)

以上是关于推送通知并检索其数据时如何在 ViewController 上执行操作的主要内容,如果未能解决你的问题,请参考以下文章

如何检索推送通知php的ck.pem文件

服务器端 Apple 推送通知

如何在 iOS 中根据位置设置推送通知?

如何向 ios 应用程序发送 Firebase 推送通知

worklight中的推送通知,如何检索userId?

如何使用 angular.js 推送通知?