Notification Service Extension
- In Xcode, select File >> New >> Target and choose the Notification Service Extension template.

- Define a name for the Notification Service Extension as given below.
Objective C

Swift code

- 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

Swift code

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

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

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

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

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)
}
Updated 9 months ago