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

时间:2018-07-10 11:10:58

标签: ios swift uitabbarcontroller appdelegate remote-notifications

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

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

谢谢大家的帮助。

1 个答案:

答案 0 :(得分:0)

获取TabBarControllerhandleNotificationScreen内部的引用,如果您想在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")
    }
}