Push

Info
Ocamba provides powerful messaging services to reach your users.

1. Setup

To configure the SDK for use, you need to initialize it with your app’s API credentials.

This should be done early in your application startup to start using API methods and features.

Also, to get notification center callbacks, you need to set its delegate to self.


```swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:
[UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    UNUserNotificationCenter.current().delegate = self
    let builder = OcambaBuilder(apiKey: "activation-code").push().build()
    OcambaHoood.initialize(config:builder)
    return true
}
```

Now send the returned device token to the Ocamba Push service.

The OS will trigger a callback with the device token.

Depending on the OS version, the definition will change slightly.

From either, you can forward the token to the OcambaHoood SDK.


```swift
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    OcambaHoood.registerToken(deviceToken)
}
```

When your app receives a push notification or is launched by tapping on a push notification, your app’s delegate will be called back by the OS.

To correctly report on push open rates and conversions, you need to tell Ocamba that the remote notification launched the app.

This sample shows how to track push open conversions when a user opened a notification and launched the app.

However, you may call pushTrackOpen on another place after additional logic but response object from the notification must be passed to it. If true is passed in method as first parameter method completion will return url that you can handle as you wish, if flag is false url will be opened by SKD it’s self.


```swift
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
           OcambaHoood.pushTrackOpen(true, response: response, completion: { url in
//handle url
})
    completionHandler()
}
```

For silent notifications that are used for administrative purposes, like updating beacons list, geofence list, and all other administrative messages that are not supposed to disturb the user, you need to implement:


```swift
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
    OcambaHoood.didReciveAdministrativeNotificationWith(userInfo) { (geo,bcs) in }
}
```

Add required capabilities

  • Select the root project and under Signing & Capabilities, enable “Push Notifications” by clicking Capability and then double click on Background Modes and Push Notifications.
  • Only do this for the main application target.
  • Do not do this for the Notification Service Extension.
  • Check “Background processing” and “Remote notifications”.

If you wish you can activate background mode for push notification directly in Info.plist file by opening it as source code and adding the “remote-notification” line in array.



<key>UIBackgroundModes</key>
<array>
    <string>remote-notification</string>
    <string>fetch</string>
    <string>location</string>
</array>

Suppose you wish to have a callback when Ocamba is inited.

In that case, you need to inherit OcambaHooodDelegate protocol, conform to protocol assigning self to OcambaHoood.ocambaDelegate and add method class where you want to catch init:


```swift
class MyClass: UIViewController, OcambaHooodDelegate {

//Desired function
{
OcambaHoood.ocambaDelegate = self
}

func ocambaInited(_ success: Bool) {
      //  Do your logic here
 }
 ```


2. Add Notification Service Extension

  • In Xcode Select File -> New -> Target
  • Select Notification Service Extension

  • Enter the product name as OcambaHooodNotificationServiceExtension and press Finish.

Press Cancel on the Activate scheme prompt.

By cancelling, you are keeping Xcode debugging your main app instead of extension.

You can always switch to debugging your extension within Xcode (next to the run button).

  • In the project navigator, click the top-level project directory to open the Xcode project settings and select the OcambaHooodNotificationServiceExtension target in the project and targets list.

On the General tab, set the Deployment Target to be iOS 10.

Open NotificationViewController.swift and replace the whole file contents with the below code.


```swift
import UserNotifications
import UIKit
import OcambaHoood

class NotificationService: UNNotificationServiceExtension {
    
    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?
    var receivedRequest: UNNotificationRequest!
    
    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        self.contentHandler = contentHandler
        receivedRequest = request
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
        if let bestAttemptContent = bestAttemptContent {
            OcambaHoood.didReceiveNotification(with: request, best: bestAttemptContent) { (bestAttempt) in
                contentHandler(bestAttempt)
            }
        }
    }

    override func serviceExtensionTimeWillExpire() {
        // Called just before the extension will be terminated by the system.
        // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
            OcambaHoood.notificationBuilderWillTerminate(with: receivedRequest.content.userInfo, best: bestAttemptContent) { (best) in
                contentHandler(best)
            }
        }
    }
}
```

3. Enable “App Groups” Capability

  • Under Signing & Capabilities, enable App Groups for all targets clicking on Capability, and the the OcambaHooodNotificationServiceExtension target (if the added Content extension should be activated on it too. About content extension read further).
  • Check an existing App Group name if you have one or create a new with the “+”. Make sure you select the same name on both targets.
  • The name of your app group should be group. {your_bundle_id}.ocambaHoood. So, for example, if your application’s bundle identifier is com.example.app, your app group name should be group.com.example.app.ocambaHoood.


4. Add Notification Content Extension

Notification content extension allows proper presentation of multimessage notifications, where you can choose our rebuild layout or create yours.

You can read more about multi messages in the multimessage section.

  • In Xcode Select File -> New -> Target
  • Select Notification Content Extension

  • Further, the procedure is the same as for Service Extension, and the name would be OcambaHooodNotificationContentExtension.

  • Open NotificationViewController.swift and replace the entire file contents with the below code.


```swift
import UIKit
import UserNotifications
import UserNotificationsUI
import OcambaHoood

class NotificationViewController: UIViewController, UNNotificationContentExtension {
    
    var ocambaNotif:OcambaNotificationView!
   
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any required interface initialization here.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    func didReceive(_ notification: UNNotification) {
        self.preferredContentSize = OcambaHoood.getPreferedContentSize(with: notification)
        ocambaNotif =  OcambaNotificationView(frame: view.frame, notification: notification, self)
    }
   
    //  Solution for iOS versions older than iOS12(with notification actions)
    func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void) {
        ocambaNotif.handleNotificationAction(response)
    }
   
}
```
  • In Info.plist of content extension you need to set UNNotificationExtensionCategory to “oc_contentExt” so that multimessages would be sent to extension to handle them. Also:

    • UNNotificationExtensionInitialContentSizeRatio should be “0.8”
    • UNNotificationExtensionDefaultContentHidden should be “YES”
    • UNNotificationExtensionUserInteractionEnabled should be “YES”
  • If you want to use custom handling for notifications presenting you need to add customNotificationHandling to OcambaBuilder:

  • OcambaBuilder(apiKey: “activation-code“).customNotificationHandling().build()

  • Don’t forget to assign proper app group to extension, so that SDK works as it’s supposed to work.


5. Push callbacks and methods

5.1. Badge

For reducing notification count by one, you can use:


```swift
OcambaHoood.decreseNotificationCount(forNotif: UNMutableNotificationContent?)  
```

For increasing badge number by one, you can use:


```swift
OcambaHoood.addNotificationCount(forNotif: UNMutableNotificationContent?)  
```

5.2. Callbacks

Callbacks that are allowing developers to create custom actions on some of OcambaHoood framework events:

Triggered when token is changed, get notification object:


```swift
OcambaHoood.getNotificationContent(userInfo: userInfo)
```

When requesting for object, Notification Service extension is the place where you can get your direct push object.

On This Page