MBurger Engagement Platform
User GuideHeadless CMSEngagement Platform
  • 🍔MBurger Engagement Platform 🍔
  • 🍏 iOS Docs
    • Messages
      • Installation
      • Initialization
      • Push notifications
        • Rich Notifications
      • Stylize in app messages
      • Message Metrics
    • Audience
      • Installation
      • Initialization
      • Tracked data
      • Tags
      • Identify a user
      • Location
    • Automation
      • Installation
      • Initialization
      • Triggers
      • Send events
      • View Tracking
  • 📱Android Docs
    • Messages
      • Prerequisites for Push Notifications
      • Installation
      • Initialization
      • Stylization and parameters
      • Push notifications
      • Message Metrics
    • Audience
      • Installation
      • Initialization
      • Tracked data
      • Tags
      • Custom Id
      • Mobile User Id
      • Location Data
    • Automation
      • Installation
      • Initialization
      • Triggers
      • Add events
      • View Tracking
      • Stop/Pause tracking
  • 🔷Flutter Docs
    • Messages
      • Installation
      • Initialization
      • Stylize in app messages
      • Push notifications
        • Rich Notifications
      • Message Metrics
    • Audience
      • Installation
      • Initialization
      • Tracked data
      • Tags
      • Custom Id
      • Mobile User Id
      • Location Data
    • Automation
      • Installation
      • Initialization
      • Triggers
      • Send events
      • View Tracking
Powered by GitBook
On this page
  • 1. Create a Notification Service target
  • 2. Download the media

Was this helpful?

  1. iOS Docs
  2. Messages
  3. Push notifications

Rich Notifications

PreviousPush notificationsNextStylize in app messages

Last updated 4 years ago

Was this helpful?

From MBurger you can send medias (images/videos/audio) with a push notification. The link of the media will be sent in the payload of the notifications in the media_url field. To view the media sent in the notifications follow tihs steps.

1. Create a Notification Service target

In Xcode go to File -> New Target and choose

. This will create a class that will intercept all push notification sent to the app, you will be able to change its content from this class.

2. Download the media

In the notification service class use this code to download the media and attach to the push notification.

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)
        
        if let bestAttemptContent = bestAttemptContent {
            if let mediaUrl = request.content.userInfo["media_url"] as? String, let fileUrl = URL(string: mediaUrl) {
                let type = request.content.userInfo["media_type"] as? String
                downloadMedia(fileUrl: fileUrl, type: type, request: request, bestAttemptContent: bestAttemptContent) {
                    contentHandler(bestAttemptContent)
                }
            } else {
                contentHandler(bestAttemptContent)
            }
        }
    }
    
    func downloadMedia(fileUrl: URL, type: String?, request: UNNotificationRequest, bestAttemptContent: UNMutableNotificationContent, completion: @escaping () -> Void) {
        let task = URLSession.shared.downloadTask(with: fileUrl) { (location, _, _) in
            if let location = location {
                let tmpDirectory = NSTemporaryDirectory()
                let tmpFile = "file://".appending(tmpDirectory).appending(fileUrl.lastPathComponent)
                let tmpUrl = URL(string: tmpFile)!
                do {
                    try FileManager.default.moveItem(at: location, to: tmpUrl)
                    
                    var options: [String: String]? = nil
                    if let type = type {
                        options = [String: String]()
                        options?[UNNotificationAttachmentOptionsTypeHintKey] = type
                    }
                    if let attachment = try? UNNotificationAttachment(identifier: "media." + fileUrl.pathExtension, url: tmpUrl, options: options) {
                        bestAttemptContent.attachments = [attachment]
                    }
                    completion()
                } catch {
                    completion()
                }
            }
        }
        task.resume()
    }

    override func serviceExtensionTimeWillExpire() {
        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
            contentHandler(bestAttemptContent)
        }
    }
}
🍏
Notification Service Extension