SDK Integration

SDK Integration

  1. AppDelegate

  2. AppDelegate Lifecycle Methods

  3. Notification Service Extension

  4. Carousel Notification Content Extension

  5. AppGroup

AppDelegate

In AppDelegate create two references to hold the SDK and a PushologiesUserNotificationDelegate (PushologiesCUserNotificationDelegate for Obj-C).

var sdk: Pushologies? 
var userNotificationDelegate: PushologiesUserNotificationDelegate?
Pushologies *sdk;
PushologiesCUserNotificationDelegate *sdkUserNotificationDelegate;

The PushologiesUserNotificationDelegate (PushologiesCUserNotificationDelegate for Obj-C) can be subclassed if customisation is required, but make sure that super calls are made at the end of each overridden method.

class MyAppUserNotificationDelegate: PushologiesUserNotificationDelegate {}
#import <PushSDK/PushSDK.h>
#import "PushSDK/PushologiesCUserNotificationDelegate.h"

NS_ASSUME_NONNULL_BEGIN

@interface AppUserDelegate : PushologiesCUserNotificationDelegate

@end

NS_ASSUME_NONNULL_END

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

sdk = Pushologies(
    apiKey: “MyPushologiesAPIKey”, 
    apiSecret: “MyPushologiesSecretKey”
)
sdk = [
        Pushologies.alloc
        initWithApiKey: infoDict[@"API_KEY"]
        apiSecret: infoDict[@"API_SECRET_KEY"]
    ];

Immediately after creating the SDK instantiate and set a PushologiesUserNotificationDelegate (PushologiesCUserNotificationDelegate for Obj-C)

userNotificationDelegate = PushologiesUserNotificationDelegate(sdk: sdk) 
UNUserNotificationCenter.current().delegate = userNotificationDelegate
sdkUserNotificationDelegate = [
        AppUserDelegate.alloc
        initWithSdk: sdk
    ];
 UNUserNotificationCenter.currentNotificationCenter.delegate = sdkUserNotificationDelegate;

Next set the customer ID to any string value you wish by calling

sdk?.customerID = "YourCustomIDHere"
sdk.customerID = @"YourCustomIDHere";

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

Task {
    await sdk?.showAuthorisationPrompts()
}
[sdk showAuthorisationPromptsWithCompletionHandler:^{}];

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

func application(
    _ application: UIApplication,
    handleEventsForBackgroundURLSession identifier: String,
    completionHandler: @escaping () -> Void
) {
    sdk?.application(
        application,
        handleEventsForBackgroundURLSession: identifier,
        completionHandler: completionHandler
    )
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [sdk application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    [sdk application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    [sdk application:application didFailToRegisterForRemoteNotificationsWithError:error];
}

- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)(void))completionHandler {
    [sdk application:application handleEventsForBackgroundURLSession:identifier completionHandler:completionHandler];
}

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 NotificationServiceExtension: PSDKServiceExtension {

    override func didReceive(
        _ request: UNNotificationRequest,
        withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void
    ) {
       if request.isPushologiesRequest {
           super.didReceive(request, withContentHandler: contentHandler)
       }
    }
    
}
#import "NotificationService.h"
#import <PushSDK/PushSDK.h>

@interface NotificationService ()

@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;

@end

@implementation NotificationService

PSDKServiceExtension *serviceExtension;

- (instancetype)init
{
    self = [super init];
    if (self) {
        serviceExtension = [PSDKServiceExtension.alloc init];
    }
    return self;
}

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    if (request.isPushologiesRequest) {
        [serviceExtension didReceiveNotificationRequest:request withContentHandler:contentHandler];
    }
}

- (void)serviceExtensionTimeWillExpire {
    [serviceExtension serviceExtensionTimeWillExpire];
}

@end

Carousel Notification Content Extension

In the folder for the notification content extension target created in Stage One open the storyboard for the view controller found there. Edit the view controller in the storyboard and make it inherit from CarouselNotificationContentViewController in the PushSDK framework. If this produces an error check that the framework has been included in the target's Frameworks and Libraries section.

Next open the Info.plist for the Content Extension target. Find UNNotificationExtensionCategory under NSExtension -> NSExtensionAttributes and add in PushologiesCarouselNotification. This will allow the content extension to recognise that it must do something if a carousel push notification arrives.

App Group

The following needs performed for each target in the project. At a minimum this will include the main app, and the two notification extension targets created while following this guide.

Signing & Capabilities

Add the AppGroup capability if not present. Then under the AppGroup capability add the reverse domain name specified during Stage One in the Apple Developer Portal. (ie. group.com.MyApp).

Info.plist

In the Info.plist of each target add the following key PushologiesAppGroup and then for the value fill in the AppGroup domain used above.

Adding Tags

By attaching a tag you can create segments and target notifications to your devices subscribed to those tags.

sdk?.attachDevice(to: "YourTagHere")
[sdk attachDeviceTo: @"YourTagHere"];

To detach a tag

sdk?.detachDevice(from: "YourTagHere")
[sdk detachDeviceFrom: @"YourTagHere"];

Show In-App Notifications (custom triggered)

By calling this method and passing the custom event identifier string as parameter, the app will show any active in-app notification already received and stored in the app.

sdk?.showInAppNotification(with: "YourCustomIdentifierHere")
[sdk showInAppNotificationWith: @"YourCustomIdentifierHere"]