Add and integrate our iOS SDK

In this guide, we're going to take you through the final steps of integrating our SDK with your app. There's a few different steps to this, but they're all very simple, so follow each carefully and you'll be fine.

πŸ“˜

Note

Apple has both a Sandbox and Production push notification service. When you generate a device token, it's only valid for one of these services. To make things easier for our fellow developers, we manage sending to the correct endpoint based on the device token for you, so you don't need to worry

Step 1 - Initialise the SDK

Open up your Xcode workspace and then at the top of your AppDelegate you need to import our SDK :

import PushSDK
#import <Pushologies/PushSDK.h>

Then you will need to add the UNUserNotificationCenterDelegate to your AppDelegate, so something like:

class AppDelegate: UIResponder, UIApplicationDelegate,UNUserNotificationCenterDelegate {
@interface AppDelegate : UIResponder   <UIApplicationDelegate,UNUserNotificationCenterDelegate>

and then initialise the SDK in your didFinishLaunchingWithOptions delegate, making sure to pass in your SDK API key and secret.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
         sdk = PushSDK(secretKey: "your SDK secret key", apiKey: "your SDK API key")
         sdk?.application(application)
        
        return true
    }
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        [PushSDK 
        secretKey:@"your secret key"
        apiKey:@"your API key"];

        return YES;
    }

Finally at the bottom of your AppDelegate, extend it with the the following code:

extension AppDelegate {
  
  func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
      
        PushSDK().application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
     }

  func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
       PushSDK().application(application, didReceiveRemoteNotification: userInfo, fetchCompletionHandler: completionHandler)
     }
    
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 
        [PushSDK application:
        application,
        didRegisterForRemoteNotificationsWithDeviceToken: deviceToken];
    }

    - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)err { 
        [PushSDK application:
        application,
        didReceiveRemoteNotification: userInfo,
        fetchCompletionHandler: completionHandler];
    }

Step 2 - Update your Notification Service Extension

This bit is nice and easy - just overwrite everything in your NotificationService file with the below code:

import PushSDK

class NotificationService : PushSDKServiceExtension{
}
#import <Pushologies/PushSDK.h>

@interface NotificationService : PushSDKServiceExtension

@implementation NotificationService
@end

Yep - just add the extension and we'll do the rest.

Step 3 - Add your app group into your plist files

The SDK needs to be able to pick up the app group name, so we store this in the Info.plist in both the main app and the Notification Service Extension.

Add a new row to the Info.plist in each folder called PushologiesAppGroup and paste in the app group.

And that's it. If you build and run, you will get prompted to allow notifications and the device will register to our API and you can start pushing!