# Rich notifications setup

![Rich notification example](https://38998153-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCL8wF0y1T2vLnm3yR2MW%2Fuploads%2FXYiig9BzUVZaPDdzsujf%2Fios10_expanded_notif.jpg?alt=media\&token=7f44fa51-f21a-47ed-b0e0-1e8918c8d8ff)

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.

{% hint style="info" %}
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](#integrating-batch-extension-sdk).
{% endhint %}

### 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](https://38998153-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCL8wF0y1T2vLnm3yR2MW%2Fuploads%2Fw7OuyEQcu9neBmslXAiI%2Fxcode_target_extension.png?alt=media\&token=511cf316-6a4e-45a0-8d8e-f35f621fb3af)

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](https://38998153-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCL8wF0y1T2vLnm3yR2MW%2Fuploads%2Fk2eL2z6gyLWNEBvY9tmC%2Fxcode_deployment_target.png?alt=media\&token=c8657ce5-eddb-4d85-bc90-23cc5c66b67d)

### Integrating Batch Extension SDK

#### Framework integration

{% tabs %}
{% tab title="Swift Package Manager (recommended)" %}
BatchExtension is distributed as an [open-source Swift package](https://github.com/BatchLabs/Batch-iOS-SDK-Extension).

Add it using Xcode with its repository URL:

{% code title="Package.swift" %}

```
https://github.com/BatchLabs/Batch-iOS-SDK-Extension.git
```

{% endcode %}

{% hint style="info" %}
**Note:** If you're getting the error: "Cannot subclass a class that was declared with the 'objc\_subclassing\_restricted' attribute", this is because Swift classes cannot be subclassed in Objective-C, and the extension code is pure Swift and SPM doesn't support mixed language packages. To fix this please use the [Pure Objective-C version](https://github.com/BatchLabs/Batch-iOS-SDK-Extension-ObjC?tab=readme-ov-file) if you wish to use SPM, use CocoaPods/Carthage or rewrite your extension code in Swift.
{% endhint %}
{% endtab %}

{% tab title="CocoaPods" %}
Make sure you're setting the dependency on the extension target. If your Podfile existed before you created your extension, you might need to add it.

{% code title="Podfile" %}

```ruby
target 'RichNotifications' do
  use_frameworks!
  pod 'BatchExtension'
end

target 'App' do
  use_frameworks!
  pod 'Batch'
end
```

{% endcode %}
{% endtab %}

{% tab title="Carthage" %}
{% code title="Cartfile" %}

```shell
github "BatchLabs/Batch-iOS-SDK-Extension"
```

{% endcode %}

Then, link the framework to the extension just like you would with any Carthage dependency.
{% endtab %}
{% endtabs %}

#### 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](https://38998153-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCL8wF0y1T2vLnm3yR2MW%2Fuploads%2FLZt1w1TnBZCmj0BbdTh5%2Fextension_default_template.png?alt=media\&token=33505168-8f4f-4f3d-9c4d-f91467b546f5)

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

{% tabs %}
{% tab title="Swift" %}

```swift
import BatchExtension

class NotificationService: BAENotificationServiceExtension {
    
}
```

{% endtab %}

{% tab title="Objective-C" %}

```objectivec
// NotificationService.h -- 
@import BatchExtension;

@interface NotificationService : BAENotificationServiceExtension
@end

// NotificationService.m
#import "NotificationService.h"
@implementation NotificationService
@end

```

{% endtab %}
{% endtabs %}

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:

{% tabs %}
{% tab title="Swift" %}

```swift
import BatchExtension
```

{% endtab %}

{% tab title="Objective-C" %}

```objectivec
@import BatchExtension;

or

#import <BatchExtension/BatchExtension.h>
```

{% endtab %}
{% endtabs %}

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

{% hint style="warning" %}
Note: You must NOT instanciate a new `BAERichNotificationHelper/RichNotificationHelper` object every time. The class needs to keep an internal state, and might not behave properly if it cannot.
{% endhint %}

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`:

{% tabs %}
{% tab title="Swift" %}

```swift
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)
        }
    }
}
```

{% endtab %}

{% tab title="Objective-C" %}

```objectivec
// 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

```

{% endtab %}
{% endtabs %}

### 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`:

{% tabs %}
{% tab title="Swift" %}

```swift
import UserNotifications
import BatchExtension

class NotificationService: UNNotificationServiceExtension {

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

{% endtab %}

{% tab title="Objective-C" %}

```objectivec
// NotificationService.m
#import "NotificationService.h"

@implementation NotificationService

- (instancetype)init
{
    self = [super init];
    if (self) {
        BAERichNotificationHelper.allowInLowDataMode = true;
    }
    return self;
}

@end

```

{% endtab %}
{% endtabs %}
