【问题标题】:how to redirect user to various screen based on remote notification type如何根据远程通知类型将用户重定向到各种屏幕
【发布时间】:2018-07-10 11:10:58
【问题描述】:

我在我的应用程序中收到远程通知。以下代码写在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")
        }
    }

谢谢大家的帮助。

【问题讨论】:

    标签: ios swift uitabbarcontroller appdelegate remote-notifications


    【解决方案1】:

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

    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")
        }
    }
    

    【讨论】:

    • 如何重定向到MoreController 下的控制器?
    • 我有 7 个控制器,其中 4 个在标签栏上可见,其他 3 个位于由 swift 自动生成的 MoreController 下。那么我该如何转移到那些控制器呢? @Shauket
    • 现在其他控制器如何出现在屏幕上?
    • screen 检查此屏幕截图,其中底部有一个 tabController 包含 viewControllers 并且有 MoreController 作为包含其他两个 viewControllers 的最后一个选项卡。 @Shauket
    • 我想去查看MoreControllers下的控制器。
    猜你喜欢
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    • 2018-05-28
    • 2011-04-08
    相关资源
    最近更新 更多