Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Add Notification Content Extension target to your project and name it RelatedDigitalNotificationContent. Change this service's target iOS version to 11.0. Remove newly added files under RelatedDigitalNotificationContent except Info.plist. Then add EMNotificationViewController.swift file with the following content.

    Code Block
    languageswift
    import UIKit
    import UserNotifications
    import UserNotificationsUI
    import Euromsg
    
    @objc(EMNotificationViewController)
    class EMNotificationViewController: UIViewController, UNNotificationContentExtension {
        
        let carouselView = EMNotificationCarousel.initView()
        var completion: ((_ url: URL?, _ bestAttemptContent: UNMutableNotificationContent?) -> Void)?
        
        var notificationRequestIdentifier = ""
        
        func didReceive(_ notification: UNNotification) {
            notificationRequestIdentifier = notification.request.identifier
            Euromsg.configure(appAlias: "APP_ALIAS", launchOptions: nil, enableLog: true)
            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 EMNotificationViewController: CarouselDelegate {
        
        func selectedItem(_ element: EMMessage.Element) {
            // Add your work...
            print("Selected element is => \(element)")
        }
        
    }

  2. Add or replace the following lines in newly added RelatedDigitalNotificationContent/Info.plist

    Code Block
    <key>NSExtension</key>
    <dict>
      <key>NSExtensionAttributes</key>
      <dict>
        <key>UNNotificationExtensionUserInteractionEnabled</key>
        <true/>
        <key>UNNotificationExtensionDefaultContentHidden</key>
        <false/>
        <key>UNNotificationExtensionCategory</key>
        <string>carousel</string>
        <key>UNNotificationExtensionInitialContentSizeRatio</key>
        <real>1</real>
      </dict>
      <key>NSExtensionPrincipalClass</key>
      <string>RelatedDigitalNotificationContent.EMNotificationViewController</string>
      <key>NSExtensionPointIdentifier</key>
      <string>com.apple.usernotifications.content-extension</string>
    </dict>
    

  3. Add below lines to your Podfile's root level.

    Code Block
    target 'RelatedDigitalNotificationContent' do
      use_native_modules!
      
      pod 'Euromsg', '>= 2.0.0'
    end

  4. In Xcode, select RelatedDigitalNotificationContent target and add below files to Build Phases->Copy Bundle Resources section. Select Create folder references when prompted. Pods/Euromsg/Euromsg/Classes/EMNotificationCarousel/CarouselCell.xib Pods/Euromsg/Euromsg/Classes/EMNotificationCarousel/EMNotificationCarousel.xib

  5. Make sure your deployment target is ios 10.

    Code Block
    platform :ios, '10.0'

  6. Execute pod install then run.

App Groups

Enable App Groups Capability for your targets. App Groups allow your app to execute code when a notification is recieved, even if your app is not active. This is required for Related Digital's analytics features and to store and access notification payloads of the last 30 days.

...