# Communication Notifications

Enhance your notifications with Communication Notifications in a few easy steps.

Communication Notifications Difference

# Xcode Capabilities

First, add Communication Notifications to your project in the Capabilities section.

Xcode Capabilities Settings

# Info.plist

Navigate to the Info.plist file of your app and add the key 'NSUserActivityTypes' with an array containing the value 'INSendMessageIntent'.

<key>NSUserActivityTypes</key>
<array>
    <string>INSendMessageIntent</string>
</array>

# Add Notification Service Extension

  1. Select File > New > Target in Xcode.
  2. Select the Notification Service Extension target from the iOS > Application section.
  3. Click Next.
  4. Specify a name and other configuration details for your app extension.
  5. Click Finish.
  6. Add Smartsupp.framework in General -> Frameworks and Libraries and set it to Do Not Embed

Once you've added the notification service extension, enabling you to modify incoming notification content, open the newly created file NotificationService.swift. Simply subclass it from SmartsuppNotificationServiceExtension and remove the prepared overridden methods. The file should now look something like this:

import Smartsupp

class NotificationService: SmartsuppNotificationServiceExtension {

}

# My App Already Uses the Notification Service Extension

If you're already utilizing the notification service extension, you can instantiate SmartsuppNotificationContentManager, responsible for modifying notification content. If successful, retain the reference to the object to prevent immediate deallocation. If initialization returns nil, indicating the notification isn't from Smartsupp, proceed with your code to modify the notification. Additionally, remember to invoke serviceExtensionTimeWillExpire() within the extension's serviceExtensionTimeWillExpire() method. Your implementation may look something like the following:

import Smartsupp
import UserNotifications

class MyAppNotificationServiceExtension: UNNotificationServiceExtension {
    private var contentHandler: ((UNNotificationContent) -> Void)?
    private var bestAttemptContent: UNNotificationContent?
    
    private var contentManager: SmartsuppNotificationContentManager?
    
    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        if let contentManager = SmartsuppNotificationContentManager(request, withContentHandler: contentHandler) {
            self.contentManager = contentManager
        } else {
            self.contentHandler = contentHandler
            self.bestAttemptContent = request.content
            
            // ...
            // Do your modifications of the notification's content here
            // ...
            
            contentHandler(request.content)
        }
    }
    
    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 contentManager {
            contentManager.serviceExtensionTimeWillExpire()
        } else if let content = bestAttemptContent {
            contentHandler?(content)
        }
    }

}