WEServiceExtension 1.1.2

WEServiceExtension 1.1.2

Maintained by WebEngage, WEPersonalization.



  • By
  • WebEngage

WEServiceExtension

Version License Platform LastUpdated


Push notification service extensions in iOS are needed to make notifications more interesting by adding images, videos, or interactive buttons. They also ensure that sensitive tasks, like processing user data, are done securely without affecting the app's performance.

Table of Content

Click to Expand

Prerequisites

  • WebEngage SDK needs to be integrated inside project
  • Basic knowledge of Service Extension and Content Extension
  • Basic knowledge about push notification , swift / Objc Programing Language

  • Step 1 : Create service extension for Project

    • In Xcode, navigate to File > New > Target and select Notification Service Extension then Next

    • Enter the Product Name as NotificationService, and click Finish.

    • Click Activate on the prompt shown to activate the service extension. Xcode will now create a new top-level folder in your project with the name NotificationService.

    • Screenshots

      Screenshot 1

      Screenshot 1

      Screenshot 2

      Screenshot 2

      Screenshot 3

      Screenshot 3

      Screenshot 4

      Screenshot 4

  • Step 2 : Integrate WEServiceExtension inside Service Extension

    There are 2 common methods for integrating a library inside a Service Extension:

    Note: Choose Either SPM or CocoaPods

    It's recommended to choose either Swift Package Manager or CocoaPods for integrating the library into your Service Extension. Mixing both methods might lead to conflicts or inconsistencies in your project setup.


    Approach 1 : Integrating via SPM

    • Select your Project > Package Dependencies > + button. Enter Package URL: https://github.com/WebEngage/WEServiceExtension.git in the search bar.

      https://github.com/WebEngage/WEServiceExtension.git
      

      Screenshot 1 (SPM)

      Screenshot 1 (SPM)

      Screenshot 2 (SPM)

      Screenshot 2 (SPM)

      Screenshot 3 (SPM)

      Screenshot 3 (SPM)
    • Under Add to Target select NotificationService (Your Service Extension Target).

      Screenshot 4 (SPM)

      Screenshot 4 (SPM)
    • Click Add Package.

      Screenshot 5 (SPM)

      Screenshot 5 (SPM)

    OR

    Approach 2 : Integrating via CocoaPods

    • Prerequisites

      • Cocoapods should be installed inside your system

      • podfile should be available for your project

    • Edit Podfile

      • Open the Podfile using a text editor.

      • Add the library dependency to the Podfile. For example:

        # this target name should be your ServiceExtension Name
        target 'NotificationService' do
          # Uncomment the line below if the parent target also uses frameworks
          # use_frameworks!
          pod 'WEServiceExtension'
          # Add other pods for the NotificationService target here
        end

        Note : Your target name should be the Service Extension name which you have entered while creating ServiceExtension, Over here refer screenshot 3

      • Install Pods

        • Save the changes to the Podfile.

        • Install the pods by running the following command:

          pod install

  • Step 3 : Import and Use the WebEngage inside Service Extension

    • SWIFT

      1. Open NotificationService.swift

      2. Import WEServiceExtension by adding code import WEServiceExtension

      3. Remove all existing code from the class NotificationService

      4. Add subclassing to NotificationService with WEXPushNotificationService

        import UserNotifications
        // Step 1 : Importing WEServiceExtension
        import WEServiceExtension
        
        // Step 2 : Subclassing service Extension
        class NotificationService: WEXPushNotificationService {
        }
    • Objective C

      1. Open NotificationService.m
      2. Import WEServiceExtension/WEServiceExtension-Swift.h
      3. Create Object of WEXPushNotificationService
      4. Pass necessary information to WebEngage through above created object

      NotificationService.m will look like below code snippet

      #import "NotificationService.h"
        // Step 1 : Importing WEServiceExtension
        #import <WEServiceExtension/WEServiceExtension-Swift.h>
      
        @interface NotificationService ()
        // Step 2 : Creating Object of service Extension
        @property (nonatomic, strong) WEXPushNotificationService *serviceExtension;
      
        @end
      
        @implementation NotificationService
      
        // Step 3 : Pass necessary information to WebEngage
        - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
            if (_serviceExtension == NULL){
                _serviceExtension = [[WEXPushNotificationService alloc]init];
            }
            [_serviceExtension didReceiveNotificationRequest:request
                                          withContentHandler:contentHandler];
        }
      
        - (void)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.
            [_serviceExtension serviceExtensionTimeWillExpire];
        }
      
        @end
  • Step 4 : Configure ServiceExtension-Info.plist

    Here's how you can go about it:

    • Open the Info.plist file for NotificationService
    • Add App Transport Security Settings key under Information Property List in NotificationService-Info.plist file.
    • Set Allow Arbitrary Loads to YES under App Transport Security Settings - Not required if you are sure that image URLs provided on WebEngage dashboard will always use https.

      Screenshot 1

      Screenshot 1
  • Step 5 : Create App Groups for all Targets

    App Groups allow your app and the WebEngageNotificationServiceExtension to communicate when a notification is received, even if your app is not active. This is required for Confirmed Deliveries.

    • Select your Main App Target > Signing & Capabilities > + Capability > App Groups.
    • Within App Groups, click the + button.
    • Set the App Groups container to be group.YOUR_BUNDLE_IDENTIFIER.WEGNotificationGroup where YOUR_BUNDLE_IDENTIFIER is the same as your Main Application "Bundle Identifier".
    • Press OK and repeat for the NotificationService Target.
    • Screenshots



      Screenshot 1

      Screenshot 1


      Screenshot 1

      Screenshot 2


      Screenshot 1

      Screenshot 3

  • Step 6 : Build and Test

    • Build your project to ensure that the library integrates successfully.

    • Test your Service Extension to ensure that it functions as expected with the integrated library.


  • Migration from WebEngageBannerPush to WEServiceExtension

    • If you've been using the old service extension and want to switch to the new one, just stick to these instructions in the documentation:

    • Below are the steps to migrate from WebEngageBannerPush to the WEServiceExtension:

      • Remove pod 'WebEngageBannerPush' from the Service Extension Target NotificationService in the podfile.

      • Then Perform pod install

      • Then Follow Step Approach 2 : Cocoapods

      • After successfully completing the above step, let's move to the code section:

        • For Swift Users :

          • Open the NotificationService.swift file.
          • Replace import WebEngageBannerPush with import WEServiceExtension.
          • Done
        • For Objective-C Users:

          • Open the NotificationService.h file.
          • Remove the import statement #import <WebEngageBannerPush/WEXPushNotificationService.h>.
          • Replace WEXPushNotificationService with UNNotificationServiceExtension.
          • Open the NotificationService.m file.
          • Replace the code as given in Step Objective C

License

WEServiceExtension is available under the MIT license. See the LICENSE file for more info.