在SWIFT中接收推送通知后控制哪个视图控制器加载

问题描述 投票:29回答:6

一旦我收到推送通知并轻扫以打开它,它就会打开我的应用而不是我想要的VC。

所以我的问题是如何加载我想要的VC?我知道如果应用程序是开放的,我会将VC移到didReceiveRemoteNotification内的另一个,但如果应用程序未打开,我该怎么做?或者它是否处于后台模式?

此外,我有两个不同的推送通知,因此我需要它来移动两个不同的VC中的一个。如何区分不同的推送通知?

谢谢。

push-notification apple-push-notifications appdelegate
6个回答
46
投票

针对Swift 4.2进行了更新

就像它说的那样,你想在applicationDidLaunchWithOptions中注册远程通知:

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    let pushSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
    UIApplication.shared.registerUserNotificationSettings(pushSettings)
    UIApplication.shared.registerForRemoteNotifications()
}

当你从lockScreen / Background回来时,无法知道你将在哪个viewController中。我所做的是从appDelegate发送通知。当您收到remoteNotification时,将调用appDelegate中的didReceiveRemoteNotification。

 func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
    let notif = JSON(userInfo) // SwiftyJSON required 

根据您的通知所包含的内容,您应首先确保它不是nil然后调用将捕获此通知的viewControllers捕获的通知。可能看起来像这样,只是把它作为一个例子:

if notif["callback"]["type"] != nil{
    NotificationCenter.default.post(name: Notification.Name(rawValue: "myNotif"), object: nil)
    // This is where you read your JSON to know what kind of notification you received, for example :    

}

例如,如果您收到消息通知但由于令牌已过期而您尚未登录,则通知将永远不会在视图控制器中捕获,因为它永远不会被监视。

现在,您可以在视图控制器中捕获通知。在viewWillAppear中:

 override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    NotificationCenter.default.addObserver(self, selector: #selector(self.catchIt), name: NSNotification.Name(rawValue: "myNotif"), object: nil)
}

现在你添加了这个观察者,每次在这个控制器中调用一个通知时,也会调用函数catchIt。您必须在要实现特定操作的每个视图控制器中实现它。

func catchIt(_ userInfo: Notification){

    let prefs: UserDefaults = UserDefaults.standard
    prefs.removeObject(forKey: "startUpNotif")

    if userInfo.userInfo?["userInfo"] != nil{
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc: RedirectAppInactiveVC = storyboard.instantiateViewController(withIdentifier: "RedirectAppInactiveVC") as! RedirectAppInactiveVC
        self.navigationController?.pushViewController(vc, animated: true)
    } else {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc: RedirectAppActiveVC = storyboard.instantiateViewController(withIdentifier: "RedirectAppActiveVC") as! RedirectAppActiveVC
        self.navigationController?.pushViewController(vc, animated: true)
    }
}

离开视图控制器时不要忘记取消订阅通知,否则viewController(如果仍在堆栈中)将捕获通知并执行它(你可能想要这样做,但知道你要进入什么更安全)。所以我建议在viewWillDisappear中取消订阅:

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillAppear(animated)
    NotificationCenter.default.removeObserver(self)
}

这样做,您将加载所需的viewController。现在我们尚未处理所有案件。如果您尚未打开申请,该怎么办?显然,没有加载UIViewController,并且它们都不能捕获通知。您想知道您是否在appDelegate中的didFinishLaunchingWithOptions中收到了通知。我所做的是:

let prefs: UserDefaults = UserDefaults.standard
if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? NSDictionary {
    prefs.set(remoteNotification as! [AnyHashable: Any], forKey: "startUpNotif")
    prefs.synchronize()
}

现在,您已设置首选项,说明应用程序是使用远程通知启动的。在应该首先在应用程序中加载的控制器中,我建议在viewDidAppear中执行以下操作:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    let prefs: UserDefaults = UserDefaults.standard
    if prefs.value(forKey: "startUpNotif") != nil {
        let userInfo: [AnyHashable: Any] = ["inactive": "inactive"]
        NotificationCenter.default.post(name: Notification.Name(rawValue: "myNotif"), object: nil, userInfo: userInfo as [AnyHashable: Any])
    }
}

希望能帮助到你。我还创建了一个GitHub存储库来说明本地通知:Local Notifications Observer Pattern(类似于远程通知)。可以使用根视图Controller Local Notifications Root Pattern实现类似的逻辑,我个人认为它将取决于您要实现的内容。

这些例子用于说明如何简单实施。对于更大的项目,您将最终得到更复杂的架构,例如内部使用类似机制的协调器。


7
投票

除了@ NickCatib的回答之外,要了解在应用程序运行时是否收到通知,如果是,在前台或后台,您需要在AppDelegate中使用此方法:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {


// You can determine your application state by
if UIApplication.sharedApplication().applicationState == UIApplicationState.Active {

// Do something you want when the app is active

} else {

// Do something else when your app is in the background


}
}

5
投票

当您运行应用程序时,您正在调用应用程序Did LaunchWithOptions:

UIApplication.sharedApplication().registerUserNotificationSettings ( UIUserNotificationSettings(forTypes: (UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound), categories: nil))



if( launchOptions != nil){
    var notificationDict: AnyObject? = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey]
    if(notificationDict != nil){
        handleNotification(notificationDict! as! [NSObject : AnyObject])
    }

}

在这里,你有handleNotification,它基本上是我的自定义函数,我从通知中提取数据并使用该信息来显示相应的控制器。

这是一个例子:

let notificationType = userInfo["aps"]!["alert"]!!["some-key-I-Need"]! as! String
var storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController = storyboard.instantiateInitialViewController() as! MyViewController
self.window?.rootViewController  = mainViewController

2
投票

我发现上面的所有答案都非常有用。然而,当应用程序停用时,投票最多的人对我没有用。后来尝试实现@NickCatib和@thefredelement组合的答案,他们在执行storyboard.instantiateInitialViewController()时生成错误 - “无法转换类型'UINavigationController'的值”。我发现这是因为我有一个带有NavController的storyboard文件作为rootViewController。为了解决这个问题,我创建了一个新的导航控制器,它解决了一个问题:我丢失了应用程序的正确导航,并且视图甚至没有显示后退按钮。我的问题的答案是使用@NickCatib和@thefredelement答案,但是使用标识符实例化视图并使用rootViewController作为UINavigationController推送它,如下所示。

let rootViewController = self.window?.rootViewController as! UINavigationController
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mvc = storyboard.instantiateViewControllerWithIdentifier("MyViewController") as! 
             MyViewController
rootViewController.pushViewController(mvc, animated: true)

这对我来说很好,我没有丢失应用程序的正确导航属性。


0
投票

以前答案的补充信息。

根据您的状态,您可以更改逻辑。在方法didReceiveRemoteNotification中:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {}

你可以做这样的开关

let state = UIApplication.sharedApplication().applicationState switch state { case UIApplicationState.Active: case UIApplicationState.Inactive: case UIApplicationState.Background: }

根据应用程序所处的当前状态执行所需的操作。


0
投票

Swift Rabbit的答案是最好的。

我想补充一点,当应用程序从关闭到活动状态时仍然缺少。

您可以在didFinishedLaunchingWithOptions中添加AppDelegate:

if let notification = launchOptions?[.remoteNotification] as? [AnyHashable : Any] {

            notificationsUserInfo = notification as [AnyHashable : Any]
            serveNotifications = true

        }

您可以使用通知的值创建全局变量或userdefault,并使用标志让应用程序的其余部分发出通知。

一旦mainViewController可见,您就可以执行操作来处理通知。

override func viewDidAppear(_ animated: Bool) {
        if serveNotifications {
      notificationManager.sharedInstance.processNotification(userInfo: notificationsUserInfo)

            serveNotifications = false

        }

    }

照顾自己。

© www.soinside.com 2019 - 2024. All rights reserved.