0

I have played video in notification service extension by saving in the disk. Now I want to play streaming URL in notification service extension as an attachment. I have tried directly passing URL as an attachment, but it returns nil in the variable attach1.

Below is my code:

import UserNotifications
import UIKit

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)

        //Media
        func failEarly() {
            contentHandler(request.content)
        }

        guard let content = (request.content.mutableCopy() as? UNMutableNotificationContent) else {
            return failEarly()
        }

        guard let attachmentURL = content.userInfo["attachment_url"] as? String, let url = URL(string: attachmentURL) else {
            return failEarly()
        }

        // Saving streaming url video
        var attach1 : UNNotificationAttachment?
        do {
        attach1 = try UNNotificationAttachment(identifier: request.content.categoryIdentifier, url: url, options: nil)
        } catch {
            failEarly()
        }    

        content.attachments = [attach1] as! [UNNotificationAttachment]
        contentHandler(content.copy() as! UNNotificationContent)
    }

    override func serviceExtensionTimeWillExpire() {
        // Called just before the extension will be terminated by the system.
        // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
            contentHandler(bestAttemptContent)
        }
    }

}

My Streaming URL is from Youtube.

Any help will be appreciated.

Ravi B
  • 1,574
  • 2
  • 14
  • 31

1 Answers1

1

There are some scenarios you must need to know.

  1. If you are using content extension that time category must be same as declare in Info.plist file of UNNotificationContentExtension.
  2. UserNotificationAttachment can't store data url. i.e. it must contains fileUrl which is url of file downloaded in UNNotificationServiceExtension and return in completion.

So, your following code is totally wrong.

var attach1 : UNNotificationAttachment?
do {
    attach1 = try UNNotificationAttachment(identifier: request.content.categoryIdentifier, url: url, options: nil)
} catch {
    failEarly()
}    

I'm not sure but, youtube video url link can't play in AVPlayer, there should be extension of video in url required when use with AVPlayer.

Check the all the things properly. See UNNotificationServiceExtension & UNNotificationContentExtension for more info.

Mayur Karmur
  • 2,119
  • 14
  • 35