Integration: V2 Swift
The following information will show you how to integrate the Vibes Push Notifications SDK (version 2.1.00) into an iOS app.
The Swift 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: "YOUR_APP_ID")
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.configure(appId: "YOUR_APP_ID")
Vibes.shared.set(delegate: self)
}
func didRegisterPush(error: VibesError?) {
...
}
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:
Vibes.configure(
appId: "YOUR_APP_ID",
trackedEventTypes: [TrackedEventType.launch, TrackedEventType.clickthru] as NSArray,
storageType: .USERDEFAULTS, // default is .KEYCHAIN
advertisingId: "YOUR_ADVERTISING_ID", // optional
logger: nil, // implement VibesLogger to configure your own logger
apiUrl: nil //defaults to US endpoint as defined below
)
You must reset the Vibes default endpoint if you want to use the Vibes Platform Europe instance, as defined in Technical Details.
- Default Vibes Push endpoint (US): https://public-api.vibescm.com/mobile_apps
- Vibes Push Europe endpoint (UK): https://public-api.vibescmeurope.com/mobile_apps
Registering a Device
Add the following code wherever it makes the most sense for your application.
Vibes.shared.registerDevice()
Delegate method:
func didRegisterDevice(deviceId: String?, error: Error?) {
...
}
Unregistering a Device
Add the following code wherever it makes the most sense for your application.
Vibes.shared.unregisterDevice()
Delegate method:
func didUnregisterDevice(error: Error?){
...
}
Push Messaging
Registering for Push
- Register for remote notifications by following the OS Local and Remote Notification Programming guide.
- Add the following code to your app delegate.
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { ... Vibes.shared.setPushToken(fromData: deviceToken) Vibes.shared.registerPush() ... }
- Delegate method:
func didRegisterPush(error: Error?) { ... }
Unregistering for Push
Add the following code wherever it makes the most sense for your application.
Vibes.shared.set(delegate: self)
Vibes.shared.unregisterPush()
Delegate method:
func didUnregisterPush(error: Error?) {
...
}
Update 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.updateDevice(lat: 41.8686839, long: -87.8075274)
Delegate method:
func didUpdateDeviceLocation(error: 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
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([])
}
}
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:
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
handlePushNotification(userInfo: response.notification.request.content.userInfo)
completionHandler()
}
// For iOS 9
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
handlePushNotification(userInfo: userInfo)
completionHandler(.newData)
}
fileprivate func handlePushNotification(userInfo: [AnyHashable : Any]) {
// Allow Vibes to track the .launch and .clickthru events from the notification
Vibes.shared.receivedPush(with: userInfo)
// Check for a deep link in the payload
guard let clientData = userInfo["client_app_data"] as? [String: Any],
let deepLink = clientData["deep_link"] as? String
else { return }
// Use deepLink 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:
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
if let customData = response.notification.request.content.userInfo["client_custom_data"] as? [String: Any] {
// Use customData here
}
...
}
// For iOS 9
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if let customData = userInfo["client_custom_data"] as? [String: Any] {
// Use customData 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 notification 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"
}
Updated over 1 year ago