Notification Service Extension

Create and configure a Notification Service Extension to support rich media attachments like images, animated GIFs, and videos in push notifications.

To support rich media attachments (images, animated GIFs, video) in push notifications, you need to create a notification service extension.

Create a Notification Service Extension Target

  1. In Xcode, click FileNewTarget….
  2. Select Notification Service Extension.
  3. Click Next and configure your extension:
    • Product Name: Your extension name (e.g., NotificationServiceExtension)
    • Bundle Identifier: Typically your app’s bundle ID with a suffix (e.g., com.example.app.NotificationServiceExtension)
  1. Verify that your app target’s Embed App Extensions includes the newly created extension.

Install Dependencies

Install Notification Service Extension

  1. Select your service extension target in the Project Navigator.
  2. Go to the Package Dependencies tab.
  3. If you haven’t already added the Airship package, click + and add: https://github.com/urbanairship/ios-library
  4. Select the AirshipNotificationServiceExtension package product for your service extension target.
 Note

The AirshipNotificationServiceExtension package should only be added to the Notification Service Extension target, not the main app target.

  1. Import the module:
import AirshipNotificationServiceExtension

Add to your Podfile:

target "<Your Service Extension Target Name>" do
  pod 'AirshipServiceExtension'
end

Install:

$ pod install
  1. Add AirshipNotificationServiceExtension.framework to your service extension target following Carthage’s instructions.
  2. Add to your Cartfile:
github "urbanairship/ios-library"
  1. Build:
$ carthage update
  1. Verify that Enable Modules and Link Frameworks Automatically are enabled in Build Settings.
  1. Download the latest iOS SDK release.
  2. Add AirshipNotificationServiceExtension.xcframework to your service extension target:
    • Select your service extension target
    • Go to GeneralFrameworks, Libraries, and Embedded Content
    • Drag in AirshipNotificationServiceExtension.xcframework
  3. Verify Build Settings:
    • Enable Modules: Yes
    • Link Frameworks Automatically: Yes

Implement the Service Extension

Replace the default NotificationService implementation with Airship’s base class:

Service Extension Implementation

import AirshipNotificationServiceExtension

class NotificationService: UANotificationServiceExtension {

}
import AirshipServiceExtension

class NotificationService: UANotificationServiceExtension {

}
// NotificationService.h
@import AirshipNotificationServiceExtension;

@interface NotificationService : UANotificationServiceExtension

@end

// NotificationService.m
@import Foundation;
#import "NotificationService.h"

@implementation NotificationService

@end
// NotificationService.h
@import AirshipServiceExtension;

@interface NotificationService : UANotificationServiceExtension

@end

// NotificationService.m
@import Foundation;
#import "NotificationService.h"

@implementation NotificationService

@end

That’s it! The Airship base class handles downloading and attaching media from URLs in your push notifications. When you send a push notification with a media URL, the service extension will automatically download and attach the media before the notification is displayed.