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.
Click to Expand
- 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
-
-
In Xcode, navigate to
File
>New
>Target
and selectNotification Service Extension
thenNext
-
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
.
-
-
There are 2 common methods for integrating a library inside a Service Extension:
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.
-
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
-
Under
Add to Target
selectNotificationService
(Your Service Extension Target). -
Click
Add Package
.
-
-
Cocoapods should be installed inside your system
-
podfile should be available for your project
-
-
-
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
-
-
Save the changes to the Podfile.
-
Install the pods by running the following command:
pod install
-
-
-
-
-
-
Open NotificationService.swift
-
Import WEServiceExtension by adding code
import WEServiceExtension
-
Remove all existing code from the class
NotificationService
-
Add subclassing to
NotificationService
withWEXPushNotificationService
import UserNotifications // Step 1 : Importing WEServiceExtension import WEServiceExtension // Step 2 : Subclassing service Extension class NotificationService: WEXPushNotificationService { }
-
-
- Open NotificationService.m
- Import
WEServiceExtension/WEServiceExtension-Swift.h
- Create Object of
WEXPushNotificationService
- 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
-
-
Here's how you can go about it:
- Open the
Info.plist
file forNotificationService
- Add
App Transport Security Settings
key underInformation Property List
inNotificationService-Info.plist
file. - Set
Allow Arbitrary
Loads toYES
underApp Transport Security Settings
- Not required if you are sure that image URLs provided onWebEngage
dashboard will always usehttps
.
- Open the
-
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 & Capabilitie
s >+ Capability
>App Groups
. - Within
App Groups
, click the+
button. - Set the App Groups container to be
group.YOUR_BUNDLE_IDENTIFIER.WEGNotificationGroup
whereYOUR_BUNDLE_IDENTIFIER
is the same as your Main Application "Bundle Identifier
". - Press
OK
and repeat for theNotificationService
Target. -
- Select your
-
-
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.
-
-
-
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 TargetNotificationService
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:
-
- Open the
NotificationService.swift
file. - Replace
import WebEngageBannerPush
withimport WEServiceExtension
. - Done
- Open the
-
- Open the NotificationService.h file.
- Remove the import statement
#import <WebEngageBannerPush/WEXPushNotificationService.h>
. - Replace
WEXPushNotificationService
withUNNotificationServiceExtension
. - Open the
NotificationService.m
file. - Replace the code as given in Step Objective C
-
-
-
WEServiceExtension is available under the MIT license. See the LICENSE file for more info.