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

iOS端Awesome Notifications插件自定义通知音效失效问题

Flutter Awesome Notifications 自定义通知音效无法播放的解决方法
  • 检查音效文件格式与路径配置

    • 双平台对音效格式支持不同:Android仅兼容MP3、WAV、OGG;iOS支持AIFF、WAV、MP3。你用的.aiff格式在Android上会失效,建议换成MP3格式适配全平台。
    • 务必在pubspec.yaml中正确声明assets路径,否则插件无法读取文件:
      flutter:
        assets:
          - assets/sounds/
      
    • 注意路径大小写(Android对路径大小写敏感),文件名不要带空格或特殊字符。
  • 修正渠道与通知的音效配置逻辑

    • Android通知渠道一旦创建就无法修改,若之前创建过不带自定义音效的basic_channel,必须卸载App重装才能更新渠道配置。
    • 双平台的音效路径写法不同,需区分处理:
      • Android:将音效文件放到android/app/src/main/res/raw目录下,路径用resource://raw/文件名(无需带后缀)。
      • iOS:直接使用assets/sounds/文件名.mp3格式。
    • 优化后的代码示例:
      import 'dart:io';
      import 'package:flutter/material.dart';
      import 'package:awesome_notifications/awesome_notifications.dart';
      import 'dart:math';
      
      // 初始化插件
      AwesomeNotifications().initialize(
        'resource://drawable/res_app_icon',
        [
          NotificationChannel(
            channelKey: 'basic_channel',
            channelName: 'Basic Notifications',
            defaultColor: Colors.blue,
            importance: NotificationImportance.High,
            channelDescription: 'Description for basic notifications',
            playSound: true,
            soundSource: Platform.isAndroid ? 'resource://raw/water_droplet' : 'assets/sounds/water_droplet.mp3',
          ),
        ],
      );
      
      // 发送本地通知
      await AwesomeNotifications().createNotification(
        content: NotificationContent(
          id: Random().nextInt(100),
          channelKey: 'basic_channel',
          title: 'test',
          body: 'test',
          payload: {'schedule': 'immediate'},
          // 渠道已配置音效时,此处可省略customSound;若需单独指定,保持路径与渠道一致即可
          customSound: Platform.isAndroid ? 'resource://raw/water_droplet' : 'assets/sounds/water_droplet.mp3',
        ),
      );
      
  • 确认权限是否齐全

    • Android 13及以上版本,需在AndroidManifest.xml中添加通知权限:
      <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
      
    • 启动App时检查并请求通知权限:
      bool isAllowed = await AwesomeNotifications().isNotificationAllowed();
      if (!isAllowed) {
        await AwesomeNotifications().requestPermissionToSendNotifications();
      }
      
  • 更新插件到最新版本

    • 旧版本插件可能存在音效适配bug,在pubspec.yaml中更新到最新版:
      dependencies:
        awesome_notifications: ^latest_version
      

内容的提问来源于stack exchange,提问作者Naif Bin Khunayn

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 08:56:10