Notifications for Chat Activity
Notifications can be sent to the user when the user isn't viewing the chat session. A notification can appear when the app is in the background, or when the app is in the foreground but the chat session is minimized. The following activities can cause notifications:
- Agent has connected
- Agent sent a message
- Agent requested a file transfer
- Agent canceled a file transfer
- Agent ended a session
- Session will timeout soon
To ensure that the app can send these notifications while in the background, chat must be configured to allow background execution (allowBackgroundExecution) and to allow for background notifications (allowBackgroundNotifications). Both these settings are turned on by default. See Configure a Chat Session for details.
-
Import the UserNotifications framework in
your AppDelegate class.
In Swift:
import UserNotifications
In Objective-C:
@import UserNotifications;
-
Register for local notifications in your AppDelegate's didFinishLaunchingWithOptions method.
In Swift:
// Get the notification center object let center = UNUserNotificationCenter.current() // Register a delegate (see next step for delegate implementation) center.delegate = self // Request authorization center.requestAuthorization(options: [.alert,.sound], completionHandler: { granted, error in // Enable or disable features based on authorization }) // Create general category let generalCategory = UNNotificationCategory(identifier: "General", actions: [], intentIdentifiers: [], options: .customDismissAction) let categorySet: Set<UNNotificationCategory> = [generalCategory] // Set category center.setNotificationCategories(categorySet)
In Objective-C:
// Get the notification center object UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter]; // Register a delegate (see next step for delegate implementation) [center setDelegate:self]; // Request authorization [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) { // Enable or disable features based on authorization }]; // Create general category UNNotificationCategory* generalCategory = [UNNotificationCategory categoryWithIdentifier:@"GENERAL" actions:@[] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction]; // Set category [center setNotificationCategories:[NSSet setWithObjects:generalCategory, nil]];
-
Implement UNUserNotificationCenterDelegate.
Handle the didReceiveNotificationResponse and
willPresentNotification methods.
iOS calls the didReceiveNotificationResponse method when your app is in the background. In this method, you can tell your app to enter the foreground and for chat to maximize. To perform this behavior, use the handle(notification:) method on the SCSChatInterface object, which is accessible from the chatUI property of the ServiceCloud shared instance.
iOS calls the willPresentNotification method when your app is in the foreground. To determine whether to display the notification, use the shouldDisplayNotificationInForeground method (accessible from the chatUI property of the ServiceCloud shared instance). If the Chat UI is already showing the relevant information related to this event, the method returns false.
In Swift:
/** This delegate method is executed when the application launches as a result of the user interacting with a notification when it is in the background. The result of passing the notification to Chat is that we will maximize if the notification was scheduled as a result of a chat event. */ func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { let chat = ServiceCloud.shared().chatUI! chat.handle(response.notification) } /** This delegate method is executed when a notification is received while the app is in the foreground. Check with Chat to see whether it's appropriate to display the notification (or if the notification relates to information already being shown to the user). */ func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { let chat = ServiceCloud.shared().chatUI! // Show we display this notification? if (chat.shouldDisplayNotificationInForeground()) { // Display notification as an alert completionHandler(.alert) } }
In Objective-C:
/** This delegate method is executed when the application launches as a result of the user interacting with a notification when it is in the background. The result of passing the notification to Chat is that we will maximize if the notification was scheduled as a result of a chat event. */ - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler { SCSChatInterface *chat = [SCServiceCloud sharedInstance].chatUI; [chat handleNotification:response.notification]; } /** This delegate method is executed when a notification is received while the app is in the foreground. Check with Chat to see whether it's appropriate to display the notification (or if the notification relates to information already being shown to the user). */ - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler: (void (^)(UNNotificationPresentationOptions options))completionHandler { SCSChatInterface *chat = [SCServiceCloud sharedInstance].chatUI; // Show we display this notification? if ([chat shouldDisplayNotificationInForeground]) { // Display notification as an alert completionHandler(UNNotificationPresentationOptionAlert); } }