Integration of DR iOS SDK
Documentation for Integration of iOS SDK from DigitalReef
DigitalReef's iOS SDK can be integrated with the help of Cocoa Pods or using Swift Package manager.
Requirements
DigitalReef iOS SDK has been developed to work with iOS version 12 and above.
1. Installation of DR SDK
Using Cocoa Pods
Link to DR SDK : https://github.com/Digita1Reef/iOSDigitalReefSDKPods
For more information on including the CocoaPods to your project please refer to https://guides.cocoapods.org/using/using-cocoapods.html
Using Swift Package manager:
Link to DR SDK : https://github.com/Digita1Reef/iOSDRSDKSPM
For more information on adding DR SDK Dependency to your project through Swift package manger:
https://developer.apple.com/documentation/xcode/adding-package-dependencies-to-your-app
2. Add Local Config file
DigitalReef SDK needs a local configuration file in the form of plist file.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>organization</key>
<string>IU</string>
<key>organizationId</key>
<string>5a294f81993c8f1570c2550d</string>
<key>fallbackURL</key>
<string>https://sdk.digitalreef.com/v1/configs/remote</string>
<key>timerTriggerConfig</key>
<string>86400</string>
<key>version</key>
<string>1</string>
<key>buildType</key>
<string>SDK</string>
<key>fcmProjectId</key>
<string>iu-android-sdk</string>
<key>fcmSenderId</key>
<string>117519441630</string>
<key>fbRemoteProjectId</key>
<string>iu-android-sdk</string>
<key>fbRemoteAPIKey</key>
<string>AIzaSyAuiuXvkX3hbXTZv1q1KUh3PbvvTT2tb04</string>
<key>fbRemoteAppId</key>
<string>1:117519441630:android:7962cb3d01ff3cc2</string>
</dict>
</plist>
You would need to update the value of organization and organizationId in the above list.
Sample plist file looks as below:
3. Add Permissions
DR SDK needs few permissions which are listed below:
- NSUserTrackingUsageDescription
- UIBackgroundModes
- Background fetch
- Remote notifications
- Background processing
Please add NSUserTrackingUsageDescription as a key to your original Info.plist
file. Add the correct description for using the IDFA:
Add the following Background Modes:
4. Enable background task functionality
To enable the background task from our SDK, you need to insert BGTaskSchedulerPermittedIdentifiers
inside your info.plist file, this will create an array of strings, just insert our identifier: com.digitalreef.taks.bg
5. Create Notification Service Extension
To receive and display the Rich images in Push Notification, add the following code to NotificationService
extending UNNotificationServiceExtension
iOS Version
Make sure that the minimum iOS version is the same in both the main target and the rich notification target.
Reference : Rich Notifications from Apple
#import "NotificationService.h"
#import <DigitalReefSDK/DigitalReefSDK.h>
@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 {
self.contentHandler = contentHandler;
self.bestAttemptContent = [request.content mutableCopy];
BOOL drAd = request.content.userInfo[@"adAvailable"];
if(drAd == YES){
NSLog(@"DR SDK");
[DigitalReef includeMediaAttachmentWithRequest:request mutableContent:self.bestAttemptContent contentHandler:self.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.
self.contentHandler(self.bestAttemptContent);
}
@end
import UserNotifications
import DigitalReefSDK
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
let drAd: Bool = request.content.userInfo["adAvailable"] as? Bool ?? false
if drAd {
print("RichNotification NotificationService drAd")
DigitalReef.includeMediaAttachment(request: request, mutableContent: bestAttemptContent!, contentHandler: 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)
}
}
}
6: Add App Groups
Go to your app target and add a Capability called App Groups
. Do the same for your Rich Notification target:
Add the following App Groups to:
- Host Application's
Info.plist
- Rich Notification service extension's
Info.plist
App Group Data
App Group's data should be same in both Host Application and Rich notification service extension.
<key>dbAppGroup</key>
<string>group.db.YOURAPPNAME.com</string>
<key>userDefaultsAppGroup</key>
<string>group.YOURAPPNAME.com</string>
Once added it would be similar to the image below
App Groups Naming
AppGroups string must be different for each host application otherwise there will be a shared database storage and shared user defaults storage between different apps.
7: App Orientation (Optional)
Check the button at least once in order to add this key (UISupportedInterfaceOrientations) in the info.plist file or it can be added explicitly in the info.plist file by adding the key (UISupportedInterfaceOrientations) in the Info.plist
file
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
</array>
Host App Orientation
Add the orientation as per the Host App support. SDK does not need all orientation, and can work with only one orientation as well.
Disable Method Swizzling (optional)
In case you have another push SDK or an implementation of your own, you might want to turn off DR SDK's method swizzling.
DR SDK performs method swizzling on top of iOS' APNS delegate methods in order to capture relevant events from notifications and analytics. This means that when our SDK is present, it overrides whatever other APNS implementation already present in the app.
To disable method swizzling, please follow the steps outlined in this section.
Reference : Push Notification
Updated about 2 months ago