Android14 SCHEDULE_EXACT_ALARM权限致Flutter应用崩溃求助
解决Flutter应用无闹钟代码却触发SCHEDULE_EXACT_ALARM权限警告的问题
问题根源
你项目中引入的flutter_local_notifications: ^9.8.0+1插件是触发警告的核心原因。该插件的定时通知功能依赖Android的精确闹钟API,即便你没主动编写定时通知相关代码,插件内置的广播接收器(比如ScheduledNotificationReceiver、ScheduledNotificationBootReceiver)仍会关联精确闹钟逻辑。在Android 14系统中,SCHEDULE_EXACT_ALARM权限不再默认授予,因此触发了崩溃警告。
具体解决方案
方案1:升级flutter_local_notifications到最新稳定版
新版本的插件已适配Android 14权限规则,会自动处理权限检查逻辑,或默认采用非精确闹钟(若你的场景无需精确定时)。执行以下命令完成升级:
flutter pub upgrade flutter_local_notifications
升级后,插件会在需要调用精确闹钟API前自动校验权限状态,避免无权限调用引发崩溃。
方案2:手动添加权限并处理检查逻辑(适合暂时无法升级的场景)
- 在
AndroidManifest.xml中添加权限声明:<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" /> - 借助
permission_handler插件检查并请求权限:
即便你未主动使用定时通知功能,插件内部仍可能调用相关API,建议在应用初始化阶段处理权限:import 'package:permission_handler/permission_handler.dart'; // 应用初始化时检查权限 Future<void> checkExactAlarmPermission() async { if (await Permission.scheduleExactAlarm.isDenied) { await Permission.scheduleExactAlarm.request(); } }
方案3:移除不必要的定时通知配置(若完全不需要定时功能)
如果你确认不需要任何定时通知功能,可在AndroidManifest.xml中移除插件的广播接收器声明:
<!-- 删除以下两个接收器节点 --> <receiver android:exported="false" android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver"/> <receiver android:exported="false" android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> <action android:name="android.intent.action.MY_PACKAGE_REPLACED"/> <action android:name="android.intent.action.QUICKBOOT_POWERON" /> <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/> </intent-filter> </receiver>
注意:此操作会导致插件的定时通知功能失效,仅在确认无需该功能时使用。
内容的提问来源于stack exchange,提问作者Arnab Ghosh
相关产品推荐
相关产品推荐

