Integration: V4 Objective-C

The following information will show you how to integrate the Vibes Push Notifications SDK (v.3+) into an iOS app (Objective-C).

The Objective-C 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.m file:

[Vibes configureWithAppId:@"[YOUR APP ID HERE]"];

The Vibes SDK will notify your app of any failed or successful API calls via a delegate. When configuring the Vibes SDK, assign the delegate to any class implementing the VibesAPIDelegate protocol.

For example, you could set the delegate to your App Delegate immediately after configuration:

    [Vibes configureWithAppId:@"[YOUR APP ID HERE]"];
    [[Vibes shared] setWithDelegate:(id)self];
}
 
- (void)didRegisterDeviceWithDeviceId:(NSString *)deviceId error:(NSError *)error {
    ...
}

If, for any reason, you want to override the default API URL, tracked event type, storage type, advertisingID (if you use AdSupport), or the default logger, pass in an optional configuration:

NSArray *trackedEvents = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:TrackedEventTypeLaunch], [NSNumber numberWithInt:TrackedEventTypeClickthru], nil];
VibesConfiguration *config = [[VibesConfiguration alloc] initWithAdvertisingId:@"[your advertising id]" // if you're using AdSupport
                                                                        apiUrl:NULL
                                                                        logger:NULL // implement VibesLogger to configure your own logger
                                                                   storageType:VibesStorageEnumUSERDEFAULTS // or VibesStorageEnumKEYCHAIN
                                                             trackedEventTypes:trackedEvents];  // you can also pass an empty array if you don't want to record any events.
[Vibes configureWithAppId:@"[YOUR APP ID HERE]"
                configuration:(id)config];

You must reset the Vibes default endpoint if you want to use the Vibes Platform Europe instance, as defined in Technical Details.

Registering a Device

Add the following code wherever it makes the most sense for your application.

[[Vibes shared] registerDevice];

Delegate method:

- (void)didRegisterDeviceWithDeviceId:(NSString *)deviceId error:(NSError *)error {
    ...
}

Unregistering a Device

Add the following code wherever it makes the most sense for your application.


[[Vibes shared] unregisterDevice];

Delegate method:

- (void)didUnregisterDeviceWithError:(NSError *)error {
    ...
}

Push Messaging Configuration

Registering for Push

  1. Register for remote notifications by following the OS Local and Remote Notification Programming guide.
  2. Add the following code to your app delegate.
    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
        ...
        [[Vibes shared] setPushTokenFromData:deviceToken];
        [[Vibes shared] registerPush];
        ...
    }
    
  3. Delegate method:
    - (void)didRegisterPushWithError:(NSError *)error {
        ...
    }
    

Unregistering for Push

Add the following code wherever it makes the most sense for your application.

[[Vibes shared] unregisterPush];

Delegate method:

- (void)didUnregisterPushWithError:(NSError *)error {
    ...
}

Update the Device Location

Add the following code wherever it makes the most sense for your application to update the location. It is not required and stores the current location with device.

[[Vibes shared] updateDeviceWithLat:latitude.doubleValue long:longitude.doubleValue];

Delegate method:

- (void)didUpdateDeviceLocationWithError:(NSError *)error {
    ...
}

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
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    ...
    [[Vibes shared] receivedPushWith:userInfo at:[NSDate date]];
    completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge);
}

# iOS 10
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
    ...
    [[Vibes shared] receivedPushWith:response.notification.request.content.userInfo at:[NSDate date]];
    ...
}

Deep Link

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"
}

Sample code for parsing the push notification payload and navigating to the deep link:

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
    [self receivedPushNotifWith:response.notification.request.content.userInfo];
    completionHandler();
}
 
 
// For iOS 9
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    [self receivedPushNotifWith:userInfo];
    completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge);
}
  
- (void)receivedPushNotifWith:(NSDictionary *)userInfo {
    [[Vibes shared] receivedPushWith:userInfo at:[NSDate date]];
    if (userInfo[@"client_app_data"][@"deep_link"]) {
        // Use the deep_link value here to navigate to the appropriate view controller
    }
}

Notification Sound

If your application contains a custom sound for a push notification, you can specify this sound on the Vibes Platform.

The push payload received will look like the following:

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

The sound will be played automatically if the sound file exists in your project resources.

Custom Properties

You can specify up to 10 key-value pairs on the Vibes Platform. The push payload received will look like the following:

{
  "aps": {
    "alert": {
      "title": "Push Notification!",
      "subtitle": "From Vibes",
      "body": "You did it! "
    }
  },
  "client_custom_data":{
      "key1":"val1",
      "key2":"val2",
      ....
   },
  "message_uid": "9b8438b7-81cd-4f1f-a50c-4fbc448b0a53"
}

In your application you can retrieve the custom data like this:

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
    if (response.notification.request.content.userInfo[@"client_custom_data"]) {
        // Use the client_custom_data value here
    }
    ...
}
 
 
// For iOS 9
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    if (userInfo[@"client_custom_data"]) {
        // Use the client_custom_data value here
    }
    ...
}

Badge

On the Vibes Platform, you can specify your application badge value. The push payload received will look like the following:

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

Category

Push notification category is a new feature from iOS 10.+. On the Vibes Platform, you can specify the category of your push notification. The push payload received will look like the following:

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

Please check the Apple documentation for how to configure categories and actionable notifications.

Rich Media: Image, Video, Audio

From version iOS 10, you can send rich content embedded (image, video, or audio) in push notifications. Please check the Apple documentation to check how to integrate rich push capability to your application.

On the Vibes Platform, you can specify the rich content you want your customers to see.

The push payload received will look like the following:


{
  "aps": {
    "alert": {
      "title": "Push Notification!",
      "subtitle": "From Vibes",
      "body": "You did it! "
    },
    "mutable-content":1
  },
  "client_app_data":{
      "client_message_id":"fgfCHIUHIY8484FYIHWF",
      "media_url" : "https://www.publiclyhostedurl.com"
   },
  "message_uid": "9b8438b7-81cd-4f1f-a50c-4fbc448b0a53"
}

Silent Push

On the Vibes Platform, you can specify to send a push notification as a silent or background push. Silent or background push notifications can be used to update a badge payload or send custom data to the app. A push notification received will look like the following:


{
  "aps": {
    "content-available": 1
  },
  "message_uid": "9b8438b7-81cd-4f1f-a50c-4fbc448b0a53"
}

Person Management

Since v 4.0.0, it is now possible to fetch a Person record, which is also accessible via the Person API as documented here.

The Structure of a Person

A Person exposes the following methods for obtaining more information.

MethodComment
public var personKey: String?Returns the UUID that uniquely identifies the user associated with the device
public var mdn: String?Returns the MDN of the user associated with the device
public var externalPersonId: String?Returns the external person id that may have been specified for this user associated with the device

Fetching the Person Record

The person associated with the device can be obtained by calling the getPerson method on the Vibes SDK.

Vibes.shared.getPerson() {
 (person: Person ? , error : VibesError ? ) in
 if let error = error {
  // error occurred, handle it
  return
 }
 let personKey = person.personKey
  // use the person key and other person information
  ...
}

App Inbox Configuration

Inbox support is now available in v 4.0.0 and later of the Vibes Push SDK. The current feature set only enables obtaining and updating these inbox messages and does not provide any UI components for rendering the inbox messages.

Initializing the Inbox Support

There is no additional requirements for configuring the support for inbox messages beyond the standard process for configuring the iOS Push SDK documented here.

The Structure of an InboxMessage

An InboxMessage class exposes the following fields for obtaining information about an inbox message.

FieldComment
public var messageUID: String?Returns the messageUID that uniquely identifies this inbox message
public var subject: String?Returns the subject that can be used as a header for an inbox message
public var content: String?Returns the content for further textual information to the reader of the message.
public var detail: String?Returns a URL that may lead to an image, a web page or any rich media that one may want to display as the landing page of the inbox message
public var read: Bool?Returns true or false to determine if the message has previously been read by the user of the app
public var expiresAt: Date?Returns a timestamp of when a message will be considered expired
public var collapseKey: String?Returns a key that is used to remove other messages previously sent with the same key
public var iconImage: String?Returns a URL that points to an image that can displayed as an icon for an inbox message list (1 of 2)
public var mainImage: String?Returns a URL that points to an image that can displayed as an icon for an inbox message list (2 of 2)
public var inboxCustomData: [String: AnyObject]Contains a map of custom data that you can pass to the app per message
public var inboxMessageUID: String?A UID which maps a push message to this inbox message

Fetching Inbox Messages

A list of maximum 200 messages for each user of the app can be fetched by invoking the fetchInboxMessages method as show below. On success, an array of InboxMessages is returned; otherwise, there is an error passed to the callback.

Note that by default, these messages are sorted in descending order by date created, which means the most recent message will be first in the collection.

[
 [Vibes shared] fetchInboxMessageWithMessageUID:inboxMessageUID: ^ (NSArray < InboxMessage * > * _Nonnull messages, NSError * _Nullable error) {
  if (error == nil) {
   // error occurred, handle here
  } else {
   // use messages
  }
 }
];

Fetching A Single Inbox Message

A single inbox message can be fetched by invoking fetchInboxMessage with the messageUID of the requested message as shown below. On success, a single InboxMessage is obtained; otherwise, there is an error passed to the callback.

[
 [Vibes shared] fetchInboxMessageWithMessageUID: messageUID: ^ (InboxMessage * _Nullable message, NSError * _Nullable error) {
  if (error == nil) {
   // error occurred, handle here
  } else {
   // use the message
  }
 }
]

Call the inbox_open event trigger after calling the fetchInboxMessage function to represent opening an inbox message. This will trigger inbox_open event for the inbox message. This will track customer engagement for platform reporting.

[[Vibes shared] onInboxMessageOpenWithInboxMessage:message]

Marking an Inbox Message as Read

An inbox message can be marked as read so it could possibly be rendered differently from unread inbox messages. This is done by invoking markInboxMessageAsRead with the messageUID of the requested message as shown below. On success, an updated version of the InboxMessage is returned, otherwise there is an error passed to the callback.

[
 [Vibes shared] markInboxMessageAsReadWithMessageUID: messageUID: ^ (InboxMessage * _Nullable message, NSError * _Nullable error) {
  if (error == nil) {
   // error occurred, handle here
  } else {
 
   // use the updated message
  }
 }
]

Expiring an Inbox Message

An inbox message can be marked as expired, which would automatically make it unavailable for viewing when inbox messages are re-fetched again. This is done by invoking expireInboxMessage with the messageUID of the requested message as shown below. On success, and updated InboxMessage is returned with the expirationDate set; otherwise. there is an error message passed to the callback.

[
 [Vibes shared] expireInboxMessageWithMessageUID: messageUID: ^ (InboxMessage * _Nullable message, NSError * _Nullable error) {
  if (error == nil) {
   // error occurred, handle here
  } else {
   // use the updated message
  }
 }
]

Push Message Linked to An InboxMessage

Since v 4.0.0, it is now possible for a push message to contain a pointer to an inbox message called inboxMessageUid. In such a scenario, one can override the default VibesReceiver, fetch the associated InboxMessage, and then open your own custom detail screen when such a message is received. Below is an example:

-(void) application: (UIApplication * ) application didReceiveRemoteNotification: (NSDictionary * ) userInfo fetchCompletionHandler: (void( ^ )(UIBackgroundFetchResult)) completionHandler {
 NSDictionary * clientAppData = userInfo[@ "client_app_data"];
 if (clientAppData != nil) {
  NSString * inboxMessageUID = [clientAppData valueForKeyPath: @ "inbox_message_uid"];
  if (inboxMessageUID != nil) {
   [
    [Vibes shared] fetchInboxMessage: ^ (InboxMessage * _Nonnull message, NSError * _Nullable error) {
     if (error != nil) {
      // handle error
     } else {
      // open your custom View detail Controller
      YourCustomDetailsViewController * yourVc = [YourCustomDetailsViewController loadFromNib];
      yourVc.message = message;
      UINavigationController * navCont = [
       [UINavigationController allo] initWithRootViewController: yourVc
      ];
      [self presentViewController: navCont animated: YES completion: nil];
     }
    }
   ]
  }
 }
}