丰富推送通知 - 视频无法在通知内容扩展中播放

时间:2017-03-03 12:53:32

标签: ios objective-c push-notification apple-push-notifications rich-notifications

我正在处理富通知Notification Content Extension并且能够成功加载imagesgif,如下面的屏幕截图:

enter image description here

现在我正在尝试播放视频,而我正在使用以下代码进行播放。

- (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];
    }
}

在NSLog中,我也获得了视频文件的URL。但那不会玩。如果有人有这个解决方案,请提供帮助。

感谢。

3 个答案:

答案 0 :(得分:4)

这是我用代码做的非常小的错误。如果我执行如下代码

,我们必须与startAccessingSecurityScopedResource核对
- (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];

            }                
        }
}

视频播放。 Boooom ...

答案 1 :(得分:0)

  1. 检查您的代码。确保资源已下载到您的磁盘。
  2. 如果你想播放网址,你应该从通知userInfo获取网址并在你的内容扩展中播放
  3. 解决 Ankur patel 的评论中提出的问题:

    1. 您应该创建通知内容扩展。
    2. - (void)didReceiveNotification:(UNNotification *)notification 是UNNotificationContentExtension协议

答案 2 :(得分:0)

这是在 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()
    }
}

通知内容扩展的使用条件还有一些,希望大家都遵守。

相关问题