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 and Notification Content 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
- Open your Xcode project.
- Go to File -> New -> Target.
- Select “Notification Service Extension” under the “iOS” category.
- Click “Next” and provide a name for your extension (e.g., “MyNotificationServiceExtension”).
- Choose the appropriate settings for your extension and click “Finish”.
- Xcode will generate the necessary files for the Notification Service Extension.
Step 2: Configuring the Info.plist File
- Open the Info.plist file of your Notification Service Extension target.
- Add the following key-value pair:
Key | Value |
---|---|
NSExtensionPrincipalClass | $(PRODUCT_MODULE_NAME).NotificationService |
NSExtensionPointIdentifier | com.apple.usernotifications.service |
Mention the name of the Notification Service class created in NSExtensionPrincipalClass
Step 3: Importing the CastledNotificationService Framework
- Add the CastledNotificationService.framework to your Xcode project.
- Go to your Notification Service Extension target’s Build Phases.
- Expand the “Link Binary With Libraries” section.
- Click the ”+” button and add the CastledNotificationService.framework.
- Import the framework in your Notification Service Extension code file
import CastledNotificationService
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.
import UserNotifications
import CastledNotificationService
class NotificationService: CastledNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
//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)
}
}
}
Please refer to the official Apple documentation for further details on working with Notification Service Extensions
Adding Notification Content Extension
Step 1: Adding the Notification Content Extension to your Xcode Project
- Open your Xcode project.
- Go to File -> New -> Target.
- Select “Notification Content Extension” under the “iOS” category.
- Click “Next” and provide a name for your extension (e.g., “MyNotificationContent”).
- Choose the appropriate settings for your extension and click “Finish”.
- Xcode will generate the necessary files for the Notification Service Extension.
Step 2: Configuring the Info.plist File
- Open the Info.plist file of your Notification Content Extension target.
- Add the following key-value pair:
Key | Value |
---|---|
UNNotificationExtensionCategory | Array of configured notification categories |
UNNotificationExtensionInitialContentSizeRatio | 1 |
UNNotificationExtensionDefaultContentHidden | YES |
NSExtensionMainStoryboard | MainStoryBoard |
NSExtensionPointIdentifier | com.apple.usernotifications.content-extension |
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
- Add the CastledNotificationContent.framework to your Xcode project.
- Go to your Notification Content Extension target’s Build Phases.
- Expand the “Link Binary With Libraries” section.
- Click the ”+” button and add the CastledNotificationContent.framework.
- Import the framework in your Notification Content Extension code file
import CastledNotificationContent
Step 4: 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.
import UIKit
import Castled
import UserNotifications
import UserNotificationsUI
import CastledNotificationContent
class NotificationViewController: CastledNotificationViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any required interface initialization here.
}
override func didReceive(_ notification: UNNotification) {
super.didReceive(notification)
}
}