Push Notifications Module

Adding Notification Service Extension

Notification Service Extension helps you get rich push messages (image, video).

  1. Open Xcode and follow File > New > Target

  2. Select Notification Service Extension and click the Next button.

     

  3. Type NotificationService in the Product Name field and click the Finish button.

     

  4. After clicking the Finish button, click the Cancel button in the dialog that will open.

    If you click the Activate button, the application target will be NotificationService. This is not a problem. You can fix it by selecting your application from the Scheme field.

  5. Select your project from the left menu and select NotificationService under TARGETS in the drop-down area. In the Deployment Info section, set the Target field to iOS 11.0.

     

  6. Open the NotificationService.swift file and replace the codes in it with the following codes.

    import UserNotifications import RelatedDigitalIOS 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) RelatedDigital.initialize(organizationId: "YOUR_ORGANIZATION_ID", profileId: "YOUR_PROFILE_ID", dataSource: "YOUR_DATA_SOURCE", launchOptions: nil) RelatedDigital.enablePushNotifications(appAlias: "YOUR_APP_ALIAS", launchOptions: nil, appGroupsKey: "YOUR_APP_GROUPS_KEY") RDPush.didReceive(bestAttemptContent, withContentHandler: contentHandler) } override func serviceExtensionTimeWillExpire() { guard let contentHandler = self.contentHandler else { return; } guard let bestAttemptContent = self.bestAttemptContent else { return; } contentHandler(bestAttemptContent) } }
  7. Make sure "Copy only when installing" is not selected. First select your Main Target, then open Build Phases > Embed App Extensions. Here, deselect if Copy only when installing is selected. If this is selected, you cannot use images in your pushes.

Adding Notification Content Extension

Note: If you are going to use Carousel Push in your push message submissions, you must add this.

  1. Open Xcode and follow File > New > Target

  2. Select Notification Content Extension and click the Next button.

     

  3. Type NotificationContent in the Product Name field and click the Finish button.

     

  4. After clicking the Finish button, click the Cancel button in the dialog that will open.

    1. If you click the Activate button, the application Target will be NotificationContent. This is not a problem. You can fix it by selecting your application from the Scheme field.

  5. Select your project from the left menu and select NotificationContent under TARGETS in the drop-down area. In the Deployment Info section, set the Target field to iOS 11.0.

     

  6. Delete the MainInterface.storyboard and NotificationContent.swift files. Then create a swift file named RDNotificationViewController under the NotificationContent folder. Note that the Info.plist file is the same as in this link.

     

    If prompted to create an Objective-C Bridging Header, click the Don't Create button.

     

  7. Open EMNotificationViewController.swift and replace the codes in it with the following.

import UIKit import UserNotifications import UserNotificationsUI import RelatedDigitalIOS @objc(RDNotificationViewController) class RDNotificationViewController: UIViewController, UNNotificationContentExtension { let carouselView = PushNotificationCarousel.initView() var completion: ((_ url: URL?, _ bestAttemptContent: UNMutableNotificationContent?) -> Void)? var notificationRequestIdentifier = "" func didReceive(_ notification: UNNotification) { notificationRequestIdentifier = notification.request.identifier RelatedDigital.initialize(organizationId: "YOUR_ORGANIZATION_ID", profileId: "YOUR_PROFILE_ID", dataSource: "YOUR_DATA_SOURCE", launchOptions: nil) RelatedDigital.enablePushNotifications(appAlias: "YOUR_APP_ALIAS", launchOptions: nil, appGroupsKey: "YOUR_APP_GROUPS_KEY") carouselView.didReceive(notification) } func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void) { carouselView.didReceive(response, completionHandler: completion) } override func loadView() { completion = { [weak self] url, bestAttemptContent in if let identifier = self?.notificationRequestIdentifier { UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: [identifier]) UNUserNotificationCenter.current().getDeliveredNotifications(completionHandler: { notifications in bestAttemptContent?.badge = NSNumber(value: notifications.count) }) } if let url = url { if #available(iOSApplicationExtension 12.0, *) { self?.extensionContext?.dismissNotificationContentExtension() } self?.extensionContext?.open(url) } else { if #available(iOSApplicationExtension 12.0, *) { self?.extensionContext?.performNotificationDefaultAction() } } } carouselView.completion = completion carouselView.delegate = self self.view = carouselView } } /** Add if you want to track which carousel element has been selected */ extension RDNotificationViewController: PushCarouselDelegate { func selectedItem(_ element: RDPushMessage.Element) { // Add your work... print("Selected element is => \(element)") } }

We have to add Related Digital iOS SDK for NotificationContent and NotificationService. There are different definitions for Cocoapods and SPM.

 

Cocoapods

Edit your podfile as follows and go to your project directory from the terminal again and type pod install and press enter.

 

platform :ios, '11.0' target 'YOUR_PROJECT_NAME' do use_frameworks! pod 'RelatedDigitalIOS' post_install do |installer| installer.pods_project.targets.each do |target| if target.name == 'RelatedDigitalIOS' target.build_configurations.each do |config| config.build_settings['APPLICATION_EXTENSION_API_ONLY'] = 'No' end end end end end target 'NotificationService' do # Comment the next line if you don't want to use dynamic frameworks use_frameworks! pod 'RelatedDigitalIOS' # Pods for RelatedDigital end target 'NotificationContent' do # Comment the next line if you don't want to use dynamic frameworks use_frameworks! pod 'RelatedDigitalIOS' # Pods for RelatedDigital end

 

SPM

For both NotificationService and NotificationContent, you must add the RelatedDigitalIOS module under Frameworks and Libraries under General.

Adding App Groups

After completing all of them, you need to add App Groups to Targets.

Add App Groups under Signing & Capabilities respectively in Targets marked on the left in the image. Type the App Groups name as group.YOUR_APP_BUNDLE_IDENTIFIER.relateddigital.

Getting Push Notification Permission

You can get permission from users in two different ways. The first of these is Provisional Push. In Provisional Push, the user is not asked for any permission by the application. In the first push you send, the Notification Center provides the user with the options to leave notifications on or off.

The other is the standard notification permission popup. It is presented as a popup when the user opens the app.

Provisional Push

If you want to get provisional push permission, add the following code to didFinishLaunchingWithOptions. For iOS 12 or older version users, this function will work as askForNotificationPermission.

If you do not want to get provisional push permission, add the following code to didFinishLaunchingWithOptions.

If users allow sending notifications, the following method works and registers to Euromessage systems by getting tokens from APNS.

Sending the Push Opened Report

Add the codes below to be able to report whether your users click on push messages via the RMC panel.

SceneDelegate.swift

Add the following code block inside the willConnectTo function.

Sending information to RMC

Run the code below when the user is a member of your application, logged in and the application is first opened.

If the user has not given e-mail permission, you should send the permission value as false.

In order for your data coming from the mobile channel to be uploaded to RMC, make sure that the data coming from the mobile channel comes with this reference, whatever your reference value (KEY_ID* or E-Mail) you used in your active RMC account.

*KEY_ID: The id values used by the application owner to deduplicate its customer. These may be expressed by different names such as CRM id, userid, customerid, accountid.

IYS E-Mail Registration

Add the IYS code just before the Euromsg.sync() code.

 

Copyright 2020 Related Digital