3.2.3. Sending Rich Push Notifications
Custom Setup Guide
Before you begin, make sure you have successfully completed the following steps:
ByĀ the end ofĀ this guide you should see the following results:
- The target device will display aĀ mobile push notification with aĀ small square image onĀ the right. AĀ long tap will expand the image toĀ full screen.
- Once expanded, the mobile push notification displays the buttons you setĀ up.
InĀ addition toĀ the usual title and body text inĀ standard push notifications, rich push notifications can have aĀ clickable image with upĀ toĀ 3Ā buttons.
Displaying images
This step will help youĀ to:
Display aĀ push notification with aĀ small square image onĀ the right and expand the image toĀ full screen with aĀ long tap.
ToĀ check that your push notifications are sent correctly, please refer toĀ this guide.
ToĀ display images inĀ push notifications, youāll need toĀ implement the Notification Service Extension.
1. Adding anĀ extension
- Open Xcode and goĀ toĀ Select File ā New ā Target...:
- Select Notification Service Extension and click Next:
- Enter the Product name
MindboxNotificationServiceExtensionand click Finish. - Click Cancel inĀ the Activate Scheme dialog box.
2. Setting upĀ the extension
2.1. App Groups
- Open the project settings.
- Select
MindboxNotificationServiceExtensionfrom the Targets. - GoĀ toĀ the
Signing &Ā Capabilitiestab. - Click Add and select
App Groups. - Add aĀ new group named asĀ
group.cloud.Mindbox.{Bundle IDĀ ofĀ your app}.
Example: with the appās Bundle IDĀ being Mindbox-Sample-App, name the relevant App Group asĀ group.cloud.Mindbox.Mindbox-Sample-App.
Please note that you need toĀ set upĀ App Groups for Maestraās SDK toĀ workIfĀ you skip App Groups setup, the extension will fail toĀ work when aĀ push notification isĀ received (and this error isĀ hard toĀ detect).
2.3. Signing the extension
Sign the extension using the same certificate you used toĀ sign the app. With auto-signing enabled, the signature will beĀ applied automatically. IfĀ not, you will have toĀ manually create certificates for your targets and publish them onĀ the Signing &Ā Capabilities tab.
Check your iOS Deployment Target releasesMake sure that your main Target, Service extension, and Content extension specify the same iOS Deployment Target.
3. Implementing the extension code
3.1. Adding the SDK toĀ the extension
Adding MindboxNotifications using Cocoapods: Service Extension
Open the Podfile and add the guide below toĀ ensure that Mindboxās SDK isĀ used byĀ the extension:
....
use_frameworks!
....
target '<your application>' do
# Comment the next line if you don't want to use dynamic frameworks
pod 'Mindbox'
end
...
# --- NEW ----
# Pods for MindboxNotificationServiceExtension
target 'MindboxNotificationServiceExtension' do
pod 'MindboxNotifications'
end
...Adding MindboxNotifications using Carthage: Service Extension
Set upMindboxNotifications for MindboxNotificationsServiceExtension:
- Close Xcode with your project.
- Access your Project folder from the terminal:
cdĀ path/to/project. - Run
touch CartfiletoĀ create aĀCartfile. - Add the echo
'github "https://github.com/mindbox-cloud/ios-sdk.git"' ā CartfiletoĀ yourCartfile. - Run
carthage update --no-use-binaries --use-xcframeworks. For Xcode 11Ā or older, runcarthage update --no-use-binaries. Note that inĀ the latter case all the .xcframework command examples should read as .framework. - Open your project inĀ Xcode.
- GoĀ toĀ the Project settings toĀ select your Target. GoĀ toĀ the
MindboxNotificationsServiceExtensiontab. - Drag the
MindboxNotifications.xcframeworkfor your Extension onto theFrameworks, Libraries, and Embedded Contenttab.
Set upMindboxNotifications for MindboxNotifcationsContentExtension:
- Close Xcode with your project.
- Access your project folder from the terminal
cdĀ path/to/project. - Run touch
CartfiletoĀ create aĀCartfile. - Add the
echo 'github "https://github.com/mindbox-cloud/ios-sdk.git"' ā CartfiletoĀ yourCartfile. - Run
carthage update --no-use-binaries --use-xcframeworks. For Xcode 11Ā or older, runcarthage update --no-use-binaries. Note that inĀ the latter case all the .xcframework command examples should read as .framework. - Open your project inĀ Xcode.
- GoĀ toĀ the Project settings toĀ select your Target. GoĀ toĀ the
MindboxNotifcationsContentExtensiontab. - Drag
MindboxNotifications.xcframeworkfor your Extension onto theFrameworks, Libraries, and Embedded Contenttab.
Adding MindboxNotifications using Swift Package Manager: Service Extension
- Open Xcode and goĀ toĀ
File ā Add Packages...from the top menu. - Enter the URL toĀ Mindboxās SDK
https://github.com/mindbox-cloud/ios-sdkinĀ the window that appears. - Select version 1.3.3Ā or higher toĀ support the Swift Package Manager and click Add Package.
- Once the package has been downloaded, specify your target:
- add
MindboxtoĀ the main project target, - add
MindboxNotificationsServicetoĀMindboxNotificationServiceExtension.
- add
3.2. Implementing the extension code inĀ your app
AĀ basic way toĀ implement the extension code into anĀ app that will workĀ if:
- your app uses Mindbox push notifications only;
- you need toĀ integrate the code ASAP.
Open the main extension file toĀ perform the following:
- import the MindboxNotifications library;
- call the
MindboxNotificationService()method; - add 2Ā method calls:
didReceiveandserviceExtensionTimeWillExpire.
Simple implementation example:
import UserNotifications
import MindboxNotifications
class NotificationService: UNNotificationServiceExtension {
lazy var mindboxService = MindboxNotificationService()
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
mindboxService.didReceive(request, withContentHandler: contentHandler)
}
override func serviceExtensionTimeWillExpire() {
// Called just before the extension will be terminated by the system.
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
mindboxService.serviceExtensionTimeWillExpire()
}
}`
AnĀ advanced method toĀ implement the extension code into anĀ app that itĀ worksĀ if:
- you use multiple mailing providers;
- you need toĀ implement aĀ unique way toĀ process notifications;
- you need toĀ process the payload data;
- you apply aĀ custom Content Extension.
How toĀ apply the advanced method:
- Call
MindboxNotificationService()toĀ access open-source API methods; - Check ifĀ the uniqueKey field isĀ empty inĀ your push notification. IfĀ itĀ isĀ not empty, the notification isĀ probably from Mindbox, soĀ call
mindboxService.pushDelivered(request)toĀ notify Mindbox that its push notification has been received. IfĀ this push notification was not from Mindbox, the receipt notification will beĀ ignored; - Implement push notification processing using one ofĀ the following methods:
- Apply the
MindboxNotificationService()methods from the example above; - Process all the fields inĀ the push notification manually. Use the iOS push notification format.
- Apply the
Example with minimum code, without content processing:
import UserNotifications
import MindboxNotifications
class NotificationService: UNNotificationServiceExtension {
lazy var mindboxService = MindboxNotificationService()
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
if(request.content.userInfo["uniqueKey"] as? String != nil) {
mindboxService.pushDelivered(request)
}
self.contentHandler = contentHandler
self.bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
guard let bestAttemptContent = self.bestAttemptContent else { return }
contentHandler(bestAttemptContent)
}
override func serviceExtensionTimeWillExpire() {
// Called just before the extension will be terminated by the system.
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}Check that push notifications with images are sent correctly.
Displaying buttons
This step will help youĀ to:Set upĀ mobile push notifications toĀ display buttons when expanded.
ToĀ check that buttons appear when aĀ push notification isĀ tapped, please refer toĀ this guide.
ToĀ enable buttons toĀ beĀ displayed inĀ push notifications, youāll need toĀ add the Notification Content extension.
1. Adding the extension
- Open Xcode and click Select File ā New ā Target...:
- Click Notification Content Extension and then Next:
- Enter
MindboxNotificationContentExtensionfor the Product name and click Finish. - Click Cancel inĀ the Activate Scheme window.
2. Setting upĀ the extension
2.1. App Groups
- Open the project settings.
- Select
MindboxNotificationServiceExtensionfrom the Targets. - GoĀ toĀ the
Signing &Ā Capabilitiestab. - Click Add and select
App Groups. - Add aĀ new group named as
group.cloud.Mindbox.{Bundle IDĀ ofĀ your app}.
Example: ifĀ the appās Bundle IDĀ is Mindbox-Sample-App, the App Group name must beĀ group.cloud.Mindbox.Mindbox-Sample-App.
Please note that you need toĀ set upĀ App Groups for Mindboxās SDK toĀ workIfĀ you skip App Groups setup, the extension will fail toĀ work when aĀ push notification isĀ received (and this error isĀ hard toĀ detect).
2.2. Signing the extension
Sign the extension using the same certificate you used toĀ sign the app. With auto-signing enabled, the signature will beĀ applied automatically. IfĀ not, you will have toĀ manually create certificates for your targets and publish them onĀ the Signing &Ā Capabilities tab.
Check your iOS Deployment Target releasesMake sure that your main Target, Service extension, and Content extension specify the same iOS Deployment Target.
3. Implementing the extension code
3.1. Adding SDK toĀ the extension
Adding MindboxNotifications using Cocoapods: Content Extension
Open the Podfile and add the guide below toĀ ensure that Mindboxās SDK isĀ used byĀ the extension.
....
use_frameworks!
....
target '<your application>' do
# Comment the next line if you don't want to use dynamic frameworks
pod 'Mindbox'
end
...
# Pods for MindboxNotificationServiceExtension
target 'MindboxNotificationServiceExtension' do
pod 'MindboxNotifications'
end
...
# ---- NEW ----
# Pods for MindboxNotificationContentExtension
target 'MindboxNotificationContentExtension' do
pod 'MindboxNotifications'
end
...Adding MindboxNotifications using Carthage: Content Extension
Set upMindboxNotifications for MindboxNotificationsContentExtension:
- Close Xcode with your project.
- Access your Project folder from the terminal:
cdĀ path/to/project. - Run
touch CartfiletoĀ create aĀCartfile. - Add the echo
'github "https://github.com/mindbox-cloud/ios-sdk.git"' ā CartfiletoĀ yourCartfile. - Run
carthage update --no-use-binaries --use-xcframeworks. For Xcode 11Ā or older, runcarthage update --no-use-binaries. Note that inĀ the latter case all the .xcframework command examples should read as .framework. - Open your project inĀ Xcode.
- GoĀ toĀ the Project settings toĀ select your Target. GoĀ toĀ the
MindboxNotificationsContentExtensiontab. - Drag the
MindboxNotifications.xcframeworkfor your Extension onto theFrameworks, Libraries, and Embedded Contenttab.
Adding MindboxNotifications using Swift Package Manager: Content Extension
- Open Xcode and goĀ toĀ
File āAdd Packages...from the upper menu. - Enter the URL toĀ Mindboxās SDK
https://github.com/mindbox-cloud/ios-sdkinĀ the window that appears. - Select version 1.3.3Ā or higher toĀ support the Swift Package Manager and click Add Package.
- Once the package has been downloaded, specify your target:
- add
MindboxtoĀ the main project target, - add
MindboxNotificationsContenttoĀMindboxNotificationContentExtension.
- add
3.2. Implementing the extension code inĀ your app
This isĀ aĀ basic way toĀ implement the extension code into anĀ app that will workĀ if:
- your app uses Mindbox push notifications only;
- you need toĀ integrate the code ASAP.
Simply follow these 2Ā steps:
- Import the library into
NotificationViewController.swiftand call theMindboxNotificationService()API method from the file; - Adjust your settings inĀ
info.plist.
Example ofĀ the basic implementation method:
import UIKit
import UserNotifications
import UserNotificationsUI
import MindboxNotifications
class NotificationViewController: UIViewController, UNNotificationContentExtension {
lazy var mindboxService = MindboxNotificationService()
func didReceive(_ notification: UNNotification) {
mindboxService.didReceive(notification: notification, viewController: self, extensionContext: extensionContext)
}
}3.3 Setting upĀ info.plist
Adjustinfo.plist asĀ follows:
- Delete the
NSExtensionMainStoryboardkey. - Add the following keys:
| Key | Value |
|---|---|
NSExtensionPrincipalClass | Create using this template: IfĀ you follow the guide, you should get the following result: |
UNNotificationExtensionCategory | IfĀ you apply the simple method toĀ implement the Service Extension, you should get the following result:MindBoxCategoryIdentifier |
UNNotificationExtensionInitialContentSizeRatio | 0.0001 |
UNNotificationExtensionUserInteractionEnabled | 1 |
The result should look like the screenshot below:
3.4 Deleting MainInterface.storyboard
MainInterface.storyboardWhen you add the extension, a .storyboard file isĀ created inĀ the folder. Delete itĀ toĀ leave Mindboxās API method toĀ handle the UI.
IfĀ the basic method above does not work for you, you can code your Rich Push layout from scratch.
There are noĀ special recommendations hereĀ ā you doĀ not have toĀ specify any additional methods.
Check that buttons are displayed when you tap aĀ push notification.
ToĀ debug common errors, refer toĀ the SDK Integration Checklist.
By now, you should see the following results:
The target device displays aĀ mobile push notification with aĀ small square image onĀ the right. AĀ long tap unfolds the image toĀ full screen.
Once expanded, the mobile push notification displays the buttons you have setĀ up.
Updated 7 months ago

