
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 1.6 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.
You'll require Xcode 8 or greater to perform this integration.
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 3.

Xcode will then ask you if you want to activate the scheme. Press Activate.
Note: Cocoapods support is coming soon
First, download the SDK. Then, include BatchExtension.framework in your extension target, by drag and dropping it into the left sidebar:

Check Copy items if needed, make sure that your extension is selected as a target and finish the wizard.
Open the project from the sidebar, pick your main application target, and select the Build Phases tab.

From there, you need to:
+ button, and select New Copy Files PhasesFrameworksBatchExtension.framework from the sidebar, and drop it in the newly added sectionOnce that's done, add a new Run Script Phase and put the following text in the box:
bash "${BUILT_PRODUCTS_DIR}/${FRAMEWORKS_FOLDER_PATH}/BatchExtension.framework/strip-frameworks.sh"

This step is required to work around a Xcode App Store submission bug with universal frameworks.
Note: If compilation fails, make sure you added these two steps to the main application target, and not the extension.
If you don't have your own code, you've probably noticed that Xcode added some sample code for you:

In order to have Batch automatically adding rich content to your notifications, simply remplace this code by:
// NotificationService.h --
@import BatchExtension;
@interface NotificationService : BAENotificationServiceExtension
@end
// NotificationService.m
#import "NotificationService.h"
@implementation NotificationService
@end
import BatchExtension
class NotificationService: BAENotificationServiceExtension {
}
That's it, no code to write! Start your app, and try sending a rich push from the dashboard.
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;
or
#import <BatchExtension/BatchExtension.h>
import BatchExtension
Then, instanciate a BAERichNotificationHelper instance, and keep it as an instance variable of your UNNotificationServiceExtension instance.
Note: You must NOT instanciate a new
BAERichNotificationHelperobject every time. The class needs to keep an internal state, and might not behave properly if it cannot.
You can then use the following methods of BAERichNotificationHelper:
- 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:
// NotificationService.h
#import <UserNotifications/UserNotifications.h>
@interface NotificationService : UNNotificationServiceExtension
@end
// NotificationService.m
#import "NotificationService.h"
@import BatchExtension;
@interface NotificationService () {
BAERichNotificationHelper *batchHelper;
}
@end
@implementation NotificationService
- (instancetype)init {
self = [super init];
if (self) {
batchHelper = [BAERichNotificationHelper new];
}
return self;
}
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
UNMutableNotificationContent *bestAttemptContent = [request.content mutableCopy];
if (bestAttemptContent) {
// Modify the notification content here...
bestAttemptContent.title = [bestAttemptContent.title stringByAppendingString:@" [modified]"];
// Ask Batch to download and add any rich content
[batchHelper appendRichDataToContent:bestAttemptContent completionHandler:^(UNNotificationContent * _Nullable result, NSError * _Nullable error) {
if (error) {
NSLog(@"Error while appending rich notification attachments %@", error);
}
if (result) {
contentHandler(result);
} else {
contentHandler(bestAttemptContent);
}
}];
} else {
contentHandler(bestAttemptContent);
}
}
@end
class NotificationService: UNNotificationServiceExtension {
let batchHelper = BAERichNotificationHelper()
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(to: 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)
}
}
}