> ## Documentation Index
> Fetch the complete documentation index at: https://docs.castled.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Rich Push Notification

## Overview

With the introduction of iOS 10, rich push notifications have made their way into the iOS ecosystem. This exciting feature allows you to enhance your push notifications by incorporating engaging multimedia elements such as images, videos, audio, or GIFs.

Rich push notifications offer a convenient way to provide users with quick interactions that don't require them to launch the entire app. This highlights the increasing significance of notifications in shaping an app's overall user experience.

Rich push notifications leverage the power of [Notification Service](https://developer.apple.com/documentation/usernotifications/unnotificationserviceextension) and [Notification Content](https://developer.apple.com/documentation/usernotificationsui/unnotificationcontentextension) extensions to enrich the user experience. These extensions are separate and unique components embedded within your app bundle.

## Enabling Rich Push Notification support

When a new push notification arrives, the system first invokes your notification service extension to customize the payload and include media attachments for display. The notification content extension comes into play by providing a custom interface to present your app's notifications in a visually appealing and interactive manner. Together, these extensions work seamlessly to deliver captivating and dynamic push notifications to your app's users.

### Adding Notification Service Extension

**Step 1: Adding the Notification Service Extension to your Xcode Project**

1. Open your Xcode project.
2. Go to File -> New -> Target.
3. Select "Notification Service Extension" under the "iOS" category.
   ![](https://mintlify.s3-us-west-1.amazonaws.com/castled/images/NSE-1.png)
4. Click "Next" and provide a name for your extension (e.g., "MyNotificationServiceExtension").
   ![](https://mintlify.s3-us-west-1.amazonaws.com/castled/images/NSE-2.png)
5. Choose the appropriate settings for your extension and click "Finish".
6. Xcode will generate the necessary files for the Notification Service Extension.

**Step 2: Importing the CastledNotificationService Framework**

Import the CastledNotificationService framework by adding the below entry in the Podfile

```Podfile
target 'YourNotificationServiceExtension' do
  use_frameworks!
   pod 'CastledNotificationService'
end
```

**Step 3: Set the App Group ID**

**3.1** First, set the App Groups for the main target by referring [here](https://docs.castled.io/developer-resources/sdk-integration/ios/push-notifications.md#3-adding-an-app-group-id).

**3.2** Enable App Groups for your notification service extension target and use the same App Group ID that was selected for the app target (as mentioned in the previous steps).

![](https://mintlify.s3-us-west-1.amazonaws.com/castled/images/push/ios/dark/service_appgroup1.png)

**Step 4: Implementing the Notification Service Extension**

In your Notification Service Extension code file, inherit from the **CastledNotificationServiceExtension** class provided by the CastledNotificationService framework.

Customize the notification handling logic in the overridden methods of MyNotificationServiceExtension class.

Override the **didReceive(\_:withContentHandler:)** method to modify the notification content if needed.

Override the **serviceExtensionTimeWillExpire()** method to handle situations when the service extension time is about to expire.

```swift swift
import UserNotifications
import CastledNotificationService

class NotificationService: CastledNotificationServiceExtension {

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        appGroupId = "<your_app_group_id>"
        //calling super to make sure Castled implementation is called.
        super.didReceive(request, withContentHandler: contentHandler)
    }

    override func serviceExtensionTimeWillExpire() {
        // This method is called right before the extension is terminated by the system.
        // Take this opportunity to provide your "best attempt" at modified content.
        // If you don't make any changes, the original push payload will be used by default.
        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {           
            contentHandler(bestAttemptContent)         
        }     
    }   
}
```

```objc objc
// NotificationService.h
#import <UserNotifications/UserNotifications.h>
#import <CastledNotificationService/CastledNotificationServiceObjC.h>

@interface NotificationService : CastledNotificationServiceObjC
@end

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

@implementation NotificationService

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.appGroupId = @"<your_app_group_id>";
    [super didReceiveNotificationRequest:request withContentHandler:contentHandler];
}

- (void)serviceExtensionTimeWillExpire {
    [super serviceExtensionTimeWillExpire];
}

@end
```

Please refer to the official Apple documentation for further details on working with [Notification Service Extensions](https://developer.apple.com/documentation/usernotifications/unnotificationserviceextension)

### Adding Notification Content Extension

**Step 1: Adding the Notification Content Extension to your Xcode Project**

1. Open your Xcode project.
2. Go to File -> New -> Target.
3. Select "Notification Content Extension" under the "iOS" category.
   ![](https://mintlify.s3-us-west-1.amazonaws.com/castled/images/NCE-1.png)
4. Click "Next" and provide a name for your extension (e.g., "MyNotificationContent").
   ![](https://mintlify.s3-us-west-1.amazonaws.com/castled/images/NCE-2.png)
5. Choose the appropriate settings for your extension and click "Finish".
6. Xcode will generate the necessary files for the Notification Service Extension.
   ![](https://mintlify.s3-us-west-1.amazonaws.com/castled/images/NCE-3.png)

**Step 2: Configuring the Info.plist File**

1. Open the Info.plist file of your Notification Content Extension target.
2. Add the following key-value pair:

| Key | Value |
| - | - |
| UNNotificationExtensionCategory | CASTLED\_PUSH\_TEMPLATE |
| UNNotificationExtensionInitialContentSizeRatio | 1 |
| UNNotificationExtensionDefaultContentHidden | YES |
| UNNotificationExtensionUserInteractionEnabled | YES |

![](https://mintlify.s3-us-west-1.amazonaws.com/castled/images/push/ios/dark/noti_content_plist.png)

> [!NOTE]
>
> Configure the Notification Categories & actions and use the same for configuring UNNotificationExtensionCategory.
> The required categories can be added as an array.

**Step 3: Importing the CastledNotificationContent Framework**

Import the CastledNotificationContent framework by adding the below entry in the Podfile

```Podfile
target 'YourNotificationContentExtension' do
  use_frameworks!
   pod 'CastledNotificationContent'
end
```

**Step 4: Set the App Group ID**

**4.1** First, set the App Groups for the main target by referring [here](https://docs.castled.io/developer-resources/sdk-integration/ios/push-notifications.md#3-adding-an-app-group-id).

**4.2** Enable App Groups for your notification content extension target and use the same App Group ID that was selected for the app target (as mentioned in the previous steps).

![](https://mintlify.s3-us-west-1.amazonaws.com/castled/images/push/ios/dark/content_appgroup1.png)

**Step 5: Implementing the Notification Content Extension**

In your NotificationViewController file, inherit from the **CastledNotificationViewController** class provided by the CastledNotificationContent framework.

Customize the notification handling logic in the overridden methods of NotificationViewController class.

Override the **viewDidLoad()** and **didReceive()** methods to modify the notification content if needed.

```swift swift
import UIKit
import Castled
import UserNotifications
import UserNotificationsUI
import CastledNotificationContent

class NotificationViewController: CastledNotificationViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        appGroupId = "<your_app_group_id>"
        // Do any required interface initialization here.
    }
    
    override func didReceive(_ notification: UNNotification) {
        super.didReceive(notification)
    }
    
}
```

```objc objc
// NotificationViewController.h
#import <UIKit/UIKit.h>
#import <CastledNotificationContent/CastledNotificationViewControllerObjC.h>

@interface NotificationViewController : CastledNotificationViewControllerObjC

@end

// NotificationViewController.m
#import "NotificationViewController.h"
#import <UserNotifications/UserNotifications.h>
#import <UserNotificationsUI/UserNotificationsUI.h>

@interface NotificationViewController () <UNNotificationContentExtension>
@property IBOutlet UILabel *label;
@end

@implementation NotificationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.appGroupId = @"<your_app_group_id>";
    // Do any required interface initialization here.
}

- (void)didReceiveNotification:(UNNotification *)notification {
    [super didReceiveNotification:notification];
}

@end
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.