BrightCoveOS Android:如何为下载通知添加PendingIntent并自定义通知
解决BrightCoveOS Android下载通知自定义问题
一、彻底隐藏BrightCove默认下载通知
你尝试的RequestConfig.setNotificationVisibility(RequestConfig.VISIBILITY_HIDDEN)未生效,大概率是配置方式不完整或版本兼容问题,可尝试以下两种方案:
方案1:禁用BrightCove默认下载服务
在AndroidManifest.xml中找到BrightCove的DownloadService声明,添加禁用配置,彻底关闭默认通知服务:
<service android:name="com.brightove.player.download.DownloadService" android:enabled="false" />
方案2:完整配置RequestConfig
创建MediaDownloadable时,用Builder模式完整配置通知隐藏参数,覆盖所有默认行为:
RequestConfig requestConfig = new RequestConfig.Builder() .setNotificationVisibility(RequestConfig.VISIBILITY_HIDDEN) .setShowNotification(false) // 部分SDK版本需额外添加此参数 .build(); mediaDownloadable = MediaDownloadable.create(context, video, downloadEventListener, requestConfig);
二、自定义下载通知并添加点击PendingIntent
通过监听MediaDownloadable的下载生命周期事件,手动创建、更新自定义通知,并配置点击跳转逻辑:
1. 实现DownloadEventListener监听状态
private DownloadEventListener downloadEventListener = new DownloadEventListener() { @Override public void onDownloadStarted(Video video) { // 下载开始时显示自定义通知 showCustomDownloadNotification(video, 0); } @Override public void onDownloadProgress(Video video, long bytesDownloaded, long totalBytes) { // 实时更新下载进度 int progress = (int) ((bytesDownloaded * 100) / totalBytes); updateCustomDownloadNotification(video, progress); } @Override public void onDownloadCompleted(Video video) { // 下载完成更新通知状态 updateCustomDownloadNotification(video, 100); } @Override public void onDownloadFailed(Video video, Throwable throwable) { // 下载失败显示错误提示 showDownloadFailedNotification(video); } };
2. 创建通知及点击PendingIntent
private static final int DOWNLOAD_NOTIFY_ID = 1001; private NotificationManager notificationManager; // 初始化通知管理器(适配Android O及以上渠道) private void initNotificationManager() { notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel( "brightcove_download", "视频下载", NotificationManager.IMPORTANCE_LOW ); notificationManager.createNotificationChannel(channel); } } // 显示下载中通知 private void showCustomDownloadNotification(Video video, int progress) { // 构建点击跳转Intent Intent intent = new Intent(this, YourTargetActivity.class); intent.putExtra("video_id", video.getId()); PendingIntent pendingIntent = PendingIntent.getActivity( this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE ); NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "brightcove_download") .setSmallIcon(R.drawable.ic_download) .setContentTitle(video.getName()) .setContentText("正在下载:" + progress + "%") .setPriority(NotificationCompat.PRIORITY_LOW) .setContentIntent(pendingIntent) .setAutoCancel(true) .setProgress(100, progress, false); notificationManager.notify(DOWNLOAD_NOTIFY_ID, builder.build()); } // 更新通知进度或状态 private void updateCustomDownloadNotification(Video video, int progress) { // 复用上述Builder逻辑,更新contentText和progress即可 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "brightcove_download") .setSmallIcon(R.drawable.ic_download) .setContentTitle(video.getName()) .setContentText(progress == 100 ? "下载完成" : "正在下载:" + progress + "%") .setPriority(NotificationCompat.PRIORITY_LOW) .setAutoCancel(true) .setProgress(100, progress, false); notificationManager.notify(DOWNLOAD_NOTIFY_ID, builder.build()); } // 显示下载失败通知 private void showDownloadFailedNotification(Video video) { NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "brightcove_download") .setSmallIcon(R.drawable.ic_error) .setContentTitle(video.getName()) .setContentText("下载失败") .setPriority(NotificationCompat.PRIORITY_LOW) .setAutoCancel(true); notificationManager.notify(DOWNLOAD_NOTIFY_ID, builder.build()); }
注意:需在Activity初始化时调用initNotificationManager()。
三、结合PlayerNotificationManager实现播放通知自定义
BrightCove Player底层基于ExoPlayer,可通过以下步骤关联PlayerNotificationManager:
1. 获取BrightCove的ExoPlayer实例
BrightcoveVideoView brightcoveVideoView = findViewById(R.id.brightcove_video_view); ExoPlayer exoPlayer = (ExoPlayer) brightcoveVideoView.getVideoView().getPlayer();
2. 初始化PlayerNotificationManager
private static final int PLAYBACK_NOTIFY_ID = 1002; private PlayerNotificationManager playerNotificationManager; private void initPlayerNotificationManager() { playerNotificationManager = new PlayerNotificationManager.Builder( this, PLAYBACK_NOTIFY_ID, "brightcove_playback" ) .setMediaDescriptionAdapter(new PlayerNotificationManager.MediaDescriptionAdapter() { @Override public CharSequence getCurrentContentTitle(Player player) { return "正在播放:" + yourVideo.getName(); } @Nullable @Override public PendingIntent createCurrentContentIntent(Player player) { // 设置播放通知点击跳转Intent Intent intent = new Intent(this, YourPlaybackActivity.class); return PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); } @Nullable @Override public CharSequence getCurrentContentText(Player player) { return null; // 可选:添加视频描述文本 } @Nullable @Override public Bitmap getCurrentLargeIcon(Player player, PlayerNotificationManager.BitmapCallback callback) { return null; // 可选:设置通知大图标 } }) .build(); // 关联ExoPlayer实例 playerNotificationManager.setPlayer(exoPlayer); }
四、额外注意事项
- 确保使用最新版BrightCove SDK,旧版本可能存在
RequestConfig参数不生效的bug; - 下载异常需检查:
MediaDownloadable初始化是否在主线程、存储权限是否配置正确(Android 10+需适配Scoped Storage); - 自定义通知优先级建议设为
IMPORTANCE_LOW,避免打扰用户。
内容的提问来源于stack exchange,提问作者Sumit Shaw
相关产品推荐
相关产品推荐

