Notification Service Extension
Notification Service Extension has been introduced in iOS 10 by Apple which allows your push to add images, audio or video content to your push notification, notifyvisitors SDK will use it to enable you to add Action buttons, badge counts and to track delivery counts in your notifyvisitors panel also. So It is an Important step which you need to configure properly as described in this documentation.
Add Notification Service Extension
- Create Notification Service Extension in your project to do so in Xcode goto File >> New >> Target.
- Now Select Notification Service Extension template under iOS section of the dialog box and press on next button to proceed.
- In the next prompt, provide the name of the extension target and select the programming language which you want to use and then click on the Finish button as shown in the screenshot below.
- Once the target is created, Activate the scheme for Extension when prompted for the same. After this your notification service extension will be added to the project you will see a class with the extension name provided by you while creation and .plist file associated with it.
Swift:
Objective-C
- Now we can proceed to add further configuration to this newly created target. Under this target you need to complete Configure info.plist, Import NotifyVisitors header file, add code to NotificationService file and configure AppGroup properly to complete this setup further steps are defined below in detail to complete this setup.
Configure Service Extension’s info.plist
From Project navigator go to your Notification Service Extension project target and open info.plist as source code (right click on info.plist and click on Open as >> Source code) and add the following code in it.
<key>App Bundle identifier</key>
<string>YOUR_APP_MAIN_TARGRT_BUNDLE_ID</string>
<key>NSExtension</key>
<dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.usernotifications.service</string>
<key>NSExtensionPrincipalClass</key>
<string>$(PRODUCT_MODULE_NAME).NotificationService</string>
<key>NSExtensionAttributes</key>
<dict>
<key>UNNotificationExtensionDefaultContentHidden</key>
<true/>
<key>UNNotificationExtensionInitialContentSizeRatio</key>
<real>0.7</real>
</dict>
</dict>
OR
You can simply open the info.plist & add the keys which works the same as above for this.
- Add a new row by going to the menu and clicking Editor>Add Item. Set a key App Bundle identifier as String and set its value to your app’s main target Bundle Identifier.
- Expand NSExtension & add NSExtensionAttributes as Dictionary. Inside NSExtensionAttributes dictionary add two keys, first key is UNNotificationExtensionDefaultContentHidden as Boolean, its value should be YES and the second key is UNNotificationExtensionInitialContentSizeRatio as Number, its value should be 0.7
Import Notifyvisitors SDK in your Notification Service Extension:
- Now you need to import our SDK to your Notification Service Extension target. In order to do so in Xcode, simply go to your Project navigator, then select the notifyvisitors.xcframework which has been imported into your main project. Next, go to File Inspectors (View >> Inspectors >> File) and under the Target Membership tab, check mark the Notification Service Extension option as shown in the screenshot provided below.
Note
Make sure your App’s Main Target and Notification Service Extension both targets should be checked to use the same SDK file in your both targets otherwise you will see errors in importing SDK files.
- Now If you have selected Objective-C in the language while creating the Notification Service Extension then you can directly import our header file in your NotificationService.m file. But if you are using Swift language then you need to create a separate Bridging-Header file and then import our header file as well as done in the main app’s target. To do so Add a new header file and name it with the following format. YOUR_ServiceExtension_NAME-Bridging-Header.h Example if your Service Extension name is MyNotificationServiceExtension. Then the header file name will be MyNotificationServiceExtension-Bridging-Header.h. Now add the following import statement in YOUR_ServiceExtension_NAME-Bridging-Header.h for accessing Native SDK Class.
Note
Make sure that path of your bridge-header.h file should be included in build settings under Swift compiler-code generation as Objective-C bridging header: YOUR_ServiceExtension_NAME/YOUR_ServiceExtension_NAME-Bridging-Header.h
#import <notifyvisitors/notifyvisitors.h>
Configure Push in SignIn and Capabilities:
Goto Signing & Capabilities tab of your Notification Service Extension target and click on + symbol on the left corner of this tab and add Push Notifications and if you are upgraded Xcode and Push Notifications was already added in previous version of Xcode then remove Push Notifications and add it again to configure push notification properly for the upgraded devices.
Modify Push Payload in Notification Service Extension
Now you need to modify Push payload to download and attach media content if available in your payload to display rich media content (image, audio or video). To do so goto your NotificationService.swift/NotificationService.m file and update the didReceiveNotificationRequest() method as shown below.
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
// Modify the notification content here…
notifyvisitors.loadAttachment(with: request, bestAttempt: bestAttemptContent, withContentHandler: self.contentHandler)
}
}
-(void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
self.contentHandler = contentHandler;
self.bestAttemptContent = [request.content mutableCopy];
// Modify the notification content here…
[notifyvisitors LoadAttachmentWithRequest: request bestAttemptContent: self.bestAttemptContent withContentHandler: self.contentHandler];
}
Make sure LoadAttachmentWithRequest() method of our SDK is called in the end of the didReceiveNotificationRequest() if you modify payload after calling this method then SDK may not work properly so you can put some checks to identify your other payloads if you need to handle any.
Important Note
Till the above steps completed you app will be able to receive rich push, Action Buttons and badge counts can be displayed but till now delivery count is not enabled so you will see 0 as delivery count to enable push delivery count in from your app to our panel you need to follow the next step to configure AppGroup properly to enable delivery count.
Configure App Groups for All Targets:
Main Target of your App:
From Project navigator select your App’s main target and Goto Signing & Capabilities tab if App Groups is not already added then click on + Capabilities symbol on the left corner of this tab and add App Groups as shown in screenshot below.
Once you add App Groups capabilities you will be able to see it in your Signin & Capabilities and then under App Groups click the "+" button and add a new app group giving the group name as group.nv.{Your App Bundle Identifier} where {Your App Bundle Identifier} is the same as the bundle identifier of your main app target as shown in screenshot below. After this press OK.
Example: if Your App’s Bundle Identifier is com.notifyvisitors.sampleiOSApp then the App Group must be named as group.nv.com.notifyvisitors.sampleiOSApp make sure this newly created app group must be checked (Turned on) as shown in screenshot below.
Notification Service Extension Target of your App:
Once you have configured App Groups in your Main App Target you need to activate same App Group into your Notification Service Extension Target as done for your main App target to do so from Project navigator select your Notification Service Extension target and Goto Signing & Capabilities tab if App Groups is not already added then click on + Capabilities symbol on the left corner of this tab and add App Groups as shown in screenshot below.
Once you’ve added App Groups capabilities you will be able to see it in your Signin & Capabilities of your Notification Service Extension Target also then under App Groups if previously created AppGroupID is not visible then click on refresh button just beside the + symbol under App Groups to refresh the list of AppGroups configured in your apple developer account. Select the app group created previously and make sure the same AppGroupID must be checked (Turned on) in your Notification Service Extension Target also as done for your Main App Target.
Example: We have previously created an AppGroupID named group.nv.com.notifyvisitors.sampleiOSApp for your Main App Target make sure the same AppGroupID must be visible here and make it checked (Turned on).
Updated 10 months ago