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

  1. Open Xcode and goĀ toĀ Select File → New → Target...:
  1. Select Notification Service Extension and click Next:
  1. Enter the Product name MindboxNotificationServiceExtension and click Finish.
  2. Click Cancel inĀ the Activate Scheme dialog box.

2. Setting upĀ the extension

2.1. App Groups

  1. Open the project settings.
  2. Select MindboxNotificationServiceExtension from the Targets.
  3. GoĀ toĀ the Signing &Ā Capabilities tab.
  4. Click Add and select App Groups.
  5. 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Ā work

IfĀ 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 releases

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

  1. Close Xcode with your project.
  2. Access your Project folder from the terminal: cdĀ path/to/project.
  3. Run touch Cartfile toĀ create aĀ Cartfile.
  4. Add the echo 'github "https://github.com/mindbox-cloud/ios-sdk.git"' → Cartfile toĀ your Cartfile.
  5. Run carthage update --no-use-binaries --use-xcframeworks. For Xcode 11Ā or older, run carthage update --no-use-binaries. Note that inĀ the latter case all the .xcframework command examples should read as .framework.
  6. Open your project inĀ Xcode.
  7. GoĀ toĀ the Project settings toĀ select your Target. GoĀ toĀ the MindboxNotificationsServiceExtension tab.
  8. Drag the MindboxNotifications.xcframework for your Extension onto the Frameworks, Libraries, and Embedded Content tab.

Set upMindboxNotifications for MindboxNotifcationsContentExtension:

  1. Close Xcode with your project.
  2. Access your project folder from the terminal cdĀ path/to/project.
  3. Run touch Cartfile toĀ create aĀ Cartfile.
  4. Add the echo 'github "https://github.com/mindbox-cloud/ios-sdk.git"' → Cartfile toĀ your Cartfile.
  5. Run carthage update --no-use-binaries --use-xcframeworks. For Xcode 11Ā or older, run carthage update --no-use-binaries. Note that inĀ the latter case all the .xcframework command examples should read as .framework.
  6. Open your project inĀ Xcode.
  7. GoĀ toĀ the Project settings toĀ select your Target. GoĀ toĀ the MindboxNotifcationsContentExtension tab.
  8. Drag MindboxNotifications.xcframework for your Extension onto the Frameworks, Libraries, and Embedded Content tab.

Adding MindboxNotifications using Swift Package Manager: Service Extension

  1. Open Xcode and goĀ toĀ File → Add Packages... from the top menu.
  2. Enter the URL toĀ Mindbox’s SDK https://github.com/mindbox-cloud/ios-sdk inĀ the window that appears.
  3. Select version 1.3.3Ā or higher toĀ support the Swift Package Manager and click Add Package.
  4. Once the package has been downloaded, specify your target:
    • add Mindbox toĀ the main project target,
    • add MindboxNotificationsService toĀ MindboxNotificationServiceExtension.

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: didReceive and serviceExtensionTimeWillExpire.

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.

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

  1. Open Xcode and click Select File → New → Target...:
  1. Click Notification Content Extension and then Next:
  1. Enter MindboxNotificationContentExtension for the Product name and click Finish.
  2. Click Cancel inĀ the Activate Scheme window.

2. Setting upĀ the extension

2.1. App Groups

  1. Open the project settings.
  2. Select MindboxNotificationServiceExtension from the Targets.
  3. GoĀ toĀ the Signing &Ā Capabilities tab.
  4. Click Add and select App Groups.
  5. 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Ā work

IfĀ 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 releases

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

  1. Close Xcode with your project.
  2. Access your Project folder from the terminal: cdĀ path/to/project.
  3. Run touch Cartfile toĀ create aĀ Cartfile.
  4. Add the echo 'github "https://github.com/mindbox-cloud/ios-sdk.git"' → Cartfile toĀ your Cartfile.
  5. Run carthage update --no-use-binaries --use-xcframeworks. For Xcode 11Ā or older, run carthage update --no-use-binaries. Note that inĀ the latter case all the .xcframework command examples should read as .framework.
  6. Open your project inĀ Xcode.
  7. GoĀ toĀ the Project settings toĀ select your Target. GoĀ toĀ the MindboxNotificationsContentExtension tab.
  8. Drag the MindboxNotifications.xcframework for your Extension onto the Frameworks, Libraries, and Embedded Content tab.

Adding MindboxNotifications using Swift Package Manager: Content Extension

  1. Open Xcode and goĀ toĀ File →Add Packages... from the upper menu.
  2. Enter the URL toĀ Mindbox’s SDK https://github.com/mindbox-cloud/ios-sdk inĀ the window that appears.
  3. Select version 1.3.3Ā or higher toĀ support the Swift Package Manager and click Add Package.
  4. Once the package has been downloaded, specify your target:
    • add Mindbox toĀ the main project target,
    • add MindboxNotificationsContent toĀ MindboxNotificationContentExtension.

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.swift and call the MindboxNotificationService() 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:

  1. Delete the NSExtensionMainStoryboard key.
  2. Add the following keys:
KeyValue
NSExtensionPrincipalClass

Create using this template: {extension name}.{controller name}

IfĀ you follow the guide, you should get the following result:
MindboxNotificationContentExtension.NotificationViewController

UNNotificationExtensionCategoryIfĀ you apply the simple method toĀ implement the Service Extension, you should get the following result:
MindBoxCategoryIdentifier
UNNotificationExtensionInitialContentSizeRatio0.0001
UNNotificationExtensionUserInteractionEnabled1

The result should look like the screenshot below:

3.4 Deleting MainInterface.storyboard

When 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.