Module responsible for delivering and displaying targeted notifications in the app. These notifications can be presented in 2 formats:
- Common push notification with image or carousel of images.
- Inbox of messages.
Before starting
To integrate MDM notifications module, the application needs to have push notifications from Apple integrated.
Follow the instructions in Apple Developer to obtain an encryption key and Apple Key ID.
To integrate MDMNotification, follow the next steps:
1. Go to Capabilities tab and activate Push Notifications service:
2. After starting the module, add the following code to your AppDelegate, at didFinishLaunchingWithOptions
method to request notification permission from iOS:
self.notificationCenter = UNUserNotificationCenter.current()
self.notificationCenter.delegate = self
self.notificationCenter.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
if granted {
OperationQueue.main.addOperation({
if !application.isRegisteredForRemoteNotifications {
application.registerForRemoteNotifications()
}
})
}
}
self.notificationCenter = [UNUserNotificationCenter currentNotificationCenter];
self.notificationCenter.delegate = self;
[self.notificationCenter requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
if (![application isRegisteredForRemoteNotifications]) {
[application registerForRemoteNotifications];
}
}];
}
}];
3. Register iOS given token to MDM, implementing the didRegisterForRemoteNotificationsWithDeviceToken
method of your AppDelegate:
MDMNotification.registerToken(deviceToken)
[MDMNotification registerToken:deviceToken];
4. Remove token from MDM, implementing the didFailToRegisterForRemoteNotificationsWithError
method of your AppDelegate:
MDMNotification.unregisterToken()
[MDMNotification unregisterToken];
5. To receive notifications sent by MDM, implement the didReceiveNotificationResponse
method of the UNUserNotificationCenterDelegate delegate in your AppDelegate:
let userInfo = response.notification.request.content.userInfo
if MDMNotification.isMDMNotification(userInfo) {
MDMNotification.processNotification(userInfo)
} else {
// Process your notification here
}
completionHandler();
NSDictionary *userInfo = response.notification.request.content.userInfo;
if ([MDMNotification isMDMNotification:userInfo]) {
[MDMNotification processNotification:userInfo];
} else {
// Process your notification here
}
completionHandler();
6. To open notifications sent by MDM, implement the willPresentNotification
method of the UNUserNotificationCenterDelegate delegate in your AppDelegate:
let userInfo = notification.request.content.userInfo
if MDMNotification.isMDMNotification(userInfo) {
MDMNotification.processNotification(userInfo)
} else {
// Open your notification here
}
NSDictionary *userInfo = notification.request.content.userInfo;
if ([MDMNotification isMDMNotification:userInfo]) {
[MDMNotification processNotification:userInfo];
} else {
// Open your notification here
}
7. To report receiving notifications sent by MDM, implement the Notification Service Extension. Important to keep the same version and language of the project:
8. Update Podfile as shown below, respecting the name given in the extension created and then run the pod update command on the terminal.
target 'MDMNotificationService' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for MDMNotificationService
pod 'MDMNotification'
end
9. Enable the App Groups option in the Capabilities tab, creating a group, preferably with the bundle identifier name of the main target, preceded by group:
10. Add to the Info.plist of your target and extension created the NSGroupIdentifier
key of type String filled with the name of the created group, as shown in the example below:
<key>NSGroupIdentifier</key>
<string>NAME OF THE GROUP</string>
11. Add to extension Info.plist created the NSAppTransportSecurity
key of Dictionary type filled with the NSAllowsArbritaryLoads
key of YES value, as shown in the example below:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbritaryLoads</key>
<true/>
</dict>
12. In the extension created, open the NotificationService class, import the MDMNotification
package, and extend the MDMNotificationService
class, as shown below:
import MDMNotification
class NotificationService: MDMNotificationService {
// Your code here
}
#import <MDMNotification/MDMNotification.h>
@interface NotificationService : MDMNotificationService
@end
13. Change the contents of the didReceiveNotificationRequest
method as shown below:
self.contentHandler = contentHandler
self.bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = self.bestAttemptContent {
if MDMNotification.isMDMNotification(bestAttemptContent.userInfo) {
super.didReceive(request, withContentHandler: contentHandler)
} else {
// Your code here
contentHandler(bestAttemptContent)
}
}
self.contentHandler = contentHandler;
self.bestAttemptContent = [request.content mutableCopy];
if ([MDMNotification isMDMNotification:self.bestAttemptContent.userInfo]) {
[super didReceiveNotificationRequest:request withContentHandler:contentHandler];
} else {
// Your code here
self.contentHandler(self.bestAttemptContent);
}
14. Change the contents of the serviceExtensionTimeWillExpire
method as shown below:
if let contentHandler = self.contentHandler, let bestAttemptContent = self.bestAttemptContent {
if MDMNotification.isMDMNotification(bestAttemptContent.userInfo) {
super.serviceExtensionTimeWillExpire()
} else {
// Your code here
contentHandler(bestAttemptContent)
}
}
if ([MDMNotification isMDMNotification:self.bestAttemptContent.userInfo]) {
[super serviceExtensionTimeWillExpire];
} else {
// Your code here
self.contentHandler(self.bestAttemptContent);
}
To implement image carousel display in the notification, follow the steps below:
1. To display notifications in image or carousel formats, implement the Notification Content Extension. Important to keep the same version and language of the project:
2. Update Podfile as shown below, respecting the name given in the extension created and then run the pod update command on the terminal.
target 'MDMNotificationContent' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for MDMNotificationContent
pod 'MDMNotification'
end
3. Activate in the Capabilities tab the App Groups option, selecting the created group:
4. Add to the extension Info.plist created the NSGroupIdentifier
key of type String filled with the name of the created group, as shown in the example below:
<key>NSGroupIdentifier</key>
<string>NAME OF THE GROUP</string>
5. Add to extension Info.plist created the NSAppTransportSecurity
key of Dictionary type filled with the NSAllowsArbritaryLoads
key of YES value, as shown in the example below:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbritaryLoads</key>
<true/>
</dict>
6. In the extension created, open the NotificationViewController file, import the MDMNotification
package, and extend the MDMNotificationViewController
class, as shown below:
import MDMNotification
class NotificationViewController: MDMNotificationViewController {
// Your code here
}
#import <MDMNotification/MDMNotification.h>
@interface NotificationViewController : MDMNotificationViewController
@end
7. Change the contents of the didReceiveNotification
method as shown below:
self.isHandsPush = MDMNotification.isMDMNotification(notification.request.content.userInfo)
if self.isHandsPush {
super.didReceive(notification)
} else {
// Your code here
}
self.isHandsPush = [MDMNotification isMDMNotification:notification.request.content.userInfo];
if (self.isHandsPush) {
[super didReceiveNotification:notification];
} else {
// Your code here
}
8. Change the contents of the didReceiveNotificationResponse
method as shown below:
if self.isHandsPush {
super.didReceive(response, completionHandler: completion)
} else {
// Your code here
}
if (self.isHandsPush) {
[super didReceiveNotificationResponse:response completionHandler:completion];
} else {
// Your code here
}
9. Open the extension extension’s Info.plist and change the UNNotificationExtensionCategory
key to the array type and add these two new items:
<key>UNNotificationExtensionCategory</key>
<array>
<string>carousel</string>
<string>image</string>
</array>
To implement Inbox display, follow the steps below:
For customizing the title in UINavigationBar, add the following code to the didFinishLaunchingWithOptions
method of your AppDelegate:
MDMInbox.setTitle("Hands Inbox")
[MDMInbox setTitle:@"Hands Inbox"];
For UINavigationBar and UITabBar color customization, add the following code to the didFinishLaunchingWithOptions
method of your AppDelegate:
MDMInbox.setNavigationBarColor("#312683")
MDMInbox.setTabBarColor("#312683")
MDMInbox.setTintColor("#C8D400")
[MDMInbox setNavigationBarColor:@"#312683"];
[MDMInbox setTabBarColor:@"#312683"];
[MDMInbox setTintColor:@"#C8D400"];
For background color customization, add the following code in the didFinishLaunchingWithOptions
method of your AppDelegate:
MDMInbox.setBackgroundColor("#312683")
[MDMInbox setBackgroundColor:@"#312683"];
For the implementation of the Inbox format, add the following code for when you want to display content:
let bundle = Bundle(identifier: "br.com.hands.mdm.libs.ios.notification")
let storyboard = UIStoryboard(name: "MDMInBox", bundle: bundle)
if let navigationController = storyboard.instantiateViewController(withIdentifier: "MDMInboxNavigationController") as? UINavigationController {
navigationController.modalPresentationStyle = .fullScreen
self.present(navigationController, animated: true, completion: nil)
}
NSBundle *bundle = [NSBundle bundleWithIdentifier:@"br.com.hands.mdm.libs.ios.notification"];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MDMInBox" bundle:bundle];
UINavigationController *navigationController = [storyboard instantiateViewControllerWithIdentifier:@"MDMInboxNavigationController"];
navigationController.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:navigationController animated:YES completion:nil];
Opt-out
To opt-out module, just call the following command:
MMDNotification.setOptOut(true)
[MDMNotification setOptOut:YES];
To undo the module opt-out, just call the following command:
MMDNotification.setOptOut(false)
[MDMNotification setOptOut:NO];