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:
1import UserNotificationsIn Objective-C:
1@import UserNotifications; -
Register for local notifications in your AppDelegate's didFinishLaunchingWithOptions method.
In Swift:
1// Get the notification center object 2let center = UNUserNotificationCenter.current() 3 4// Register a delegate (see next step for delegate implementation) 5center.delegate = self 6 7// Request authorization 8center.requestAuthorization(options: [.alert,.sound], 9 completionHandler: { granted, error in 10 // Enable or disable features based on authorization 11}) 12 13// Create general category 14let generalCategory = UNNotificationCategory(identifier: "General", actions: [], 15intentIdentifiers: [], options: .customDismissAction) 16let categorySet: Set<UNNotificationCategory> = [generalCategory] 17 18// Set category 19center.setNotificationCategories(categorySet)In Objective-C:
1// Get the notification center object 2UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter]; 3 4// Register a delegate (see next step for delegate implementation) 5[center setDelegate:self]; 6 7// Request authorization 8[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound) 9 completionHandler:^(BOOL granted, NSError * _Nullable error) { 10 // Enable or disable features based on authorization 11}]; 12 13// Create general category 14UNNotificationCategory* generalCategory = 15 [UNNotificationCategory 16 categoryWithIdentifier:@"GENERAL" 17 actions:@[] 18 intentIdentifiers:@[] 19 options:UNNotificationCategoryOptionCustomDismissAction]; 20 21// Set category 22[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:
1/** 2 This delegate method is executed when the application launches as a result 3 of the user interacting with a notification when it is in the background. 4 The result of passing the notification to Chat is that we will 5 maximize if the notification was scheduled as a result of a chat event. 6*/ 7func userNotificationCenter(_ center: UNUserNotificationCenter, 8 didReceive response: UNNotificationResponse, 9 withCompletionHandler completionHandler: 10 @escaping () -> Void) { 11 12 let chat = ServiceCloud.shared().chatUI! 13 chat.handle(response.notification) 14} 15 16/** 17 This delegate method is executed when a notification is received while 18 the app is in the foreground. Check with Chat to see whether it's 19 appropriate to display the notification (or if the notification 20 relates to information already being shown to the user). 21*/ 22func userNotificationCenter(_ center: UNUserNotificationCenter, 23 willPresent notification: UNNotification, 24 withCompletionHandler completionHandler: 25 @escaping (UNNotificationPresentationOptions) -> Void) { 26 27 let chat = ServiceCloud.shared().chatUI! 28 29 // Show we display this notification? 30 if (chat.shouldDisplayNotificationInForeground()) { 31 32 // Display notification as an alert 33 completionHandler(.alert) 34 } 35}In Objective-C:
1/** 2 This delegate method is executed when the application launches as a result 3 of the user interacting with a notification when it is in the background. 4 The result of passing the notification to Chat is that we will 5 maximize if the notification was scheduled as a result of a chat event. 6*/ 7- (void)userNotificationCenter:(UNUserNotificationCenter *)center 8didReceiveNotificationResponse:(UNNotificationResponse *)response 9 withCompletionHandler:(void(^)(void))completionHandler { 10 11 SCSChatInterface *chat = [SCServiceCloud sharedInstance].chatUI; 12 [chat handleNotification:response.notification]; 13} 14 15/** 16 This delegate method is executed when a notification is received while 17 the app is in the foreground. Check with Chat to see whether it's 18 appropriate to display the notification (or if the notification 19 relates to information already being shown to the user). 20*/ 21- (void)userNotificationCenter:(UNUserNotificationCenter *)center 22 willPresentNotification:(UNNotification *)notification 23 withCompletionHandler: 24 (void (^)(UNNotificationPresentationOptions options))completionHandler { 25 26 SCSChatInterface *chat = [SCServiceCloud sharedInstance].chatUI; 27 28 // Show we display this notification? 29 if ([chat shouldDisplayNotificationInForeground]) { 30 31 // Display notification as an alert 32 completionHandler(UNNotificationPresentationOptionAlert); 33 } 34}