Skip to content

Notification Service Extension for Rich Notifications and better delivery reporting on iOS 10

Olga Koroleva edited this page Aug 17, 2023 · 53 revisions

With iOS 10 you can attach a static or animated (GIF) image (see Rich Notifications for more details), or even a video to push notifications as well as modify the notification content prior to show it to the user. But what is more important for us is that with the Notification Service Extensions we can run arbitrary code in background even if your main application is killed by the user. This feature gives us an opportunity to improve message handling and delivery reporting. In this tutorial you will learn how to integrate the Notification Service Extension with your existing application.

Requirements

  • Ruby 2.3.8 to run "mmine" command.

Setting up App Group ID for an App ID

As a prerequisite, you need to register a new App Group with a unique App Group Id on this site: https://developer.apple.com/account/ios/identifier/applicationGroup. The App Group is needed for your application and notification extension to exchange important data via shared data container, remember this ID for further integration steps.

Setting up Xcode project

Warning

  • Please make sure you've committed all your project changes before going further!
  • If you are integrating Mobile Messaging SDK without use_frameworks!, check this guide
  • If you are integrating Mobile Messaging SDK via Swift Package Manager, check this guide
  1. Install and run our special Ruby Gem called "mmine", passing appropriate parameters:

    $ sudo gem install mmine
    $ mmine integrate \
    --application-code <your Push Application Code from Infobip portal> \
    --project <absolute path to your Xcode project (.xcodeproj file)> \
    --app-group <your App Group Id from previous section> \
    --target <name of your projects main target>

    This tool will automatically integrate Mobile Messaging Notification Extension into your Xcode project. Your Application Code will be automatically injected into NotificationService.swift source file as a hardcoded string parameter for MobileMessagingNotificationServiceExtension.startWithApplicationCode(_) method call.

Notice

Further steps applicable if you use Cocoapods to integrate MobileMessaging SDK. If you use Carthage, next steps are on Integration via Carthage page.

  1. Add settings for MobileMessagingNotificationExtension target to your Podfile as follows:

    Notice

    It's strongly recommended to have the latest version of CocoaPods.

    use_frameworks!
    target 'YourProjectsMainTarget' do
        pod 'MobileMessaging'
         
        target 'MobileMessagingNotificationExtension' do
            inherit! :search_paths
        end
    end
  2. Run pod update from Terminal.

Notice

If you are facing with the following error in your console: [User Defaults] Failed to read values in CFPrefsPlistSource<0xXXXXXXX> (Domain: ..., User: kCFPreferencesAnyUser, ByHost: Yes, Container: (null)): Using kCFPreferencesAnyUser with a container is only allowed for SystemContainers, detaching from cfprefsd.

Although this warning doesn't mean that our code doesn't work, you can shut it up by prefixing your App Group ID with a Team ID of a certificate that you are signing the build with. For example: "9S95Y6XXXX.group.com.mobile-messaging.notification-service-extension". The App Group ID itself doesn't need to be changed though.

Troubleshooting

  1. While developing, make sure you run the project right way. You should build and run the extension's scheme as follows:
  1. Make sure your extension allows arbitrary loads from non-secure sources (i.e. images from non-https (http) sources). In order to allow your extension to load from non-secure sources, append the following settings in your extensions Info.plist:
Clone this wiki locally