Skip to content

Push notifications

Overview

This guide explains how to integrate with Push Notifications for iOS and how to add UNNotificationServiceExtension to Xcode prject to show image in notification.

Before you begin

Register for remote notifications

Either at startup, or at the desired point in your application flow, register your app for remote notifications.

You can manually register for remote push notifications and use UNUserNotificationCenterDelegate protocol methods to handle incoming notifications.

In addition, the SDK also helps you to do these things by calling the [SohaSDK requestPushNotification] method.

Below, we provide instructions in two different ways for your convenience:
- Method 1: Manual.
- Method 2: Using the SDK's method.

Method 1: Manual

Step 1: Register Remote Notifications
[UNUserNotificationCenter currentNotificationCenter].delegate = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge)
                          completionHandler:^(BOOL granted, NSError * _Nullable error) {
    if( !error ) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [[UIApplication sharedApplication] registerForRemoteNotifications];
        });
    } else {
         NSLog( @"ERROR: %@ - %@", error.localizedFailureReason, error.localizedDescription );
    }
}];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
Step 2: Handle the notification center’s delegate.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler{
    [SohaSDK didReceiveNotificationResponse:response];
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
    [SohaSDK willPresentNotification:notification];
}

Tip

To simplify and avoid manual handling of code snippets, use the requestPushNotification method.

Method 2: Using the SDK's method

The SDK provides a wrapping method that simplifies integration. Simply call the following method:

[SohaSDK requestPushNotification];
By calling this method, the wrapped code will be executed, and any necessary integration steps will be handled internally. This method will take care of registering for remote push notifications and handling the notification center's delegate, allowing you to focus on using the SDK's functionality without the need for manual code handling.

Access the registration token

Implement the application(_:didRegisterForRemoteNotificationsWithDeviceToken:) method to retrieve the APNs token, and then call SohaSDK registerDeviceToken:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [SohaSDK registerDeviceToken:deviceToken];
}