Versions Compared

Key

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

If you are using a version lower than 2.0.0 go here.

...

NotificationServiceExtension allows your iOS application to receive rich notifications with images, buttons, and badges. It's also required for Euromessage's analytics features.

  1. In Xcode File > New > Target...

  2. Select Notification Service Extension then press Next.

    Image Modified

  3. Enter the product name as NotificationService and press Finish.

    Image Modified

  4. Do not select Activate on the dialog that is shown after selecting Finish.

    Image Modified

    By canceling, you are keeping Xcode debugging your app, instead of the extension you just created.

    If you activated by accident, you can switch back to debug your app within Xcode (next to the play button).

  5. In the project navigator, select the top-level project directory and select the NotificationSerivce target in the project and targets list. Unless you have a specific reason not to, you should set the Deployment Target to be iOS 11.

    Image Modified
  6. Open NotificationService.swift and replace the whole file's contents with the following code.

    Code Block
    languageswift
    import UserNotifications
    import Euromsg
    
    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)
            Euromsg.configure(appAlias: "EuromsgIOSTest", launchOptions: nil, enableLog: true)
            Euromsg.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.

    Image Added

Add a Notification Content Extension

Note: If you are going to use carousel push sending, you need to add this.

  1. In Xcode File > New > Target...

  2. Select Notification Content Extension then press Next.

    Image Modified

  3. Enter the product name as NotificationContent and press Finish.

    Image Modified

  4. Do not select Activate on the dialog that is shown after selecting Finish.

    Image Modified

    By canceling, you are keeping Xcode debugging your app, instead of the extension you just created.

    If you activated by accident, you can switch back to debug your app within Xcode (next to the play button).

  5. In the project navigator, select the top-level project directory and select the NotificationContent target in the project and targets list. Unless you have a specific reason not to, you should set the Deployment Target to be iOS 11.

    Image Modified

  6. Delete MainInterface.storyboard and NotificationContent.swift files. Then create a swift file named EMNotificationViewController under the NotificationContent folder. Make sure the info.plist file is the same as in this link.

    Image Modified

    Don't Create Objective-C Bridging Header

    Image Modified

  7. Open EMNotificationViewController.swift and replace the whole file's contents with the following code

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: "EuromsgIOSTest", 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)")
    }   
}

...

Run the code below when the user successfully logs in, registers and the first time the application is opened.

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

*KEY_ID: These are the id values ​​used by the application owner to deduplicate the customer. These can be expressed with different names such as CRMid, userid, customerid, accountid.

...