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

iOS自定义29秒通知音效仅播放8秒,需调整哪些设置?是否修改AppDelegate.swift/info.plist?

问题

我设置了一段时长约29秒的自定义通知音效,但触发通知时该音效仅播放约8秒就停止。请问需要调整哪些设置才能让通知完整播放这段29秒的音效?是否需要修改AppDelegate.swift或info.plist文件?

当前使用的Flutter AwesomeNotifications代码

NotificationChannel(
  channelKey: "alarm_Category",
  channelName: "alarm_Category",
  channelDescription: "Notification tests as alerts",
  defaultColor: Colors.blue,
  ledColor: Colors.white,
  playSound: true,
  onlyAlertOnce: false,
  importance: NotificationImportance.Max,
  soundSource: 'resource://raw/notification_sound',
  criticalAlerts: true,
  enableVibration: true,
  enableLights: true,
  ledOnMs: 1000,
  ledOffMs: 500,
  locked: true,
),


await AwesomeNotifications().createNotification(
        content: NotificationContent(
          category: NotificationCategory.Alarm,
          fullScreenIntent: true,
          backgroundColor: Colors.black,
          id: subId,
          channelKey: "alarm_Category",
          title: DateFormat('yyyy.MM.dd HH:mm').format(currentNotificationTime),
          body: 'Alarm Memo: ${schedule.enteredText}',
          payload: {
            'subId': subId.toString(),
            'enteredText': schedule.enteredText,
            'categoryIdentifier': "alarm_Category",
            'identifier': "alarm_Category",
            'channelKey': "alarm_Category",
            'category': "alarm_Category",
          },
          displayOnForeground: true,
          displayOnBackground: true,
          autoDismissible: false,
          locked: true,
          icon: 'resource://drawable/ic_launcher',
          customSound: 'resource://raw/notification_sound',
          showWhen: true,
          wakeUpScreen: true,
          criticalAlert: true,
        ),
        schedule: NotificationCalendar(
          repeats: true,
          preciseAlarm: true,
          year: currentNotificationTime.year,
          month: currentNotificationTime.month,
          day: currentNotificationTime.day,
          hour: currentNotificationTime.hour,
          minute: currentNotificationTime.minute,
          second: currentNotificationTime.second,
        ),
      );

解决方案

要让29秒的自定义音效完整播放,需要从Flutter配置和iOS端设置两方面调整,Android端基本无需额外修改:

Flutter端调整

  1. 确认核心配置有效性:
    • 你已经设置onlyAlertOnce: false,这个要保持,避免音效触发一次就中断。
    • 保留importance: NotificationImportance.Max和criticalAlerts: true,这两个配置能让通知获得最高优先级,突破系统默认的播放时长限制。
    • 确保fullScreenIntent: true,全屏通知的音效播放不受普通通知的时长约束,是长音效完整播放的必要条件。
    • 检查soundSource(频道级)和customSound(通知级)的路径是否正确,音效文件格式要匹配平台要求:Android用MP3,iOS用CAF或WAV。

iOS端必须修改的设置

1. 编辑Info.plist

添加以下权限配置,否则系统会限制后台音效播放:

<!-- 关键警报权限说明 -->
<key>NSCriticalAlertUsageDescription</key>
<string>需要播放完整的闹钟音效,确保用户能及时收到提醒</string>

<!-- 后台模式权限 -->
<key>UIBackgroundModes</key>
<array>
  <string>audio</string>
  <string>remote-notification</string>
</array>

2. 修改AppDelegate.swift

配置音频会话,允许App在后台播放音频,确保音效能完整播放:

import UIKit
import Flutter
import AwesomeNotifications
import AVFoundation // 导入音频框架

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    // 初始化AwesomeNotifications
    AwesomeNotifications().initialize(
      [
        NotificationChannel(
          channelKey: "alarm_Category",
          channelName: "alarm_Category",
          channelDescription: "Notification tests as alerts",
          defaultColor: .blue,
          ledColor: .white,
          playSound: true,
          onlyAlertOnce: false,
          importance: .max,
          soundSource: "resource://raw/notification_sound",
          criticalAlerts: true,
          enableVibration: true,
          enableLights: true,
          ledOnMs: 1000,
          ledOffMs: 500,
          locked: true
        )
      ]
    )
    
    // 配置音频会话,允许后台播放
    do {
      let audioSession = AVAudioSession.sharedInstance()
      try audioSession.setCategory(.playback, mode: .default, options: [.mixWithOthers, .allowAirPlay])
      try audioSession.setActive(true)
    } catch let error {
      print("音频会话配置失败: \(error.localizedDescription)")
    }
    
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

Android端注意事项

确保音效文件放在android/app/src/main/res/raw目录下,文件名不要有空格或特殊字符,你的现有配置已经满足Android端的权限要求,无需额外修改。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 11:02:03