Notification Service Extension

  1. In Xcode, select File >> New >> Target and choose the Notification Service Extension template.
724724
  1. Define a name for the Notification Service Extension as given below.

Objective C

724724

Swift code

723723
  1. Once the target is created, Activate the scheme for Extension when prompted for the same. After this your extension will be added to project you will see a class with the extension name provided by you while creation and .plist file associated with it.

Objective C

258258

Swift code

210210
  1. You can configure the plist via source code or editor.

Source Code

Right Click on Info.plist and click on Open as >> Source code.Add the following code in it.

<key>App Bundle identifier</key>
            <string>”Your App’s Project Identifier Name”</string>
        <key>NSExtension</key>
    <dict>
        <key>NSExtensionAttributes</key>
        <dict>
            <key>UNNotificationExtensionDefaultContentHidden</key>
            <true/>
            <key>UNNotificationExtensionInitialContentSizeRatio</key>
            <real>0.7</real>
        </dict>
        <key>NSExtensionPointIdentifier</key>
        <string>com.apple.usernotifications.service</string>
        <key>NSExtensionPrincipalClass</key>
        <string>NotificationService</string>
    </dict>

Editor
Add the following keys with their required values.

Add a new row by going to the menu and clicking Editor > Add Item. Set a key App Bundle identifier as a String and set its value to your app’s Bundle Identifier.

699699

Expand the NSExtension and add NSExtensionAttributes as Dictionary. Inside NSExtensionAttributes dictionary add two keys : UNNotificationExtensionDefaultContentHidden as Boolean, its value should be YES and UNNotificationExtensionInitialContentSizeRatio as Number, its value should be 0.7

813813
  1. Go to Build Phase tab of your Notification Service Extension target and click on “+” sign under Link Binary With Libraries. Then, click on “Add Other…” from the promoted box and add the reference of our sdk .a file from your apps project folder (Don’t give reference to.a file from outside of your project).
824824

In the Capabilities Tab Turn On App Groups and select the app group created previously in the beginning.

672672

Example: we have previously created a app group named group.nv.com.example.myapp make sure this app group must be visible here and make it checked (Turned on).
Goto NotificationService.m file and update didReceiveRequestWithContentHandler delegate mehod as follows

-(void)didReceiveNotificationRequest: (UNNotificationRequest*)request
            withContentHandler: (void (^)(UNNotificationContent *))contentHandler {
                self.contentHandler = contentHandler;
                self.bestAttemptContent = [request.content mutableCopy];    
                [notifyvisitors LoadAttachmentWithRequest: request bestAttemptContent: self.bestAttemptContent
                withContentHandler:  self.contentHandler];
                }
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {

        self.contentHandler = contentHandler

        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)

notifyvisitors.LoadAttachmentWithRequest(request, bestAttemptContent: self!.bestAttemptContent, withContentHandler: self!.contentHandler)      

    }