5

I am working on rich notification's Notification Content Extension and have able to load images and gif successfully like following screenshot:

enter image description here

Now i am trying to play video and i am doing following code for playing it.

- (void)didReceiveNotification:(UNNotification *)notification {
    //self.label.text = @"HELLO world";//notification.request.content.body;

    if(notification.request.content.attachments.count > 0)
    {

        UNNotificationAttachment *Attachen = notification.request.content.attachments.firstObject;


        NSLog(@"====url %@",Attachen.URL);
        AVAsset *asset = [AVAsset assetWithURL:Attachen.URL];
        AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:asset];

        AVPlayer  *player = [AVPlayer playerWithPlayerItem:item];
        AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
        playerLayer.contentsGravity = AVLayerVideoGravityResizeAspect;
        player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
        playerLayer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

            [self.VideoPlayerView.layer addSublayer:playerLayer];
        [player play];
    }
}

In NSLog i get the file url of video as well. but that wont play. Kindly help if any one have that solution.

Thanks.

Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144

3 Answers3

4

It's Very small mistake i did with the code that. we must be check with startAccessingSecurityScopedResource if i do code like following

- (void)didReceiveNotification:(UNNotification *)notification {

    if(notification.request.content.attachments.count > 0)
    {
            UNNotificationAttachment *Attachen = notification.request.content.attachments.firstObject;

            if(Attachen.URL.startAccessingSecurityScopedResource)
            {

                NSLog(@"====url %@",Attachen.URL);
                AVAsset *asset = [AVAsset assetWithURL:Attachen.URL];
                AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:asset];

                AVPlayer  *player = [AVPlayer playerWithPlayerItem:item];
                AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
                playerLayer.contentsGravity = AVLayerVideoGravityResizeAspect;
                player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
                playerLayer.frame = CGRectMake(0, 0, self.VideoPlayerView.frame.size.width, self.VideoPlayerView.frame.size.height);

                [self.VideoPlayerView.layer addSublayer:playerLayer];
                [player play];

            }                
        }
}

And Video is playing. Boooom...

Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
  • hi Guys can I play video in Notification Service Extension. If yes then How ? - (void)didReceiveNotification:(UNNotification *)notification did you this method in NotificationService.h or Appdelegate.h I am still confuse using Notification Service Extension I successfully download video but can not play video – Ankur Patel Mar 01 '18 at 12:43
  • @Nitin Hey I need your help. Can you help me ? – Ekta Padaliya Apr 11 '18 at 14:06
  • sure connect at skype nitin.gohel10 – Nitin Gohel Apr 12 '18 at 07:16
0
  1. Check your code. make sure the resource is downloaded to your disk.
  2. If you want to play a url, you should get the url from the notification userInfo and play it in your Content Extension

To address the question asked in the comments by Ankur patel:

  1. You should create a Notification Content Extension.
  2. - (void)didReceiveNotification:(UNNotification *)notification is a UNNotificationContentExtension Protocol
g00glen00b
  • 41,995
  • 13
  • 95
  • 133
Frank
  • 320
  • 1
  • 8
0

Here is the solution to play video in Notification in Swift 5.

func didReceive(_ notification: UNNotification) {
    let content = notification.request.content
    titleLabel.text = content.title
    subtitleLabel.text = content.body
    
    playerController = AVPlayerViewController()
    preferredContentSize.height = 475
    
    // fetch your url string from notification payload.
    let urlString = "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerBlazes.mp4"
    if let url = URL(string: urlString) {
        guard let playerController = self.playerController else { return }
        let player = AVPlayer(url: url)
        playerController.player = player
        playerController.view.frame = self.playerBackgroundView.bounds
        playerBackgroundView.addSubview(playerController.view)
        addChild(playerController)
        playerController.didMove(toParent: self)
        player.play()
    }
}

There are some more conditions to use of Notification Content Extension, I hope you are following all of them.

nitin.agam
  • 1,949
  • 1
  • 17
  • 24