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



一天来我一直在挠头,试图让它发挥作用。我收到了通知,但单击时从未导航到特定的选项卡。


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推送通知的更多帮助,请告诉我。祝大家编码愉快!

对于导航到特定视图控制器,我使用此扩展

public extension UIApplication {
class func getTopViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {

if let nav = base as? UINavigationController {
return getTopViewController(base: nav.visibleViewController)

} else if let tab = base as? UITabBarController, let selected = tab.selectedViewController {
return getTopViewController(base: selected)

} else if let presented = base?.presentedViewController {
return getTopViewController(base: presented)
}
return base
}
}

然后称之为如下:

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 storyboard = UIStoryboard(name: "yourStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "yourStoryboardId") as! YourViewcontroller
//                   vc.nNotificationID = notificationID

if let topVC = UIApplication.getTopViewController() {
topVC.navigationController?.pushViewController(vc, animated: false)
}

print("Clicked Notification")
completionHandler()
}

相关内容

  • 没有找到相关文章

最新更新