4. SDK Integration

SDK Integration

  1. AppDelegate

  2. AppDelegate Lifecycle Methods

  3. Notification Service Extension

  4. AppGroup

AppDelegate

In AppDelegate create two references to hold the SDK and a PushologiesUserNotificationDelegate.


var sdk: Pushologies? 

var userNotificationDelegate: PushologiesUserNotificationDelegate? 

The PushologiesUserNotificationDelegate can be subclassed if customisation is required. Make sure that super calls are made for each overridden method.

class MyAppUserNotificationDelegate: PushologiesUserNotificationDelegate {}

During the didFinishLaunchingWithOptions AppDelegate lifecycle method instantiate the SDK, using the API credentials gathered from Stage Two.

self.sdk = Pushologies(apiKey: “MyPushologiesAPIKey”, secretKey: “MyPushologiesSecretKey”)

Immediately after creating the SDK instantiate and set a PushologiesUserNotificationDelegate


userNotificationDelegate = PushologiesUserNotificationDelegate(sdk: sdk) 

UNUserNotificationCenter.current().delegate = userNotificationDelegate 

Lastly, following the above statements, ask the SDK to show authorisation prompts for push notification authorisation and location access authorisation.

sdk?.showAuthorisationPrompts()

AppDelegate Lifecycle

If not already present add the following three lifecycle methods and call the SDK’s corresponding methods. Here is a complete example.


func application( 

    _ application: UIApplication, 

    didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data 

) { 

    sdk?.application( 

        application, 

        didRegisterForRemoteNotificationsWithDeviceToken: deviceToken 

    ) 

} 

 

func application( 

    _ application: UIApplication, 

    didReceiveRemoteNotification userInfo: [AnyHashable : Any], 

    fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void 

) { 

    sdk?.application( 

        application, 

        didReceiveRemoteNotification: userInfo, 

        fetchCompletionHandler: completionHandler 

    ) 

} 

 

func application( 

    _ application: UIApplication, 

    didFailToRegisterForRemoteNotificationsWithError error: Error 

) { 

    sdk?.application( 

        application, 

        didFailToRegisterForRemoteNotificationsWithError: error 

    ) 

} 

Notification Service Extension

In the folder for the notification service extension target created in Stage One create a new class that inherits from PSDKServiceExtension. Here is an example of the new class:


import PushSDK 

 

class NotificationService: PSDKServiceExtension { 

         

    override func didReceive( 

    _ request: UNNotificationRequest, 

    withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void 

    ) { 

       if request.isPushologiesRequest { 

           super.didReceive(request, withContentHandler: contentHandler) 

       } 

    } 

     

} 

 App Group

In both the main target‘s (ie. MyApp) Info.plist and the service extension target’s Info.plist add the following key PushologiesAppGroup and then for the value fill in the App Group name gathered in Stage One (ie. group.com.MyApp).