在 AppDelegate 中单击 FCM 推送通知时导航到特定选项卡 iOS - Swift
Navigating to specific tab when clicking on FCM push notification in AppDelegate iOS - Swift
为了让它发挥作用,我绞尽脑汁想了一天。我收到了通知,但在单击时从未导航到特定选项卡。
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()
}
对于 iOS 14.4 使用 Swift。
在其他问题的帮助下,这最终对我有用。
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 推送通知的更多帮助,请告诉我。祝大家编程愉快!
对于特定的导航ViewController我使用这个扩展
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()
}
为了让它发挥作用,我绞尽脑汁想了一天。我收到了通知,但在单击时从未导航到特定选项卡。
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()
}
对于 iOS 14.4 使用 Swift。 在其他问题的帮助下,这最终对我有用。
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 推送通知的更多帮助,请告诉我。祝大家编程愉快!
对于特定的导航ViewController我使用这个扩展
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()
}