Integration: V1

The following information will show you how to integrate the Vibes Push Notifications SDK into an iOS app. The iOS Example App is also available to show you how to implement the SDK.

App Configuration

Add the following to your application:didFinishLaunchingWithOptions in your AppDelegate.swift file:

Vibes.configure(appid: "MY_APP_ID")

Registering a Device

Add the following lines to application:didFinishLaunchingWithOptions, or wherever it makes the most sense for your application:

Vibes.shared.registerDevice { result in
    // This callback is optional. If you choose to use it, result will be a
    // VibesResult<Credential>.
}

Unregistering a Device

Use the following code to unregister a device. You can add the following code wherever it makes the most sense for your application:

Vibes.shared.unregisterDevice { result in
    // This callback is optional. If you choose to use it, result will be a
    // VibesResult<Void>.
}

Push Messaging

Registering for Push

  1. Register for remote notifications by following the OS Local and Remote Notification Programming guide.
  2. Add the following to application:didRegisterForRemoteNotificationsWithDeviceToken in your AppDelegate:
    Vibes.shared.setPushToken(fromData: deviceToken)
    
  3. When you would like to register for push, you should call:
    Vibes.shared.registerPush { result in
        // This callback is optional. If you choose to use it, result will be a
        // VibesResult<Void>.
    }
    

Unregistering for Push

Use the following code to unregister for push. You can add the code wherever it makes the most sense for your application.

Vibes.shared.unregisterPush { result in
    // This callback is optional. If you choose to use it, result will be a
    // VibesResult<Void>.
}

Updating Device

Use the following code to update a device. You can add the code wherever it makes the most sense for your application.

Vibes.shared.updateDevice { result in
    // This callback is optional. If you choose to use it, result will be a
    // VibesResult<Credential>.
}

Event Tracking

Launch, clickthru, and pushreceived events are mostly automatically tracked for you, although to properly track clickthru events, you must add the following to your AppDelegate:

# iOS 9
extension AppDelegate {
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        VibesPush.Vibes.shared.receivedPush(with: userInfo)
    }
}
 
# iOS 10
extension AppDelegate: UNUserNotificationCenterDelegate {
    public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        VibesPush.Vibes.shared.receivedPush(with: notification.request.content.userInfo)
        completionHandler([])
    }
}

If you would like to change the events that are being tracked, you can modify your Vibes.configure call.

Vibes.configure(appid: "TEST_APP_ID", trackedEventTypes: [.launch])

Deep Linking

Deep linking consists of adding functionality to go to a specific view, a particular section of a page, or a certain tab. If you have deep linking enabled in your push notification setup, you can retrieve its value in the push notification payload.

{
  "aps": {
    "alert": {
      "title": "Push Notification!",
      "subtitle": "From Vibes",
      "body": "You did it! "
    }
  },
  "client_app_data": {
    ...
    "deep_link": "XXXXXXX",
    ...
  },
  "message_uid": "9b8438b7-81cd-4f1f-a50c-4fbc448b0a53"
}

One way to handle deep linking in your application is to parse the push notification payload as follows, and push the viewController:

fileprivate let kClientDataKey = "client_app_data"
fileprivate let kDeepLinkKey = "deep_link"
fileprivate let kPushDeepLinkView = "XXXXXXX"
 
...
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    completionHandler()
    receivePushNotif(userInfo: response.notification.request.content.userInfo)
}
 
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    var configuration = Configuration()
    Vibes.configure(appId: configuration.environment.appKey, logger: EventEmitterLogger.shared, apiUrl: configuration.environment.baseURL)
    VibesPush.Vibes.shared.receivedPush(with: userInfo)
    completionHandler(.newData)
}
 
/// When the user clicks on a push notif, two events will be sent to Vibes backend: .launch, .clickthru events.
/// If you specify a value for 'deep_link' in client_app_data, you can redirect the user to the viewcontrollers of
/// your choice when he clicks on the push notification. The deep_link format is free
/// (best practice:{nameApp}://{viewcontrollers}{%parameters}
fileprivate func receivePushNotif(userInfo: [AnyHashable : Any]) {
    Vibes.configure(appId: kAppKey)
    VibesPush.Vibes.shared.receivedPush(with: userInfo)
 
    // Over simplified deep_link mechanism, but you get the idea.
    guard let client_data = userInfo[kClientDataKey] as? [String: Any],
        let deepLink = client_data[kDeepLinkKey] as? String
        else { return }
    if (deepLink == kPushDeepLinkView) {
        self.navigationController.pushViewController(deepLinkViewController, animated: true)
    }
}