0

我在我的应用程序中收到远程通知。以下代码写在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 Then 在用户单击通知时启动任务屏幕。

我有一个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")
        }
    }

谢谢你们的帮助。

4

1 回答 1

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")
    }
}
于 2018-07-10T11:23:42.763 回答