Notification Service

Why add a Notification Service Extension to your project?

Notification Service extension is needed to achieve the following: 

  1. Add media support in Notifications: Post iOS10 Apple has given us the ability to add images, gifs, audio, and video files to the notifications and this can be done using the Notification Service Extension.

  2. For supporting Inbox Feature: Notification Service Extension is also used to save the received notifications which can later be shown in the App Inbox.

  3. For Updating the Notification Badge count: MoEngage makes use of the extension to update the notification badge count and doesn't send badge in the notification payload.

  4. For Tracking Notification Impression: We can track if a Notification is received by the device using the Notification Service Extension.

Follow the below steps to set up Notification Service Extension:

1. Create a Notification Service Extension Target:

Screenshot 2024-02-06 at 2.55.06 PM.png

After the target is created, Activate the scheme for Extension when prompted for the same. 

Screenshot 2024-02-06 at 2.56.38 PM.png

2. Set the deployment target

Make sure to set the deployment target the same as the main app target

3. Integrate MoEngageRichNotification framework to Extension:

Integrate using CocoaPod

For integrating through CocoaPod, include MoEngageRichNotification pod for your Notification Service Extension as shown below, and run the pod update / install command :

Ruby
target "NotificationService" do
	pod 'MoEngageRichNotification','~>7.14.0'
end

Integrate using Swift Package Manager

To integrate via SPM use the https://github.com/moengage/MoEngage-iOS-RichNotification.git GitHub url link and set the branch as master or required version.

Manual Integration

info

Note

  • To integrate the MoEngageRichNotification  SDK manually to your project follow this doc.

  • Add MoEngageRichNotification to embedded binaries in the App target, and is linked in your Notification Service Extension target.

4. Set the App Group ID :

First, select your Main App Target and select Capabilities do the changes as shown in the image below:

Screenshot 2024-02-07 at 2.26.22 PM.png

info

App Group ID Recommendation

We recommend having a separate App Group ID set for MoEngage with the format group.{app bundle id}.MoEngage. And make sure the same app group id is enabled for all the targets where MoEngage is being used.

Provide the App Group ID to SDK

Provide the App Group ID selected in Capabilities in MoEngageSDKConfig instance while initializing the SDK as shown below:

Swift Objective-C
let sdkConfig = MoEngageSDKConfig(withAppID: "MoEngage App ID")
sdkConfig.appGroupID = "App Group ID"

Set the App Group Id to Extension

Turn ON App Groups for your notification service extension target and enable the same App group ID that was selected for the App Target(In the above steps).

Screenshot 2024-02-06 at 3.21.36 PM.png

5. Set the KeyChain Sharing

If storage encryption is enabled add the KeyChain Sharing capability and set the value as the same as of main App target.

Screenshot 2024-02-07 at 12.05.45 PM.png

Refer to the doc for more on Storage encryption and KeyChain sharing 

6. Code Changes in Notification Service Extension:

Swift Objective-C
import UserNotifications
// 1st Step
import MoEngageRichNotification

class NotificationService: UNNotificationServiceExtension {

    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
    
    	// 2nd Step	
      MoEngageSDKRichNotification.setAppGroupID("YOUR APPGROUP ID")
    
      self.contentHandler = contentHandler
      bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
      
      // 3rd Step
      MoEngageSDKRichNotification.handle(richNotificationRequest: 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.
        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
            contentHandler(bestAttemptContent)
        }
    }

}

Refer to the code above and make the following changes:

  1. Import MoEngageRichNotification framework.
  2. Set the App Group ID selected in the settings earlier using setAppGroupID(_:) method.
  3. Call handle(richNotificationRequest:withContentHandler:) method.
warning

Rich Notification Media Limitations:

  • Refer to the following link to know about the size and format limitation for attachments(media) supported in Rich Notifications.

  • Http URL's aren't supported in iOS10 unless explicitly specified in the plist. You will have include App Transport Security Settings Dictionary in your Notification Service Extension's Info.plist and inside this set Allow Arbitrary Loads to YES.

check_circle

Image Guidelines

  • File Size: The maximum file size for image attachments can be 10MB.
  • Dimensions: The maximum possible dimensions are 1038 x 1038 pixels. It can be anything smaller than 1038 pixels.
  • Landscape vs Portrait: iOS supports both orientations but we recommend using images that have a landscape orientation this is because depending on the dimensions, portrait images may look too tall.
Was this article helpful?
0 out of 0 found this helpful

How can we improve this article?