单击通知时更改选项卡栏

Change Tab Bar When Notification is Clicked

每当用户收到通知并单击它时,他们都应该被带到该特定选项卡。例如,当用户收到消息并打开通知时,应用程序应该打开,但不会打开消息选项卡。想想推特和 dm 的。 在我的项目中,我试图实现非常相似的东西。在 appdelegate 和 notificationReceived(用于推送通知的 OneSignal)中,我尝试这样做:

 let sb: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let messageNavigationController: UINavigationController = sb.instantiateViewController(withIdentifier: "MessagesNav") as! UINavigationController
            self.window = UIWindow(frame: UIScreen.main.bounds)
            self.window?.rootViewController = messageNavigationController
            self.window?.makeKeyAndVisible()

但是没有任何反应。每当用户收到通知时,应用程序只会打开一个主选项卡(选项卡 0)。我做错了什么?

我不知道你所说的 notificationReceived 是什么意思,那不是 UIApplicationDelegate 的预定义函数。如果你想在 OneSignal 发生推送通知时打开不同的页面,你应该将你的处理函数传递给 OneSignal.initWithLaunchOptionshandleNotificationAction

您还提到在选项卡控制器中更改选项卡,但您粘贴的代码将根视图设置为 UINavigationController。如果您真的想更改选项卡,您需要获得对选项卡控制器的引用并在其上设置正确的 selectedIndex