iosobjective-cpush-notificationapple-push-notificationsrich-notifications

Rich Push Notification - Video won't play in Notification Content Extension


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.


Solution

  • 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...