Configure the Service Extension for the iOS Extension SDK

Setup and configure the Notification Service Extension for use with the iOS Extension SDK.

The notification service app extension is a bundle within your main app. To add a service extension target, complete these steps.

  1. In Xcode, go to File > New > Target.

  2. In the iOS > Application Extension section, select Notification Service Extension.

  3. Click Next.

  4. Configure the app extension and click Finish.

  5. In the General Settings section for your project target, verify that the new extension is listed under Frameworks, Libraries, and Embedded Content. If it isn’t listed there, add it.

    Use the same Xcode-managed profile for the extension targets as the main project. Match the service extension version to the main app version whenever possible, and prefix the service extension bundle ID with the main app’s bundle ID. For example, if the main app’s bundle ID is com.salesforce.MyAwesomeApp, the service extension bundle ID can be com.salesforce.MyAwesomeApp.MyServiceExtension.

To integrate the Service Extension, add the dependencies to CocoaPods or Swift Package Manager (SPM). You can also add the dependencies manually.

If you use CocoaPods to manage dependencies for your app, add the extension SDK to your Podfile.

  1. Update your Podfile to include the extension SDK.
  1. In your project directory, run pod install.
  2. Open the .xcworkspace file that CocoaPods generates.

Don’t open the .xcodeproj file directly. Opening a project file instead of a workspace can lead to errors.

For more information about updating Podfiles, see Using CocoaPods: Adding pods to an Xcode project on the CocoaPods documentation site.

If you use Swift Package Manager (SPM) to manage dependencies for your app, add the extension SDK to your Podfile.

  1. On the Package Dependencies tab of your project’s settings, click the plus sign (+) to add a package.
  2. Search for the package MCExtensionSDK using the URL https://github.com/salesforce-marketingcloud/extension-sdk-ios.git
  3. Select the package to include it in your app.

If you don’t use CocoaPods or Swift Package Manager, you can add the extension packages manually.

  1. Download the MCExtensionSDK.

  2. Copy the MCExtensionSDK directory from your downloads folder to your project directory.

    To keep the binary in a different location, adjust the Framework Search Path (FRAMEWORK_SEARCH_PATHS) in your build settings.

  3. In your project, select the Service Extension target

  4. In the General Settings for your project, in the Frameworks, Libraries, and Embedded Content section, add the MCExtensionSDK framework.

After you add the dependencies to your project, inherit the main class of the Service Extension from the SFMCNotificationService class.

If your project is written in Swift, use this code to inherit the service.

If your project uses ObjectiveC, use this header.

In your ObjectiveC implementation file, use this code.

Don’t implement any UNNotificationServiceExtension methods, such as func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) → Void) or open func serviceExtensionTimeWillExpire().

The MCExtensionSDK manages the UNNotificationServiceExtension lifecycle methods. You can customize some of the ways the extension behaves.

  • Enabling or disabling logging and configuring log levels
  • Downloading and attaching images or videos to push notifications
  • Adding custom key-value pairs to the notification’s userInfo
  • Performing other necessary operations

To enable or disable logging and configure log levels, use this code to override the sfmcProvideConfig() method:

To enable or disable the first image of the Carousel template as a thumbnail, use this code to override the sfmcProvideConfig() method.

Next, set the value of the shouldShowCarouselThumbnail property. This property is true by default.

This property is available in version 9.0.1 and later.

To execute custom code, override func sfmcDidReceive(_ request: UNNotificationRequest, mutableContent: UNMutableNotificationContent, withContentHandler contentHandler: @escaping ([AnyHashable : Any]?) → Void) to process notifications, such as downloading media or adding custom key-value pairs.

This code example depicts a Swift-based implementation where SFMCNotificationService is extended to configure logging through sfmcProvideConfig() and customize push notification handling in sfmcDidReceive(_:mutableContent:withContentHandler:), allowing for operations like adding custom key-value pairs.

View this code example

This example shows an Objective-C-based implementation where SFMCNotificationService is extended to configure logging and handle custom push notification processing.

View this code example