如何根据远程通知类型将用户重定向到各种屏幕



我在应用程序中收到远程通知。以下代码写在AppDelegate文件中,当我收到通知时调用该文件。

当应用程序在后台时获得通知

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
print("Notification Recieved")
UIApplication.shared.applicationIconBadgeNumber += 1
//process notification
for (key,val) in userInfo{
let nkey = key as! String
if nkey == "notificationType"{
let notificationType = val as! String
handleNotificationScreen(type: notificationType)
}
}
}

当应用程序在后台,用户点击通知时,调用上述函数。函数userInfo是一个包含通知信息的有效负载。在userInfo中,我从服务器传递notificationType,其中包含各种值,如TASKKRA

现在,基于这些notificationType值,我想启动不同的屏幕。如果notificationType为TASK,则在用户单击通知时启动Task Screen。

我有一个TabBarController,它有多个viewControllers

viewControllers = [taskController,messageController,notificationConroller,userProfileNavigationController,empCorner,perfomranceVC]

现在我该怎么做才能在handleNotificationScreen(type: notificationType)函数中启动屏幕。

func handleNotificationScreen(type: String){
switch type {
case "KRA":
print("anc")
case "TASK":
print("task")
case "EMPMONTH":
print("empMonth")
default:
print("none")
}
}

谢谢你们的帮助。

获取您的TabBarControllerhandleNotificationScreen内部的引用,如果您想在Appdelegate内部获取TabBarControllers,请像这样声明可选属性。

var tabbarController: UITabBarController?

didFinishLaunchingWithOptions函数内部调用这个

tabbarController=self.window?。rootViewController作为?UITabBarController

现在可以使用选项卡控制器在所需函数中调用特定的控制器。

func handleNotificationScreen(type: String){
switch type {
case "KRA":
print("anc")
case "TASK":
print("task")
tabBarController?.selectedIndex = 0 //it will open taskbarcontroler
case "EMPMONTH":
print("empMonth")
default:
print("none")
}
}

最新更新