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

Flutter中Firebase推送通知自定义铃声无法触发的解决方法

Firebase推送自定义铃声不触发的排查与解决方法

1. 音频文件的放置与格式检查

  • iOS端:
    • 把alert.mp3放到项目主Bundle内(直接拖进Xcode,确保勾选"Copy items if needed"和对应target)。
    • 格式需符合苹果要求:线性PCM、MA4(IMA/ADPCM)、μLaw、aLaw,文件时长不超过30秒。
    • 注意文件名区分大小写,推送配置里的文件名要和实际文件完全一致。
  • Android端:
    • 将音频文件放到res/raw/目录下(没有raw文件夹就新建一个)。
    • 推送配置里的sound字段不需要带后缀名,直接写"sound": "alert"即可。

2. 推送Payload配置验证

  • iOS:确保aps字典内的sound字段正确配置,示例:
    {
      "aps": {
        "alert": "测试自定义铃声",
        "sound": "alert.mp3"
      }
    }
    
  • Android:在notification字段内设置sound,示例:
    {
      "notification": {
        "title": "自定义铃声通知",
        "body": "这是一条带自定义铃声的通知",
        "sound": "alert"
      }
    }
    
  • 用Firebase控制台发测试通知时,需在「高级选项」的「自定义数据」里手动添加sound键值对,控制台默认不会包含自定义铃声配置。

3. 客户端逻辑适配

  • iOS静默通知:如果推送是带content-available:1的静默通知,系统不会自动播放铃声,需要在didReceiveRemoteNotification回调里手动调用音频播放逻辑。
  • Android Data通知:如果推送用的是data payload(而非notification),需要客户端接收后自行构建通知并设置铃声,示例代码:
    Uri soundUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.alert);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, YOUR_CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_notification)
            .setContentTitle(notificationTitle)
            .setContentText(notificationBody)
            .setSound(soundUri);
    

4. Android通知渠道配置(8.0+)

Android 8.0及以上版本必须创建通知渠道,且渠道的铃声设置会覆盖推送payload里的配置:

  • 创建或更新渠道时指定自定义铃声:
    NotificationChannel channel = new NotificationChannel(YOUR_CHANNEL_ID, "自定义铃声渠道", NotificationManager.IMPORTANCE_DEFAULT);
    Uri soundUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.alert);
    channel.setSound(soundUri, AudioAttributes.DEFAULT_NOTIFICATION);
    NotificationManager notificationManager = getSystemService(NotificationManager.class);
    notificationManager.createNotificationChannel(channel);
    
  • 如果之前已创建渠道且用的是默认铃声,需要卸载重装APP或在系统设置里手动更新渠道铃声。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 14:45:30