Notifications (BrandMessenger iOS SDK)
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.
- Visit this link, Apple Push Notification service SSL (Sandbox)

- 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.
- Push Notifications
- 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)
}
})
Check if its a Brand Messenger notification
You can check if its a Brand Messenger APNs notification or not like below:
NSDictionary * userInfo = response.notification.request.content.userInfo;
KBMPushNotificationService *service = [[KBMPushNotificationService alloc]init];
if ([service isBrandMessengerChatNotification: userInfo]) {
// Brand Messenger notification
} else {
//Handle your notification
}
}
Custom Push Notification messages
When using generic push-notifications, the SDK Provides a way for the developer to customize the title and text of the incoming push-notification displayed in the iOS Banner.
This requires the following steps.
1) Create new Notification Service Extension target
From Xcode, in project TARGETS
, add a new target of type Notification Service Extension
.
Official instructions are https://developer.apple.com/documentation/usernotifications/modifying_content_in_newly_delivered_notifications?language=objc
This will create a new NotificationService
file.
2) Update NotificationService file
Update the file to look like the following
import UserNotifications
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
// Modify the notification content here...
/// Brand messenger notification
if isBrandMessengerChatNotificaiton(content: request.content) {
bestAttemptContent.title = NSLocalizedString("GenericNotificationTitle", tableName: nil, bundle: .main, value: "Chat", comment: "")
bestAttemptContent.body = NSLocalizedString("GenericNotificationContentText", tableName: nil, bundle: .main, value: "You have a new message.", comment: "")
}
contentHandler(bestAttemptContent)
}
}
override func serviceExtensionTimeWillExpire() {
// Called just before the extension will be terminated by the system.
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
/// Use this method to check for notification for Brand Messenger.
/// - Parameter content:Pass `UNNotificationContent` object for notification userInfo payload.
/// - Returns: Returns `true` if it's a Brand Messenger notification otherwise `false`.
func isBrandMessengerChatNotificaiton(content: UNNotificationContent) -> Bool {
let prefix = "BRANDMESSENGER_"
let notificationKey = "AL_KEY"
guard let brandMessengerPrefix = content.userInfo[notificationKey] as? String, brandMessengerPrefix.hasPrefix(prefix) else {
return false
}
return true
}
}
3) Bundle Localizable.strings into the created extension
In Xcode, in Project settings, select the newly created Notification Service Extension
TARGET
. Go to Build Phases
tab, and in Copy Bundle Resources
, add the app's Localizable.strings
file. If one does not already exist, create one and add.
4) Modify the notification strings
In the Localizable.strings
file, add values for GenericNotificationTitle
and GenericNotificationContentText
to set them on the incoming push-notification banners.
GenericNotificationTitle = "Khoros Chat";
GenericNotificationContentText = "You have a new message!";
Updated 4 months ago