gpt4 book ai didi

swift - 在 AppDelegate iOS - Swift 中单击 FCM 推送通知时导航到特定选项卡

转载 作者:行者123 更新时间:2023-12-02 02:17:59 25 4
gpt4 key购买 nike

为了让它发挥作用,我绞尽脑汁想了一天。我收到了通知,但在单击时从未导航到特定选项卡。


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(tag + "Message ID: \(messageID)")
}

// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)
// Print full message.
print(userInfo)

// let myTabBar = self.window?.rootViewController as? UITabBarController
// myTabBar?.selectedIndex = 2

//TabBarController - StoryBoard Id
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let tabController = storyboard.instantiateViewController(withIdentifier: "TabBarController") as? UITabBarController {
tabController.selectedIndex = 2
}

completionHandler()
}

最佳答案

适用于使用 Swift 的 iOS 14.4。在其他问题的帮助下,这最终对我有用。


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(tag + "Message ID: \(messageID)")
}

// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)
// Print full message.
print(userInfo)

let scene = UIApplication.shared.connectedScenes.first
if let sceneDelegate = scene?.delegate as? SceneDelegate {
if let tabController = sceneDelegate.window?.rootViewController as? UITabBarController {
tabController.selectedIndex = 2
}

}

print("Clicked Notification")

completionHandler()
}

如果您需要有关 FCM 推送通知的更多帮助,请告诉我。祝大家编码愉快!

关于swift - 在 AppDelegate iOS - Swift 中单击 FCM 推送通知时导航到特定选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66897118/

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