控制这在SWIFT收到推送通知后视图控制器负载 [英] Controlling which view controller loads after receiving a push notification in SWIFT

查看:156
本文介绍了控制这在SWIFT收到推送通知后视图控制器负载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一次,我收到一个推送通知,并刷卡打开它,它只是打开我的应用程序,而不是VC我想要的。

Once I receive a push notification and swipe to open it, it just opens my app and not the VC I want.

所以我的问题是我怎么加载VC我想要什么?我知道,如果应用程序是我开过会移动VC另一个里面的 didReceiveRemoteNotification 但我怎么做,如果应用程序是不开?或者,如果它是在后台模式?

So my question is how do I load the VC I want? I know if the app is open I would move the VC over to another inside the didReceiveRemoteNotification but how do I do it if the app isn't open? or if it is in background mode?

我也有两个不同的推送通知,所以因此我需要它移动的两个不同的VC之一。我怎样才能知道不同的推送notifactions区别?

Also I have TWO different push notifications, so therefore I need it to move ONE of TWO different VCs. How can I tell the difference between different push notifactions?

感谢。

推荐答案

就像有人说的,你要注册到远程通知在applicationDidLaunchWithOptions:

Like it was said, you want to register to remote notifications in applicationDidLaunchWithOptions :

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    var pushTypes = UIUserNotificationType.Badge | UIUserNotificationType.Alert | UIUserNotificationType.Sound
    var pushSettings = UIUserNotificationSettings(forTypes: pushTypes, categories: nil)
    UIApplication.sharedApplication().registerUserNotificationSettings(pushSettings)
    UIApplication.sharedApplication().registerForRemoteNotifications()
}

有没有办法知道其中的viewController当你从锁屏/背景后面你会。我做的是我从的appDelegate发送通知。当您收到remoteNotification,didReceiveRemoteNotification中的appDelegate被调用。

There is no way to know in which viewController you will be when you come back from the lockScreen/Background. What I do is I send a notification from the appDelegate. When you receive a remoteNotification, didReceiveRemoteNotification in the appDelegate is called.

 func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    let notif = JSON(userInfo) // SwiftyJSON required 

根据你的通知包含,你应该首先确保它不是零,然后调用将由应该抓住这一通知viewControllers被钓到了一条通知。看起来是这样的,只是把它作为一个例子:

Depending on what your notification contains, you should first make sure it is not nil and then call a notification that will be catched by the viewControllers that should catch this notification. Could look like this, just take it as an example :

if notif["callback"]["type"] != nil{
    NSNotificationCenter.defaultCenter().postNotificationName("myNotif", object: nil, userInfo: userInfo as [NSObject : AnyObject])
    // This is where you read your JSON to know what kind of notification you received, for example :    

}

例如,如果收到短信通知,你是不是在了,因为令牌已经到期,那么通知将永远不会在视图控制器逮住登录,因为它永远不会被关注。

For example, if you receive a message notification and you are not logged in anymore because the token has expired, then the notification will never be catched in the view controller, because it will never be watched.

现在的你在哪里赶上视图控制器通知的一部分。在viewWillAppear中:

Now for the part where you catch the notification in the view controller. In the viewWillAppear :

 override func viewWillAppear(animated: Bool) {
     NSNotificationCenter.defaultCenter().addObserver(self, selector: "catchIt:", name: "myNotif", object: nil)

现在,您添加了此观测,每一个通知被称为该控制器时间,函数catchIt也将被调用。您必须实现它在每一个视图控制器要实现一个具体行动。

Now that you added this observer, each time a notification is called in this controller, the function catchIt will also be called. You will have to implement it in every view controller you want to implement a specific action.

func catchIt(userInfo: NSNotification){
    var not = JSON(userInfo.valueForKey("userInfo")!)
    // Check nil and do redirect here, for example:
    if not["callback"]["type"].int == 10{
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        var vc1: example1VC = storyboard.instantiateViewControllerWithIdentifier("example1") as! example1VC
        self.navigationController?.pushViewController(vc, animated: true)
    }
    else if not["callback"]["type"].int == 20{
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        var vc2: example2VC = storyboard.instantiateViewControllerWithIdentifier("example2") as! example2VC
        self.navigationController?.pushViewController(vc, animated: true)
    }

离开视图控制器的时候,否则的viewController,如果仍然在堆栈中,将捕获的通知和执行它(当然,你可能想的不要忘记从通知退订,但它的安全知道你正在进入。因此,我建议在viewWillDisappear退订。

Don't forget to unsubscribe from the notifications when leaving the view controller, else the viewController, if still in the stack, will catch the notification and execute it (well you might want to that, but it's safer to know what you are going into. So I suggest unsubscribing in the viewWillDisappear.

 override func viewWillDisappear(animated: Bool) {
    NSNotificationCenter.defaultCenter().removeObserver(self)

这样做,这样,你会加载所需的的viewController。现在,我们还没有处理所有的情况呢。如果你没有打开你的应用程序呢。显然,没有任何的UIViewController已经加载,和他们没有将能够赶上通知。你想,如果你收到didFinishLaunchingWithOptions通知知道:在的appDelegate。我做的是:

Doing it this way, you will load the viewController you want. Now we haven't treated all the cases yet. What if you haven't opened your application yet. Obviously no UIViewController has been loaded, and none of them will be able to catch the notification. You want to know if you received a notification in didFinishLaunchingWithOptions: in the appDelegate. What I do is:

var prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()
if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {
    prefs.setObject(remoteNotification as [NSObject : AnyObject], forKey: "startUpNotif")
    prefs.synchronize()
}

现在,你已经设置了preference说该应用程序是使用远程通知启动。在应在应用程序第一次加载的控制器,我建议做了viewDidAppear以下内容:

Now, you have set a preference saying the application was started using a remote notification. In the controllers that should be loaded first in your application, I suggest doing the following in the viewDidAppear:

override func viewDidAppear(animated: Bool) {
    var prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()
    if prefs.valueForKey("startUpNotif") != nil {
        let userInfo: [NSObject : AnyObject] =     prefs.valueForKey("startUpNotif") as! [NSObject : AnyObject]
        prefs.removeObjectForKey("startUpNotif")
        prefs.synchronize()
        NSNotificationCenter.defaultCenter().postNotificationName("catchIt", object: nil, userInfo: userInfo as [NSObject : AnyObject])
    }

希望它帮助。

这篇关于控制这在SWIFT收到推送通知后视图控制器负载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆