Rich notifications setup

Rich notification example

iOS 10 introduced support for rich notifications: they can now contain custom content, such as images, videos, sounds or even a fully custom view controller. Batch comes with built-in support for these, but due to the way they're implemented, integration of a supplementary SDK is required. Don't worry, we've made it really easy.

Note: This tutorial assumes that you haven't already added a Notification Content extension. If you do, jump straight to Integrating the Batch Extension SDK.

Adding a Notification Service Extension

In order to set up the Batch Extension SDK, you'll need a notification service extension. It's a standard iOS component that will take care of downloading rich content and add it to the notification.

Open your Xcode project, click on the File menu and then pick New -> Target. Then, pick Notification Service Extension and fill in what's left of the wizard. You can name the extension as you wish: we will name it RichNotificationsExtension for the rest of this tutorial, and write it in Swift.

Xcode target wizard screenshot

Xcode will then ask you if you want to activate the scheme. Press Activate.

Before going any further, you might want to check the extension's Deployment Traget. It usually is the latest iOS minor, meaning that your extension will not run on older iOS versions. We recommend that you set it to the lowest version of iOS that your app supports, but not lower than iOS 10.0 as this is the version that introduced this extension kind.

Xcode deployment target

Integrating Batch Extension SDK

Framework integration

Automatic integration

If you don't have your own code, you've probably noticed that Xcode added some sample code for you:

Default extension code

In order to have Batch automatically adding rich content to your notifications, simply remplace this code with:

import BatchExtension

class NotificationService: BAENotificationServiceExtension {
    
}

That's it, no code to write! Start your app, and try sending a rich push from the dashboard.

Manual integration

If you've already added your own extension code, you might want to manually integrate Batch and perform your own modifications to the notification content.

First, import the extension SDK:

import BatchExtension

Then, instanciate a BAERichNotificationHelper/RichNotificationHelper instance, and keep it as an instance variable of your UNNotificationServiceExtension instance.

You can then use the following methods of BAERichNotificationHelper/RichNotificationHelper:

  • didReceive, which has the same signature as the one you're already in, but allows you to tweak the UNNotificationRequest beforehand

  • appendRichData, which will download and add attachments to the content, and call you back once done.

Here's an example of a class that uses appendRichData:

class NotificationService: UNNotificationServiceExtension {

    let batchHelper = RichNotificationHelper()
    
    var bestAttemptContent: UNMutableNotificationContent?

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        
        if let bestAttemptContent = request.content.mutableCopy() as? UNMutableNotificationContent {
            // Modify the notification content here...
            bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"
            
            // Ask Batch to download and add any rich content
            batchHelper.appendRichData(toContent: bestAttemptContent, completionHandler: { (content: UNNotificationContent?, err: Error?) in
                if let err = err {
                    print("Error while appending rich notification attachments \(err)")
                }
                contentHandler(content ?? bestAttemptContent)
            })
        } else {
            contentHandler(request.content)
        }
    }
}

Configuring low data mode

Starting with version 3.0.0, BatchExtension doesn't download rich notification content in low data mode anymore.

To change this, use BAERichNotificationHelper/RichNotificationHelper:

import UserNotifications
import BatchExtension

class NotificationService: UNNotificationServiceExtension {

    override init() {
        super.init()
        RichNotificationHelper.allowInLowDataMode = true
    }
    
}

Last updated

Was this helpful?