Skip to content

Instantly share code, notes, and snippets.

@Hokila
Created July 27, 2017 15:07
Show Gist options
  • Save Hokila/9b929d4c49926d65f0e41bf12ffcada8 to your computer and use it in GitHub Desktop.
Save Hokila/9b929d4c49926d65f0e41bf12ffcada8 to your computer and use it in GitHub Desktop.
Put AVPlayerLayer on UserNotification, also add play pause button
/*
See LICENSE folder for this sample’s licensing information.
Abstract:
Contains ImageContentExtension's NotificationViewController.
*/
import UIKit
import UserNotifications
import UserNotificationsUI
import AVFoundation
/// - Tag: NotificationViewController
class NotificationViewController: UIViewController, UNNotificationContentExtension {
@IBOutlet var imageView: UIImageView!
@IBOutlet var likeLabel: UILabel!
var videoPlayer: AVPlayer?
var playerLayer: AVPlayerLayer?
func didReceive(_ notification: UNNotification) {
guard let attachment = notification.request.content.attachments.first else { return }
self.imageView.alpha = 0
// Get the attachment and set the image view.
if attachment.url.startAccessingSecurityScopedResource() {
self.videoPlayer = AVPlayer(url: URL(string: "https://bitdash-a.akamaihd.net/content/MI201109210084_1/m3u8s/f08e80da-bf1d-4e3d-8899-f0f6155f6efa.m3u8")!)
self.playerLayer = AVPlayerLayer(player: self.videoPlayer)
self.playerLayer?.frame = self.view.frame
self.view.layer.addSublayer(self.playerLayer!)
attachment.url.stopAccessingSecurityScopedResource()
}
}
func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void) {
// Handle various actions.
if response.actionIdentifier == "likeAction" {
likeLabel.isHidden = false
} else if response.actionIdentifier == "reactAction" {
becomeFirstResponder()
}
// Dont dismiss extension to allow further interaction.
completion(.doNotDismiss)
}
override var canBecomeFirstResponder: Bool {
// Need to become first responder to have custom input view.
return true
}
override var inputView: UIView? {
// Instantiate and return custom input view.
let reactionsViewController = ReactionsViewController()
reactionsViewController.delegate = self
return reactionsViewController.view
}
var mediaPlayPauseButtonFrame: CGRect {
return CGRect(x: self.view.frame.size.width - 50, y: self.view.frame.size.height - 50, width: 40, height: 40)
}
var mediaPlayPauseButtonTintColor: UIColor {
return .white
}
var mediaPlayPauseButtonType: UNNotificationContentExtensionMediaPlayPauseButtonType {
return .default
}
func mediaPlay() {
self.videoPlayer?.play()
}
func mediaPause() {
self.videoPlayer?.pause()
}
}
extension NotificationViewController: ReactionsViewDelegate {
func didSelectReaction(_ reaction: String?) {
let reactionLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
reactionLabel.font = UIFont.systemFont(ofSize: 22)
// Set and position label text to reaction that was selected.
reactionLabel.textAlignment = .center
reactionLabel.text = reaction
// Place label in random location in view.
let originX = arc4random_uniform(UInt32(view.bounds.width))
let originY = arc4random_uniform(UInt32(view.bounds.height))
reactionLabel.center = CGPoint(x: CGFloat(originX), y: CGFloat(originY))
// Rotate label to a random angle.
let rotation = Float(arc4random_uniform(360))
reactionLabel.transform = CGAffineTransform(rotationAngle: CGFloat(.pi * rotation / 180.0))
self.view.addSubview(reactionLabel)
}
func didCompleteReacting() {
// Resign first responder to dismiss custom input view.
resignFirstResponder()
}
}
@irfaan
Copy link

irfaan commented May 28, 2018

This was incredibly useful. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment