gpt4 book ai didi

ios - 如何获取通知以将我带到所需的 viewController?

转载 作者:行者123 更新时间:2023-11-30 12:08:03 27 4
gpt4 key购买 nike

我正在开发一个项目,但很难收到将我带到“NotiViewController”的通知。有时它会按预期工作,但其他时候它只是将我的应用程序打开到我上次使用的任何 viewController。

我将这段代码与一些在线资源结合在一起:

extension QuestionViewController: UNUserNotificationCenterDelegate{
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response:
UNNotificationResponse, withCompletionHandler completionHandler: @escaping () ->
Void) {
let vc = storyboard?.instantiateViewController(withIdentifier: "NotiViewControllerID")
as! NotiViewController
show(vc, sender: Any?.self)
completionHandler()
}

我希望能够发送一条通知,询问用户一个问题,然后一旦单击,用户就会得到答案...

例如通知问题:“美国本土有多少个州?” =>点击通知=>加载适当的NotiViewController:“48”

最佳答案

试试这个代码:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//when application is close

if let userInfo = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [AnyHashable : Any] {
print(userInfo)
self.getBGUserInfo(userInfo: userInfo)
// if notification call to redirect perticular page

}
}
func getBGUserInfo(userInfo : [AnyHashable : Any] ) {
//get userInfo details here
NotificationCenter.default.post(name: Notification.Name(rawValue: "NotificationRedirect"), object: nil)
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
NotificationCenter.default.post(name: Notification.Name(rawValue: "NotificationRedirect"), object: nil)
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
NotificationCenter.default.post(name: Notification.Name(rawValue: "NotificationRedirect"), object: nil)
}

//In ViewController which open first while open app
//In ViewDidLoad
NotificationCenter.default.addObserver(self, selector: #selector(self.receiveTestNotification(_:)), name: NSNotification.Name(rawValue: "NotificationRedirect"), object: nil)
//Over

func receiveTestNotification(_ notification: Notification) {
let notName : String = String(describing: notification.name.rawValue)
if (notName == "NotificationRedirect"){
//Redirect your NotiViewController
}

}

关于ios - 如何获取通知以将我带到所需的 viewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46454936/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com