如何根据推送通知的类型打开 Viewcontroller?
How to open a Viewcontroller depending on the type of Push Notification?
我在我的应用程序中启用了推送通知,并且有 4 种类型的推送通知(类型 1、2、3、4)。如何让我的应用程序根据推送通知的类型打开不同的 ViewControllers?
我尝试搜索 Whosebug,发现了几个关于从推送通知打开 VC 的线程,但不幸的是,我找不到任何关于打开 VC 的线程,具体取决于推送通知的类型。
我是推送通知设置的新手,我对此一无所知,如有任何帮助,我们将不胜感激。 (这就是我无法包含任何代码的原因)
谢谢
编辑:通知类型为 Int 1,2,3,4。
您只需要找到顶部视图控制器并根据您收到的推送通知类型推送相应的 vc。
在Appdelegate.swift
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
handlePushNotification(userInfo as NSDictionary)
}
func handlePushNotification(_ userInfo: NSDictionary) {
if
let apsInfo = userInfo["aps"] as? NSDictionary,
let vcType = apsInfo["type"] as? String,
{
// TODO: Here you do string matching to find which VC you want to push
// Else you can send a local notification and read it where ever necessary
}
}
总是很乐意提供一些示例代码供我们使用。例如,我们不知道通知 "type" 是什么——它是 int 还是 string 等?另外,这种类型来自哪里?它在推送通知的用户信息中吗?
也就是说,根据您的描述,假设通知 "type" 只是一个整数,并且您已经从通知中获得了类型,您可以使用枚举来使您的类型更具可读性,并启用 switch 语句。
enum NotificationType: Int {
case firstType
case secondType
case thirdType
}
func showViewController(withNotificationType type: Int) {
guard let notificationType = NotificationType(rawValue: type) else {
print("Unrecognised notification type")
return
}
let viewController: UIViewController
switch notificationType {
case .firstType:
viewController = FirstViewController()
case .secondType:
viewController = SecondViewController()
case .thirdType:
viewController = ThirdViewController()
}
navigationController.pushViewController(viewController, animated: true)
}
如果您使用的 TabBarController 在导航控制器中嵌入了 childViewController,那么这里有一个有用的示例。
/// Respond to user notification taps
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
/// Get notification Info which can help you determine what VC to present.
let userInfo = response.notification.request.content.userInfo
// Based on userInfo add your logics
// --------------
// Now that you know which ViewController to present/push proceed as follows
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
/// Assumming your viewControllers are in tabbar controller named "CustomTabBarcontroller" you have to change this to your tabbar's name.
if let mainTabBarController = self.window?.rootViewController as? CustomTabBarController {
/// Select your desired ViewController, it can be at any position, I used index 2 as an example for my VC named ActivityViewController
mainTabBarController.selectedIndex = 2
/// Dismis any presented previous ViewController before we present a user profile.
mainTabBarController.presentedViewController?.dismiss(animated: true, completion: nil)
/// Assume that you want to push to a viewController called ActivityViewController which is inside a navigationController located at index 2 of your tabbarController.
if let activityNavigationController = mainTabBarController.viewControllers?[2] as? UINavigationController {
/// Now you can present or push to any detail view from your activityNavVC as you would do it in a normal way. Let's say you want to push to detail vc named "YourDetailVC", Note if you want to present you don't need to access the navigationController.
activityNavigationController.pushViewController(YourDetailVC, animated: true)
}
}
}
}
我在我的应用程序中启用了推送通知,并且有 4 种类型的推送通知(类型 1、2、3、4)。如何让我的应用程序根据推送通知的类型打开不同的 ViewControllers?
我尝试搜索 Whosebug,发现了几个关于从推送通知打开 VC 的线程,但不幸的是,我找不到任何关于打开 VC 的线程,具体取决于推送通知的类型。
我是推送通知设置的新手,我对此一无所知,如有任何帮助,我们将不胜感激。 (这就是我无法包含任何代码的原因)
谢谢
编辑:通知类型为 Int 1,2,3,4。
您只需要找到顶部视图控制器并根据您收到的推送通知类型推送相应的 vc。
在Appdelegate.swift
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
handlePushNotification(userInfo as NSDictionary)
}
func handlePushNotification(_ userInfo: NSDictionary) {
if
let apsInfo = userInfo["aps"] as? NSDictionary,
let vcType = apsInfo["type"] as? String,
{
// TODO: Here you do string matching to find which VC you want to push
// Else you can send a local notification and read it where ever necessary
}
}
总是很乐意提供一些示例代码供我们使用。例如,我们不知道通知 "type" 是什么——它是 int 还是 string 等?另外,这种类型来自哪里?它在推送通知的用户信息中吗?
也就是说,根据您的描述,假设通知 "type" 只是一个整数,并且您已经从通知中获得了类型,您可以使用枚举来使您的类型更具可读性,并启用 switch 语句。
enum NotificationType: Int {
case firstType
case secondType
case thirdType
}
func showViewController(withNotificationType type: Int) {
guard let notificationType = NotificationType(rawValue: type) else {
print("Unrecognised notification type")
return
}
let viewController: UIViewController
switch notificationType {
case .firstType:
viewController = FirstViewController()
case .secondType:
viewController = SecondViewController()
case .thirdType:
viewController = ThirdViewController()
}
navigationController.pushViewController(viewController, animated: true)
}
如果您使用的 TabBarController 在导航控制器中嵌入了 childViewController,那么这里有一个有用的示例。
/// Respond to user notification taps
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
/// Get notification Info which can help you determine what VC to present.
let userInfo = response.notification.request.content.userInfo
// Based on userInfo add your logics
// --------------
// Now that you know which ViewController to present/push proceed as follows
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
/// Assumming your viewControllers are in tabbar controller named "CustomTabBarcontroller" you have to change this to your tabbar's name.
if let mainTabBarController = self.window?.rootViewController as? CustomTabBarController {
/// Select your desired ViewController, it can be at any position, I used index 2 as an example for my VC named ActivityViewController
mainTabBarController.selectedIndex = 2
/// Dismis any presented previous ViewController before we present a user profile.
mainTabBarController.presentedViewController?.dismiss(animated: true, completion: nil)
/// Assume that you want to push to a viewController called ActivityViewController which is inside a navigationController located at index 2 of your tabbarController.
if let activityNavigationController = mainTabBarController.viewControllers?[2] as? UINavigationController {
/// Now you can present or push to any detail view from your activityNavVC as you would do it in a normal way. Let's say you want to push to detail vc named "YourDetailVC", Note if you want to present you don't need to access the navigationController.
activityNavigationController.pushViewController(YourDetailVC, animated: true)
}
}
}
}