Search code examples
iosswiftpush-notificationforeground

push notification in foreground


I have an application with ability to receive Push Notifications.

When the application is in background mode push notification shows well, but if the application is in foreground mode I want to show my custom view with information from userInfo.

How can I notify any viewController from my appDelegate didReceiveRemoteNotification to show this custom view and send userInfo dictionary there?

Can someone help me with my problem?


Solution

  • You could send out a notification and then the viewControllers that you want to be alerted can have observers for them:

    NSNotificationCenter.defaultCenter().postNotificationName("nameOfNotification", object: yourVariableContainingUserInfo)
    

    Can you add observers like so:

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "nameOfNotification:", name: "nameOfNotification", object: nil)
    

    And your selector would look something like this:

    func nameOfNotification(notification: NSNotification) {
        if let dict = notification.object as? NSDictionary {
            //user your userInfo dictionary here
        }
    }