Notifications (BrandMessenger iOS SDK) (APAC)

How to receive push notifications for the chat within your iOS app

📘

Note: This section applies only if you're using Brand Messenger SDK.

Setting up APNS Certificates

Brand Messenger sends its payload to Apple servers which then send the Push notification to your user's device.

Creating APNS certificates

For Apple to send these notifications, you must create an APNS certificate in your Apple developer account.

  1. Visit this link, Apple Push Notification service SSL (Sandbox)
  1. Visit this link and select Apple Push Notification service SSL (Sandbox & Production), and click Create.

📘

Note: The supported format for APNS certificates is .p12.

Export a .p12 file from keychains and upload it to Brand messenger

The p12 files are required to for authentication with Apple servers and for sending APNS push notifications to devices. This section shows you how to export p12 file from Keychains for uploading to the Brand Messenger.

To export Apple push service and APNS Development iOS certificate p12 file:

For Apple push service export:

A. Open Keychain Access on your Mac and go to login > Select My Certificates.

B. Find the Apple push services certificate you added, right-click on the certificate, and select the Export option from the menu to get a .p12 file. Type a name for the file and save it with the password.

For APNS Development iOS export :

A. Open Keychain Access on your Mac and go to login > Select My Certificates.

B. Find the APNS Development iOS certificate you added, right-click on the certificate, and select the Export **** option from the menu to get a .p12 file. Type a name for the file and save it with the password.

Upload APNS Certificates

Contact Khoros Support and provide the your exported p12 files with password to upload the push notification certificates.

Updating Capabilities

After setting up the APNS, you must enable push notifications within your project.
Click on your project, click Capabilities, and select Enable.

  1. Push Notifications
  2. Background Modes - Remote notification

Setup Push Notifications

Push notification setup is rather straightforward, all you need to do is to find the AppDelegate file in your project and follow all the instructions below.

Add the below push notification code in your AppDelegate.swift or AppDelegate.m file

If the below method already exists, copy the code inside the method and paste it into your AppDelegate file.

🚧

Make sure your AppDelegate conforms to UNUserNotificationCenterDelegate and make sure that each of the methods or code inside the method is added in your Appdelegate file

Sample code is as below:

import BrandMessengerUI
import UIKit
 
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
    var window: UIWindow?
 
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
 
        // Push notification register.
        UNUserNotificationCenter.current().delegate = self
        BrandMessengerManager.application(application, didFinishLaunchingWithOptions: launchOptions)
        return true
    }
 
 
    func applicationWillEnterForeground(_ application: UIApplication) {
        // Reset the app badge count
        BrandMessengerManager.applicationWillEnterForeground(application)
    }
 
    func applicationWillTerminate(_ application: UIApplication) {
        // Save context in app will Terminate
        BrandMessengerManager.applicationWillTerminate(application: application)
    }
 
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        // Register device token to Brand Messenger server.
        BrandMessengerManager.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
    }
 
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
 
        // Pass the notification to the Brand Messenger method for processing.
        BrandMessengerManager.application(application, didReceiveRemoteNotification: userInfo) { result in
            // Process your own notification here.
            completionHandler(result)
        }
    }
 
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
 
        // Pass the notification to the Brand Messenger method for processing.
        BrandMessengerManager.userNotificationCenter(center, willPresent: notification) { options in
            // Process your own notification here.
            completionHandler(options)
        }
    }
 
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
 
        // Pass the response to the Brand Messenger method for processing notification.
        BrandMessengerManager.userNotificationCenter(center, didReceive: response) {
            // Process your own notification here.
            completionHandler()
        }
    }
}
#import "AppDelegate.h"
@import BrandMessengerCore;
#import "YOUR_TARGET_NAME_HERE-Swift.h" // Important: Replace 'YOUR_TARGET_NAME_HERE' with your target name.
#import <UserNotifications/UserNotifications.h>
 
/// Add this UNUserNotificationCenterDelegate in your Appdelegate file
@interface AppDelegate () <UNUserNotificationCenterDelegate>
 
@end
 
@implementation AppDelegate
 
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 
    // Push notification register.
    UNUserNotificationCenter.currentNotificationCenter.delegate = self;
    [BrandMessengerManager application:application didFinishLaunchingWithOptions:launchOptions];
    return YES;
}
 
 
-(void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler {
 
    [BrandMessengerManager application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:^(UIBackgroundFetchResult backgroundFetchResult) {
        // Perform your other operations here
        completionHandler(backgroundFetchResult);
    }];
}
 
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification*)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
 
    [BrandMessengerManager userNotificationCenter:center willPresent:notification withCompletionHandler:^(UNNotificationPresentationOptions options) {
        // Process your own notification here.
        completionHandler(options);
    }];
}
 
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(nonnull UNNotificationResponse* )response
         withCompletionHandler:(nonnull void (^)(void))completionHandler {
 
    [BrandMessengerManager userNotificationCenter:center didReceive:response withCompletionHandler:^{
        // Process your own notification here.
        completionHandler();
    }];
}
 
- (void)applicationWillEnterForeground:(UIApplication *)application {
    [BrandMessengerManager applicationWillEnterForeground:application];
}
 
- (void)applicationWillTerminate:(UIApplication *)application {
    [BrandMessengerManager applicationWillTerminateWithApplication:application];
}
 
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [BrandMessengerManager application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
 
@end

📘

Note: Above didFinishLaunchingWithOptions BrandMessengerManager code is required in all the cases if you're using any other push notification setup like firebase or one signal or any other it needs to be added in your Appdelegate file.

Disable Notification

In order to disable the incoming push notification alert or only notification sound, you need to update notification mode. Below are the possible values of notification mode.

Notification Mode

Explanation

0

Enable notification with sound (Default)

1

Enable notification without sound

2

Disable notification (No Alert)

KBMRegisterUserClientService.updateNotificationMode(modeValue, withCompletion: {
    response, error in
    if(error != nil) {
     // Failed to update notification mode
    } else {
     KBMUserDefaultsHandler.setNotificationMode(modeValue)
    }
})

Did this page help you?