You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android 11下ExoPlayer服务通知发布数分钟后自动隐藏如何解决

音乐播放器通知自动隐藏问题解决方案

下面是可直接落地的排查修复步骤:

1. 补全PlayerNotificationManager必要配置

你当前的Builder构造逻辑缺少必填的小图标配置,同时可关闭默认的自动取消通知规则,避免超时后通知被自动回收:

playerNotificationManager = new PlayerNotificationManager.Builder(this, NOTIFICATION_ID, NOTIFICATION_CHANNEL)
        .setSmallIcon(R.mipmap.ic_launcher) // 必须设置有效通知小图标,否则系统会判定为无效通知
        .setMediaDescriptionAdapter(new DescriptionAdapter())
        .setNotificationListener(listener)
        .setCustomActionReceiver(new CustomActionReceiver())
        .setAutoRemoveNotification(false) // 禁用播放停止后自动移除通知的逻辑
        .setNotificationVisibility(PlayerNotificationManager.VISIBILITY_PUBLIC) // 保持通知始终可见
        .build();

2. 修正前台服务绑定逻辑

调整onNotificationPosted回调中的逻辑,避免暂停状态下错误退出前台服务:

@Override
public void onNotificationPosted(int notificationId, Notification notification, boolean ongoing) {
    // 无论播放还是暂停状态都保持服务处于前台,避免通知被系统回收
    startForeground(notificationId, notification);
    // 如果需要在暂停时退后台但保留通知,不要调用stopForeground(true),仅可调用stopForeground(false)
}

3. 检查通知通道配置

创建通知通道时必须设置正确的重要等级,低重要等级的通知会被系统自动折叠甚至隐藏:

// 此代码需要在创建Notification前调用,建议放在Application的onCreate或者服务的onCreate中
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel channel = new NotificationChannel(
        NOTIFICATION_CHANNEL,
        "播放控制",
        NotificationManager.IMPORTANCE_LOW // 至少设置为IMPORTANCE_LOW,如需悬浮通知可设为IMPORTANCE_DEFAULT
    );
    channel.setSound(null, null); // 媒体播放通知不需要提示音
    channel.enableVibration(false);
    NotificationManager manager = getSystemService(NotificationManager.class);
    manager.createNotificationChannel(channel);
}

4. 适配高版本安卓系统权限

  • 安卓12及以上需在AndroidManifest.xml中声明媒体前台服务权限:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
  • 同版本下服务声明需添加前台服务类型:
<service
    android:name=".你的音乐服务类名"
    android:foregroundServiceType="mediaPlayback"
    android:exported="false" />
  • 安卓13及以上需动态申请POST_NOTIFICATIONS权限,无通知权限时通知会被系统自动清理。

5. 排查额外逻辑冲突

如果用原生通知也能复现问题,说明问题和ExoPlayer无关,可排查以下场景:

  • 你是否在服务的其他逻辑中调用了stopForeground或者stopSelf方法
  • 是否有电量优化相关逻辑禁止了应用的后台运行权限
  • 部分厂商ROM有独立的通知收归规则,需将应用加入通知白名单

内容的提问来源于stack exchange,提问作者user13674724

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 00:24:00