Ios 丰富的推送通知-视频赢得';无法在通知内容扩展中播放
我正在开发rich notification的Ios 丰富的推送通知-视频赢得';无法在通知内容扩展中播放,ios,objective-c,push-notification,apple-push-notifications,rich-notifications,Ios,Objective C,Push Notification,Apple Push Notifications,Rich Notifications,我正在开发rich notification的通知内容扩展,并且能够成功加载图像和gif,如下图所示: 现在我正在尝试播放视频,我正在为播放它做以下代码 - (void)didReceiveNotification:(UNNotification *)notification { //self.label.text = @"HELLO world";//notification.request.content.body; if(notification.request.con
通知内容扩展
,并且能够成功加载图像
和gif
,如下图所示:
现在我正在尝试播放视频,我正在为播放它做以下代码
- (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。但这不起作用。如果有人有这样的解决方案,请给予帮助
谢谢。这是我在代码中犯的一个很小的错误。如果我执行以下代码,我们必须与
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];
}
}
}
视频正在播放。呜呜
检查你的代码。确保资源已下载到您的磁盘
如果你想播放一个url,你应该从通知用户信息中获取url并在你的内容扩展中播放
要解决Ankur patel在评论中提出的问题,请执行以下操作:
您应该创建通知内容扩展李>
-(void)direceivenotification:(UNNotification*)通知
是一个未通知的ContentExtension协议
以下是在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()
}
}
使用通知内容扩展还有一些条件,我希望您能遵守所有这些条件。大家好,我可以在通知服务扩展中播放视频吗。如果是,那么如何进行(void)didReceiveNotification:(UNNotification*)notification您在NotificationService.h或Appdelegate.h中使用此方法了吗?我仍然无法使用notification Service Extension成功下载视频,但无法播放video@Nitin嘿,我需要你的帮助。你能帮我吗?当然可以连接skype nitin。Gohel10avLayerViewController()未知。很多事情都没有被描述。请描述一下。你可以查看我的文章以了解全部细节。这是有效的解决办法。此外,本文还附带了完整的代码。