Notification Service Extension

  1. Create a Notification Service Extension in your project. To do that, in Xcode, select File >> New Target and choose the Notification Service Extension template.

  1. Define a name for the Notification Service Extension as given below:

Now close your xcode.

  1. If you already have a Podfile in your project. Please skip this step. Goto your ios folder
    from your project root folder by using the following command.
$ cd platforms && cd ios && pod init
  1. Now open your Xcode and add the notifyvisitors dependency on the pod file under your Notification Service Extension name target like below.
target 'YourNotificationServiceExtension' do
       pod 'notifyvisitors', '6.4.2'
 end
  1. Now Run the following commands in the terminal from the ios folder directory.
pod repo update && pod install && cd .. && cd ..
  • 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 info.plist file associated with it.

  1. Now Open info.plist of your Notification Service Extension 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’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>

OR

  • You can simply open the info.plist and add the keys which works as same as above for this.

    6.1 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 main target Bundle Identifier.

6.2 Expand the NSExtension and add NSExtensionAttributes as Dictionary. Inside NSExtensionAttributes dictionary add a key UNNotificationExtensionDefaultContentHidden as Boolean its value should be YES and add another key UNNotificationExtensionInitialContentSizeRatio as Number its value should be 0.7 as the following preview.

7 Select Notification Service Extension Target from your target and Goto Capabilities Tab Turn On Push Notifications and if you are upgraded Xcode 8.0 or later and Push Notifications was turned on in previous version turn off Push Notifications and turn it on again to configure push notification properly for the upgraded devices.

  1. Capabilities Tab Turn On App Groups and select the app group created previously in the beginning.
    Example: We have previously created an app group named group.nv.com.example.myapp make sure this app group must be visible here and make it checked (Turned on).

  1. Import the header file ServiceExtension.m file.
#import <notifyvisitors/notifyvisitors.h>
  1. Goto NotificationService.m file and update didReceiveRequestWithContentHandler delegate method as follows:
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
     self.contentHandler = contentHandler;
     self.bestAttemptContent = [request.content mutableCopy];
     [notifyvisitors LoadAttachmentWithRequest: request bestAttemptContent: self.bestAttemptContent
                withContentHandler:  self.contentHandler];
}