我在我的应用程序中收到远程通知。以下代码写在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
,其中包含各种值,例如TASK
,KRA
等
现在基于这些notificationType
值,我想启动不同的屏幕。
如果notificationType为TASK
,则当用户单击通知时启动任务屏幕。
我有一个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")
}
}
谢谢大家的帮助。
答案 0 :(得分:0)
获取TabBarController
和handleNotificationScreen
内部的引用,如果您想在TabBarControllers
中获取Appdelegate
,则声明这样的可选属性。
var tabbarController: UITabBarController?
内部didFinishLaunchingWithOptions
函数调用此
tabbarController = self.window?.rootViewController as? UITabBarController
现在可以使用 tabbarController 在所需的函数中调用您的特定控制器。
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")
}
}