Rich Push Notifications

With Intercom version 12.3.0+, you can send rich push notifications on iOS. You can add images to your notifications.

Prerequisites

Before you begin with this, make sure you have gone through Push Notifications. Everything required for simple push notifications needs to be done in order for Rich Push Notifications to work.

Once you are finished with setting up basic push notifications, you can proceed further.

Step 1: Create Notification Service Extension

In Xcode, go to File -> New -> Target, and choose “Notification Service Extension”. Give it a name and click finish. A new target will be added to your project. Set the desired deployment target. The extension will also require its own provisioning profile.

Step 2: Integrate Intercom

Option 1: Cocoapods

In your podfile, add a new target for your Notification Service Extension and add the Intercom pod to that target. Run pod install to install the pod to the extension.

target :YourNotificationServiceExtensionTargetName do
  pod 'Intercom'
End

Option 2: Swift Package Manager

If you had used Swift Package Manager to add the Intercom SDK to your main app, then:

  1. Go to the File Inspector.
  2. Select your project.
  3. Select your Notification Service Extension target.
  4. In the General tab, add the Intercom framework to the Framework and Libraries section.

Option 3: Manual Integration

If you had manually added the Intercom framework to your project, then

  1. Go to the File Inspector.
  2. Select your project.
  3. Select your Notification Service Extension target.
  4. In the General tab, add the Intercom framework to the Framework and Libraries section.
  5. Ensure that Do Not Embed is selected. Otherwise you will get errors during app distribution.

Step 3: Handle Rich Push Notification requests

Automatically (Default)

When your NotificationServiceExtension receives a push notification request, Intercom for iOS checks if it is an Intercom push notification and if it is, it then downloads the media and attaches it to the notification. To do this we safely swizzle the public methods in your Notification Service Extension’s "Principal Class", which will be NotificationService by default, that handle receiving push notification requests. We do not use any private APIs to do this.

Manually

In certain circumstances you may want more control of your push notifications. You can disable automatic handling of Intercom push notifications by doing the following:

  1. Add the following to your Info.plist of your Notification Service Extension:
    IntercomAutoIntegratePushNotifications with a value of NO.
  1. Import Intercom in your NotificationService class.
  2. Handle Intercom push notifications manually in didReceiveNotificationResponse in your UNNotificationServiceExtension:
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];
    
    if (!self.bestAttemptContent) {
        contentHandler(request.content);
        return;
    }
    
    if ([Intercom isIntercomPushNotification:self.bestAttemptContent.userInfo]) {
        [Intercom handleIntercomRichPushNotificationContent:self.bestAttemptContent withContentHandler:contentHandler];
    } else {
        // Handle non Intercom push notifications here.
        self.contentHandler(self.bestAttemptContent);
    }
}
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
  self.contentHandler = contentHandler
  bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)

  guard let bestAttemptContent = bestAttemptContent else {
    contentHandler(request.content)
    return
  }

  if Intercom.isIntercomPushNotification(bestAttemptContent.userInfo) {
    Intercom.handleRichPush(bestAttemptContent, withContentHandler: contentHandler);
  } else {
    // Handle non Intercom push notifications here.
    contentHandler(bestAttemptContent)
  }
}