【问题标题】:Execute function on tap Push Notifications点击执行功能推送通知
【发布时间】:2018-03-08 11:41:19
【问题描述】:

我有一个 iOS APP 和一个推送通知系统,它以编程方式发送通知以将其令牌存储在数据库中。

我有一些视图在默认视图控制器中显示或隐藏。我想让 APP 在用户点击通知时显示特定视图。我创建了一个函数,但我不知道如何使它工作。

【问题讨论】:

  • 在推送通知负载中,您可以发送额外的自定义数据,您需要相应地发送自定义数据并根据该自定义数据中接收到的数据处理视图(显示/隐藏)
  • 我建议在 UIViewController 扩展中设置一个 notificationCenter 观察者,并让它从接收通知的 appDelegate 函数中侦听,并作为 @AshwinShrestha 的提及使用您发送的数据作为视图控制器标识符

标签: ios swift firebase push-notification


【解决方案1】:

您可以在AppDelegate 中捕获推送通知,并在您希望在点击时显示特定视图的控制器文件中添加观察者。

userInfoobject 中发送推送通知负载数据NotificationCenter.default.post

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    UIApplication.shared.applicationIconBadgeNumber = 0
    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "NewNotification") , object: nil, userInfo: response.notification.request.content.userInfo)
}

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    completionHandler(.alert)
}

在你的控制器文件中添加通知观察者:

NotificationCenter.default.addObserver(self, selector: #selector(pushNotificationHandler(_:)) , name: NSNotification.Name(rawValue: "NewNotification"), object: nil)

然后调用 UI 更新方法:

 func pushNotificationHandler(_ notification : NSNotification) {
   // self.udateUI() // Add code to show a specific view on tap.
}

【讨论】:

    【解决方案2】:

    在 AppDelegate APNS 委托方法中,根据您通过通知/有效负载获得的 userInfo 进行检查,然后调用该函数

     func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    
     if response.notification.request.content.userInfo["ViewType"] as! String == "ShowNotificationView" {
     // Then you can implement your functionaliton here according to the need. 
        }
    

    如果您需要任何其他帮助,请通过发布实际代码和要求告诉我

    【讨论】:

    • 您好,我已经用 Sid Mhatre 分享的解决方案解决了这个问题。无论如何,谢谢!
    【解决方案3】:

    首先,需要根据您需要导航或显示特定视图控制器(屏幕)的屏幕内容,向推送通知有效负载添加一个键为screen_type

    之后,将以下方法添加到实现通知处理程序的 AppDelegate 类中

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
        let payload = userInfo
        let type = payload["type"]
        if type == "all-posts" || type == "default"{
            /*
             * I'm assuming you have your own HomeViewController object
             */
            let homeVC = HomeViewController()
            // present or push
            UIApplication.topViewController()?.present(homeVC, animated: true, completion: nil)
        }else if type = "post-details"{
            /*
             * I'm assuming you have your own PostDetailViewController object
             */
            let postDetailVC = PostDetailViewController()
            // present or push
            UIApplication.topViewController()?.present(postDetailVC, animated: true, completion: nil)
        }else if type == "friend-request"{
            /*
             * I'm assuming you have your own UserViewController object
             */
            let userVC = UserViewController()
            // present or push
            UIApplication.topViewController()?.present(userVC, animated: true, completion: nil)
        }
    }
    

    辅助方法

    extension UIApplication {
        class func topViewController(base: UIViewController? = (UIApplication.sharedApplication().delegate as! AppDelegate).window?.rootViewController) -> UIViewController? {
            if let nav = base as? UINavigationController {
                return topViewController(base: nav.visibleViewController)
            }
            if let tab = base as? UITabBarController {
                if let selected = tab.selectedViewController {
                    return topViewController(base: selected)
                }
            }
            if let presented = base?.presentedViewController {
                return topViewController(base: presented)
            }
            return base
        }
    }
    

    注意:如果您使用的是最新版本的 swift,则需要实现 UNUserNotificationCenterDelegate 方法

    【讨论】:

    • 您好,我已经用 Sid Mhatre 分享的解决方案解决了这个问题。无论如何,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-23
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多