Configure the Content Extension for the iOS Extension SDK

Set up and configure the Notification Content Extension for use with the iOS Extension for the Salesforce Engagement SDK.

Add a Content Extension Target 

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

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

  2. In the iOS > Application Extension section, select Notification Content 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 content extension version to the main app version whenever possible, and prefix the content extension bundle ID with the main app’s bundle ID. For example, if the main app’s bundle ID is com.salesforce.MyAwesomeApp, the content extension bundle ID can be com.salesforce.MyAwesomeApp.MyContentExtension.

    Important

Integrate the Extension SDK with the Content Extension 

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

Integrate the Extension SDK with CocoaPods 

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.
1target ‘<appTarget>do
2  use_frameworks!
3  pod ‘MarketingCloudSDK’
4end
5
6target ‘<appExtensionTarget>do
7  use_frameworks!
8  pod ‘MarketingCloud-ExtensionSDK’
9end
  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.

Important

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

Integrate the Extension SDK with Swift Package Manager 

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

  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.

Integrate the Extension SDK Manually 

If you don’t use CocoaPods or Swift Package Manager, 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.

    Note

  3. In your project, select the Content Extension target.

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

Inherit from SFMCNotificationViewController 

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

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

1import UIKit
2import UserNotifications
3import UserNotificationsUI
4import MCExtensionSDK
5
6class NotificationViewController: SFMCNotificationViewController { }

If your project uses ObjectiveC, use this header.

1#import <UIKit/UIKit.h>
2#import <MCExtensionSDK/MCExtensionSDK.h>
3
4@interface NotificationViewController : SFMCNotificationViewController
5
6@end

In your ObjectiveC implementation file, use this code.

1#import "NotificationViewController.h"
2
3@implementation NotificationViewController
4
5@end

SFMCNotificationViewController fully manages UI rendering. Don’t implement or override any UIViewController methods in the principal class of the content extension, as doing so can interfere with UI rendering.

Warning

Configure Info.plist Settings 

Configure your content extension’s Info.plist file to register the correct notification category, build the rich push notification UI in code using the Extension SDK, and enable user interaction. These steps make sure that the content extension is correctly triggered and displays the intended rich UI as defined in Marketing Cloud Engagement.

Set Up the Extension Category 

To set up and synchronize the notification category between your content extension’s Info.plist file and the Marketing Cloud Engagement UI:

  1. Contact your Marketing Cloud Engagement admin to obtain the correct category name for the Rich UI template.
  2. In your content extension’s Info.plist file, replace the value for UNNotificationExtensionCategory under NSExtension > NSExtensionAttributes.

iOS activates the content extension only if the category value in the push notification payload matches the one defined in Info.plist > NSExtension > NSExtensionAttributes > UNNotificationExtensionCategory. Make sure that the category value in Marketing Cloud Engagement matches the value in your content extension’s Info.plist file. Otherwise, the Carousel template won’t show.

Warning

Build the UI Programmatically 

Unlike Apple’s auto-generated content extension template, MCExtensionSDK builds the notification UI programmatically instead of using an Interface Builder file. To modify the project to build the rich push notification UI entirely in code using the MCExtensionSDK:

  1. Remove the storyboard reference by deleting the NSExtensionMainStoryboard item from Info.plist > NSExtension.

  2. Add the NSExtensionPrincipalClass key to Info.plist > NSExtension

    • Value type: String
    • Value: $(PRODUCT_MODULE_NAME).<MainClassName>

    For Objective-C projects, specify the class name directly.

    Note

  3. Remove MainInterface.storyboard from your project.

Enable User Interaction 

Add this key to your content extension’s Info.plist file:

  • Key: UNNotificationExtensionUserInteractionEnabled
  • Value type: Boolean
  • Value: YES

Additional Configuration Options 

The MCExtensionSDK manages the content extension lifecycle. You can customize some of the ways the extension behaves:

  • Enabling or disabling logging and configuring log levels
  • Adjusting the HTTP request timeout

Configure Logging and Timeout 

If your project is written in Swift, implement the sfmcProvideConfig() method to define custom log levels and HTTP timeout settings for the extension.

1import UIKit
2import UserNotifications
3import UserNotificationsUI
4import MCExtensionSDK
5
6class NotificationViewController: SFMCNotificationViewController {
7
8    override func sfmcProvideConfig() -> SFContentExtensionConfig {
9        var logLevel: LogLevel = .none
10#if DEBUG
11        logLevel = .debug
12#endif
13        return SFContentExtensionConfig(logLevel: logLevel, timeoutIntervalForRequest: 30.0)
14    }
15}

If your project uses ObjectiveC, use the sfmcProvideConfig method to configure logging behavior and HTTP request timeout values for the extension.

1#import "NotificationViewController.h"
2
3@implementation NotificationViewController
4
5- (SFMCContentExtensionConfig *)sfmcProvideConfig {
6    SFMCExtensionSdkLogLevel logLevel = SFMCExtensionSdkLogLevelNone;
7#if DEBUG
8    logLevel = SFMCExtensionSdkLogLevelDebug;
9#endif
10    return [[SFMCContentExtensionConfig alloc] initWithLogLevel:logLevel timeoutIntervalForRequest:30.0];
11}
12
13@end

The default timeout for network requests is 15 seconds. Additionally, iOS blocks insecure (non-HTTPS) URLs by default. To allow loading Carousel images from HTTP URLs, configure an exception.

Note