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

Android Firebase推送通知自定义mp3提示音不播放问题求助

问题根因

代码里存在4个直接导致自定义提示音不生效的问题:

  • super.onMessageReceived(message)调用位置错误。父类方法会在执行时自动使用系统默认通知通道弹出通知,你后续自定义通道、构建通知的逻辑要么被父类逻辑覆盖,要么发出的通知被父类发出的默认通知顶替,最终展示的是带默认提示音的系统默认通知。
  • 通知通道配置存在缓存。安卓8.0及以上版本中,通知通道一旦创建完成,后续重复调用createNotificationChannel不会更新已有通道的声音、优先级等配置,如果你之前已经创建过ID为MyNotifications的通道,后续设置的自定义声音不会生效。
  • 自定义音频的Uri写法错误。引用res/raw目录下的资源时,路径不需要带文件后缀,你代码里写的/raw/music.mp3会导致系统找不到对应音频资源,自动回退到系统默认提示音。
  • Logcat抛出如下警告,原因是你没有在AndroidManifest中配置Firebase Messaging的默认通知通道元数据,Firebase收到通知消息时会强制使用系统默认通道下发通知,不会读取你代码里自定义的通道配置:

W/FirebaseMessaging: Missing Default Notification Channel metadata in AndroidManifest. Default value will be used.

修复步骤
  1. 调整super.onMessageReceived()的调用位置,放到所有自定义通知逻辑执行完成之后,或者直接删除该调用(你已经手动实现了通知构建、发送的全流程逻辑,不需要父类默认实现,避免重复弹出通知)。
  2. 修正自定义音频的Uri路径,去掉.mp3后缀。
  3. 创建通知通道前先删除已存在的旧通道,避免旧配置缓存导致新参数不生效,也可以直接卸载重装应用清除旧通道配置。
  4. 在AndroidManifest.xml中添加Firebase默认通知通道配置,消除警告,让Firebase默认使用你自定义的通道。
修正后代码

Firebase消息服务代码

public class firebase_connection extends FirebaseMessagingService {
    private static final String CHANNEL_ID = "MyNotifications";
    @Override
    public void onMessageReceived(@NonNull RemoteMessage message) {
        String title = Objects.requireNonNull(message.getNotification()).getTitle();
        String body = message.getNotification().getBody();
        // 修正Uri:去掉.mp3后缀
        Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/raw/music");

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationManager manager = getSystemService(NotificationManager.class);
            // 先删除旧通道,清除历史配置缓存
            manager.deleteNotificationChannel(CHANNEL_ID);

            NotificationChannel channel = new NotificationChannel(CHANNEL_ID,"MyNotifications", NotificationManager.IMPORTANCE_DEFAULT);
            AudioAttributes aud_att = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                    .build();
            channel.setSound(sound,aud_att);
            manager.createNotificationChannel(channel);

            Notification.Builder notification = new Notification.Builder(this,CHANNEL_ID)
                    .setContentTitle(title)
                    .setContentText(body)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setAutoCancel(true);

            NotificationManagerCompat.from(this).notify(1,notification.build());
        }
        // 自定义逻辑全部执行完成后再调用super,避免提前触发默认通知逻辑
        super.onMessageReceived(message);
    }
}

AndroidManifest.xml配置(添加到<application>标签内部)

<meta-data
    android:name="com.google.firebase.messaging.default_notification_channel_id"
    android:value="MyNotifications" />
注意事项
  • 测试时确认设备通知音量未被关闭,未开启静音、勿扰模式。
  • 如果后续需要修改通知声音、优先级等通道参数,必须先删除旧通道再重新创建,否则新配置不会生效。
  • 放在res/raw目录下的音频文件名不要包含中文、特殊字符,全部使用小写字母、数字、下划线命名,避免系统解析资源失败。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.01 03:39:43