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

Flutter开发如何开启通知声音?已集成FCM与flutterLocalNotificationsPlugin

Flutter通知声音播放功能配置方案

Android端配置

  • 通知通道属性重置:Android 8.0及以上系统中,通知通道一旦创建后属性无法修改,如果你之前创建过ID为com.testApp的通道且未开启声音,需要卸载应用重装或者修改通知通道ID才能生效新配置。
  • 音频资源放置:将你用到的slow_spring_board音频文件(支持mp3、wav等格式)放到android/app/src/main/res/raw目录下,若没有raw文件夹可手动新建。
  • 权限配置:在android/app/src/main/AndroidManifest.xml中添加通知权限声明:
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

Android 13及以上系统需要在应用启动时动态申请该通知权限,用户同意后才能正常弹出通知并播放声音。

iOS端配置

  • 补充通知声音配置:你当前的IOSNotificationDetails未配置声音参数,需要添加sound属性:
    先将音频文件(iOS推荐使用aiff、caf、wav格式)放到iOS项目的Runner/Resources目录下,同时在Xcode中确认该文件已被添加到应用的Bundle资源中。
  • 权限配置:在申请通知权限时,必须包含声音权限选项,iOS端需要在AppDelegate中注册通知时指定UNAuthorizationOptionSound权限。
  • 前台通知兼容:如果需要应用在前台运行时也能播放通知声音,需要实现willPresentNotification回调,返回包含sound的展示选项。

完整修改后的代码示例

// Android端配置
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
    // 若之前创建过同ID通道,建议修改ID避免缓存问题
    'com.testApp.v2', 
    'fcm通知', 
    '应用推送通知通道',
    icon: 'notif_icon',
    color: Color(0xFFFFC33E),
    importance: Importance.max,
    priority: Priority.max,
    playSound: true,
    sound: RawResourceAndroidNotificationSound('slow_spring_board'),
    groupKey: 'com.testApp');
// iOS端配置补充sound参数
var iOSPlatformChannelSpecifics = IOSNotificationDetails(
  sound: 'slow_spring_board.aiff', // 替换为你实际的音频文件名
  threadIdentifier: 'com.testApp'
);
var platformChannelSpecifics = NotificationDetails(
    android: androidPlatformChannelSpecifics,
    iOS: iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
  0,
  notification.title,
  notification.body,
  platformChannelSpecifics,
  payload: json.encode(message.data),
);

系统权限检查

你截图中显示系统设置里的应用通知声音默认处于关闭状态,代码配置完成后,需要引导用户手动进入系统应用设置页面,开启通知的声音开关,否则无法播放通知声音。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 23:09:03