iosswiftintercomintercom-ios

catch notification and do something


I'm working with Intercom's iOS SDK and reading through the Header available I found:

//=========================================================================================================
/*! @name Intercom Notifications */
//=========================================================================================================

/*!
These are notifications thrown by Intercom for iOS when the Intercom window is displayed and hidden or when
a new conversation has been started. These notifications are fired only when there is a change in the state
of Intercom's UI: when a user receives a message for instance, willShow and didShow notifications will be 
fired accordingly when the Intercom Notification (chat head) is presented. 

Once the user taps on the chat head, the message is presented in your app. It will be presented covering
the entire screen, but no notifications will be thrown here as Intercom has already been visible.

In the case of a new conversation this notification may be used to prompt users to enable push notifications.
*/

let IntercomWindowWillShowNotification: String
let IntercomWindowDidShowNotification: String
let IntercomWindowWillHideNotification: String
let IntercomWindowDidHideNotification: String
let IntercomDidStartNewConversationNotification: String

How can I got about catching these notifications in a controller and then do something based on the specific notification that is thrown?


Solution

  • You will need to add your ViewControllerClass as an observer to each notification it should observe in viewDidLoad...

    class ITC: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("icWindowWillShowNotification:"), name: IntercomWindowWillShowNotification, object: nil)
        }
    
        deinit {
            NSNotificationCenter.defaultCenter().removeObserver(self)
        }
    
        func icWindowWillShowNotification (notification: NSNotification) {
            //Do something
        }
    
    }