When I use the Notification Content Extension in my iOS app and set the play pause button type to overlay, the button frame starts out showing the play icon even though I have run the extensionContext?.mediaPlayingStarted() method, which is supposed to show the pause icon in the button frame, indicating to the user that to pause the video, he should tap the pause button. Also, when I tap the button, it disappears. I suspect there is something else I'm supposed to do. It works just fine when I set the button type to default.
Here is my code for the NotificationViewController class if anyone wants to see it. It's verbose. I don't think it would help for you to look at it. I embed a view controller in the NotificationViewController. The view controller I embed depends on the media type I send in notification userInfo. I'm only working with the video media type.
class NotificationViewController: UIViewController, UNNotificationContentExtension {
private var privateVideoViewController: VideoViewController! = nil
private var privateMediaPlayPauseButtonType: UNNotificationContentExtensionMediaPlayPauseButtonType = .none
var mediaPlayPauseButtonType: UNNotificationContentExtensionMediaPlayPauseButtonType {
get {
return privateMediaPlayPauseButtonType
}
}
var mediaPlayPauseButtonFrame: CGRect {
get {
let buttonDimension: CGFloat = 36
let x: CGFloat = (view.frame.maxX - buttonDimension) / 2
let y: CGFloat = view.frame.maxY - 44
let buttonSize: CGSize = CGSize(width: buttonDimension, height: buttonDimension)
let buttonOrigin: CGPoint = CGPoint(x: x, y: y)
let frame: CGRect = CGRect(origin: buttonOrigin, size: buttonSize)
return frame
}
}
func mediaPlay() {
if let videoViewController = privateVideoViewController {
videoViewController.playerView.player?.play()
}
}
func mediaPause() {
if let videoViewController = privateVideoViewController {
videoViewController.playerView.player?.pause()
}
}
func didReceive(_ notification: UNNotification) {
let userInfo = notification.request.content.userInfo
// Get values from userInfo
guard let mediaTypeRawValue: Int64 = userInfo[NotificationUserInfoKeys.mediaType] as? Int64,
let mediaType: MediaType = MediaType(rawValue: mediaTypeRawValue),
let data: Data = notification.request.content.userInfo[NotificationUserInfoKeys.mediaURL] as? Data,
let mediaURL = URL(dataRepresentation: data, relativeTo: nil) else {
return
}
removeAllChildViewControllers()
var controller: UIViewController
switch mediaType {
case .nil:
controller = instantiatePlainViewController()
case .video:
controller = instantiateVideoViewController()
privateVideoViewController = controller as? VideoViewController
case .image:
controller = instantiateImageViewController()
case .audio:
controller = instantiateAudioViewController()
}
addChild(controller)
controller.view.frame = view.bounds
controller.view.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(controller.view)
NSLayoutConstraint.activate([
controller.view.leftAnchor.constraint(equalTo: view.leftAnchor),
controller.view.rightAnchor.constraint(equalTo: view.rightAnchor),
controller.view.topAnchor.constraint(equalTo: view.topAnchor),
controller.view.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
controller.didMove(toParent: self)
if var visionNotificationViewController: VisionNotification = controller as? VisionNotification {
let content = notification.request.content
visionNotificationViewController.body = content.body
}
switch mediaType {
case .nil:
break
case .video:
if let videoViewController = privateVideoViewController {
privateMediaPlayPauseButtonType = .default
videoViewController.setVideo(with: mediaURL)
extensionContext?.mediaPlayingStarted()
}
case .image:
...
case .audio:
...
}
}
private func removeAllChildViewControllers() {
for child in children {
child.willMove(toParent: nil)
child.view.removeFromSuperview()
child.removeFromParent()
}
}
private func instantiateVideoViewController() -> UIViewController {
guard let controller = storyboard?.instantiateViewController(withIdentifier: "VideoViewController")
as? VideoViewController
else { fatalError("Unable to instantiate an VideoViewController from the storyboard") }
return controller
}
}