2

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?

mkz
  • 2,302
  • 2
  • 30
  • 43
  • it is enough to notify the root navigation- or tab bar- or split view controller... and how? maybe post a notification to them...? – holex Oct 23 '15 at 13:24
  • If you want to add a custom View then you can simply add it to Window like this [[UIApplication sharedApplication].keyWindow addSubview:overlayView]; no Need to Inform any other View Controller – Muhammad Waqas Bhati Oct 23 '15 at 13:25
  • 1
    Possible duplicate of [Passing data received from a push notification from the AppDelegate to a ViewController](http://stackoverflow.com/questions/22300144/passing-data-received-from-a-push-notification-from-the-appdelegate-to-a-viewcon) – tilo Oct 23 '15 at 13:31

2 Answers2

4

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
    }
}
Swinny89
  • 7,273
  • 3
  • 32
  • 52
1

You can use NSNotificationCenter to notify your viewcotroller from the didReceiveRemoteNotification. Then you can show custom view as you want with the userinfo dictionary passed in that NSNotificationCenter method .

Rohan
  • 2,939
  • 5
  • 36
  • 65