Why add a Notification Service Extension to your project?
Notification Service extension is needed to achieve the following:
-
Add media support in Notifications: Post iOS10 Apple has given us the ability to add images, gifs, audio, and video files to the notifications and this can be done using the Notification Service Extension.
-
For supporting Inbox Feature: Notification Service Extension is also used to save the received notifications which can later be shown in the App Inbox.
-
For Updating the Notification Badge count: MoEngage makes use of the extension to update the notification badge count and doesn't send badge in the notification payload.
-
For Tracking Notification Impression: We can track if a Notification is received by the device using the Notification Service Extension.
Follow the below steps to set up Notification Service Extension:
1. Create a Notification Service Extension Target:
After the target is created, Activate the scheme for Extension when prompted for the same.
2. Set the deployment target
Make sure to set the deployment target the same as the main app target
3. Integrate MoEngageRichNotification framework to Extension:
Integrate using CocoaPod
For integrating through CocoaPod, include MoEngageRichNotification pod for your Notification Service Extension as shown below, and run the pod update / install command :
target "NotificationService" do
pod 'MoEngage-iOS-SDK/RichNotification','~>9.18.0'
end
Integrate using Swift Package Manager
To integrate via SPM use the https://github.com/moengage/MoEngage-iOS-SDK.git GitHub url link and set the branch as master or required version.
Manual Integration
info |
Note
|
4. Set the App Group ID :
First, select your Main App Target and select Capabilities do the changes as shown in the image below:
info |
App Group ID Recommendation We recommend having a separate App Group ID set for MoEngage with the format group.{app bundle id}.MoEngage. And make sure the same app group id is enabled for all the targets where MoEngage is being used. |
Provide the App Group ID to SDK
Provide the App Group ID selected in Capabilities in MoEngageSDKConfig instance while initializing the SDK as shown below:
let sdkConfig = MoEngageSDKConfig(withAppID: "MoEngage Workspace ID")
sdkConfig.appGroupID = "App Group ID"
MoEngageSDKConfig* sdkConfig = [[MoEngageSDKConfig alloc] initWithAppID:"MoEngage Workspace ID"];
sdkConfig.appGroupID = @"App Group ID";
Set the App Group Id to Extension
Turn ON App Groups for your notification service extension target and enable the same App group ID that was selected for the App Target(In the above steps).
5. Set the KeyChain Sharing
If storage encryption is enabled add the KeyChain Sharing capability and set the value as the same as of main App target.
Refer to the doc for more on Storage encryption and KeyChain sharing
6. Code Changes in Notification Service Extension:
import UserNotifications
// 1st Step
import MoEngageRichNotification
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
// 2nd Step
MoEngageSDKRichNotification.setAppGroupID("YOUR APPGROUP ID")
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
// 3rd Step
MoEngageSDKRichNotification.handle(richNotificationRequest: request, withContentHandler: contentHandler)
}
override func 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.
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}
#import "NotificationService.h"
// 1st Step
@import MoEngageRichNotification;
@interface NotificationService ()
@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;
@end
@implementation NotificationService
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
@try {
// 2nd Step
[MoEngageSDKRichNotification setAppGroupID: @"YOUR APPGROUP ID"]
self.contentHandler = contentHandler;
self.bestAttemptContent = [request.content mutableCopy];
// 3rd Step
[MoEngageSDKRichNotification handleWithRichNotificationRequest:request withContentHandler:contentHandler];
} @catch (NSException *exception) {
NSLog(@"MoEngage : exception : %@",exception);
}
}
- (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.
self.contentHandler(self.bestAttemptContent);
}
@end
Refer to the code above and make the following changes:
- Import MoEngageRichNotification framework.
- Set the App Group ID selected in the settings earlier using setAppGroupID(_:) method.
- Call handle(richNotificationRequest:withContentHandler:) method.
warning |
Rich Notification Media Limitations:
|
check_circle |
Image Guidelines
|