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

Android API26+:FCM通知无默认声音问题及解决咨询

Hey,我来帮你搞定这个FCM通知没声音的问题,先排查原因,再给你一套靠谱的系统默认通知音设置方案~

问题排查:为什么你的通知没声音?

看了你的代码,核心问题有两个:

  • 你创建NotificationChannel时没有指定通知声音,虽然设置了IMPORTANCE_DEFAULT(这个级别本身支持声音),但系统不知道该用什么声音播放
  • 虽然代码里频道ID看起来一致,但最好用常量统一管理,避免拼写错误导致通知绑定到错误频道
解决方案:设置系统默认通知音

Android提供了RingtoneManager工具类,可以轻松获取系统默认的通知铃声Uri,把它绑定到NotificationChannel上,整个频道的通知就都会用这个声音了。

修改后的完整代码示例

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// 用常量统一管理频道ID,彻底避免拼写错误
final String CHANNEL_ID = "xxx";
CharSequence name = "xxx";
String description = "xxx";

// IMPORTANCE_DEFAULT对应正常的通知声音+震动,需要更强提醒可以用IMPORTANCE_HIGH
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
mChannel.setDescription(description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});

// 关键步骤:获取系统默认通知音并绑定到频道
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mChannel.setSound(defaultSoundUri, AudioAttributes.DEFAULT_NOTIFICATION);

mNotificationManager.createNotificationChannel(mChannel);

// 构建通知(注意频道ID要和上面的常量完全一致)
Notification notification = new Notification.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.volusha_notifications)
        .setContentText(text)
        .setChannelId(CHANNEL_ID)
        .setContentIntent(pendingIntent)
        .setContentTitle(title)
        .setAutoCancel(true)
        // 若需单独给这个通知设置专属声音(覆盖频道设置),可以添加这行,一般不需要
        // .setSound(defaultSoundUri)
        .build();

mNotificationManager.notify(new Random().nextInt(), notification);

额外注意事项

  • 频道属性修改限制:Android的通知频道一旦创建,重要属性(比如声音、重要级别)就无法通过代码修改了,除非用户卸载重装APP,或者你使用新的频道ID。测试时建议先卸载旧版本再安装新包。
  • 通知权限检查:确保APP的通知权限已经开启,并且允许声音提醒,可以在系统设置的APP通知管理里确认。
  • 重要级别对应行为:如果设置IMPORTANCE_LOW或更低级别,通知会静默显示无声音,所以务必使用IMPORTANCE_DEFAULT或IMPORTANCE_HIGH。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:31:54