三星Galaxy S21 Ultra(Android13)Flutter应用通知权限与显示问题求助
三星Galaxy S21 Ultra(Android 13)Flutter应用通知故障排查
问题概述
基于教程开发的Flutter应用运行正常,但无法显示通知;在应用信息页面无法授予通知权限,系统禁止修改该设置。已确认AndroidManifest.xml添加了所需权限,核心代码如下:
Flutter核心代码
import 'package:awesome_notifications/awesome_notifications.dart'; import 'package:flutter/material.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); AwesomeNotifications().initialize( 'resource://drawable/notification_icon', [ // notification icon NotificationChannel( channelGroupKey: 'basic_test', channelKey: 'basic', channelName: 'Basic notifications', channelDescription: 'Notification channel for basic tests', channelShowBadge: true, importance: NotificationImportance.High, enableVibration: true, ), NotificationChannel( channelGroupKey: 'image_test', channelKey: 'image', channelName: 'image notifications', channelDescription: 'Notification channel for image tests', defaultColor: Colors.redAccent, ledColor: Colors.white, channelShowBadge: true, importance: NotificationImportance.High ) //add more notification type with different configuration ] ); //tap listiner on notification AwesomeNotifications().actionStream.listen((ReceivedNotification receivedNotification){ print(receivedNotification.payload!['name']); //output from first notification: FlutterCampus }); runApp(MyApp()); } class MyApp extends StatelessWidget{ @override Widget build(BuildContext context) { return MaterialApp( home: Home() ); } } class Home extends StatefulWidget { @override State<Home> createState() => _HomeState(); } class _HomeState extends State<Home> { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("Local Notification in Flutter"), backgroundColor: Colors.deepOrangeAccent, ), body: Container( alignment: Alignment.topCenter, padding: EdgeInsets.all(20), child: Column( children: [ ElevatedButton( onPressed: () async { bool isallowed = await AwesomeNotifications().isNotificationAllowed(); if (!isallowed) { //no permission of local notification AwesomeNotifications().requestPermissionToSendNotifications(); }else{ //show notification AwesomeNotifications().createNotification( content: NotificationContent( //simgple notification id: 123, channelKey: 'basic', //set configuration wuth key "basic" title: 'Welcome to FlutterCampus.com', body: 'This simple notification is from Flutter App', payload: {"name":"FlutterCampus"} ) ); } }, child: Text("Show Notification") ), ElevatedButton( onPressed: () async { bool isallowed = await AwesomeNotifications().isNotificationAllowed(); if (!isallowed) { //no permission of local notification AwesomeNotifications().requestPermissionToSendNotifications(); }else{ //show notification AwesomeNotifications().createNotification( content: NotificationContent( //with asset image id: 1234, channelKey: 'image', title: 'Simple Notification With Image', body: 'This simple notification is from Flutter App', bigPicture: 'asset://assets/images/elephant.jpg', notificationLayout: NotificationLayout.BigPicture, fullScreenIntent: true, //it will display over app locked: true, payload: {"id":"1234"} ) ); } }, child: Text("Show Notification With Asset Image") ), ElevatedButton( onPressed: () async { bool isallowed = await AwesomeNotifications().isNotificationAllowed(); if (!isallowed) { //no permission of local notification AwesomeNotifications().requestPermissionToSendNotifications(); }else{ //show notification AwesomeNotifications().createNotification( content: NotificationContent( //with image from URL id: 12345, channelKey: 'image', title: 'Simple Notification with Network Image', body: 'This simple notification is from Flutter App', bigPicture: 'https://www.fluttercampus.com/img/logo_small.webp', notificationLayout: NotificationLayout.BigPicture, payload: {"id":"12345"} ) ); } }, child: Text("Show Notification With Image From URL") ), ElevatedButton( onPressed: (){ AwesomeNotifications().dismiss(123); }, child: Text("Cancel Notification") ), ElevatedButton( onPressed: (){ AwesomeNotifications().dismissAllNotifications(); }, child: Text("Cancel All Notifications") ), ], ), ), ); } }
AndroidManifest.xml配置
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/> <application android:label="untitled" android:name="${applicationName}" android:icon="@mipmap/ic_launcher"> <activity android:name=".MainActivity" android:exported="true" android:launchMode="singleTop" android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize"> <!-- Specifies an Android theme to apply to this Activity as soon as the Android process has started. This theme is visible to the user while the Flutter UI initializes. After that, this theme continues to determine the Window background behind the Flutter UI. --> <meta-data android:name="io.flutter.embedding.android.NormalTheme" android:resource="@style/NormalTheme" /> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <!-- Don't delete the meta-data below. This is used by the Flutter tool to generate GeneratedPluginRegistrant.java --> <meta-data android:name="flutterEmbedding" android:value="2" /> <receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver" android:exported="false"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> </application> </manifest>
针对性解决方案
1. 移除冲突的接收器配置
你的AndroidManifest.xml中包含了flutter_local_notifications插件的启动接收器,但实际使用的是awesome_notifications,两者会导致冲突。删除以下代码块:
<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver" android:exported="false"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver>
2. 修复权限请求逻辑
Android 13要求POST_NOTIFICATIONS必须通过运行时请求触发,且需确保权限请求在通知渠道初始化之后执行。修改权限请求代码,添加弹窗回调处理:
if (!isallowed) { // 请求权限并处理结果 final permissionStatus = await AwesomeNotifications().requestPermissionToSendNotifications(); if (permissionStatus.isDenied) { // 引导用户到应用设置页面手动开启 await AwesomeNotifications().openNotificationSettings(); } }
3. 验证通知图标合规性
三星设备对通知图标有严格要求:
- 图标必须放置在
android/app/src/main/res/drawable目录 - 图标需为透明背景、单色矢量图,避免使用彩色或复杂图案
- 文件名需与初始化代码中的
notification_icon完全一致
4. 清理应用缓存并重新安装
- 卸载应用,进入手机设置的
应用管理,找到该应用残留信息并清除所有数据 - 执行
flutter clean命令清理项目缓存 - 重新编译安装应用
5. 检查三星系统特殊限制
- 进入三星
智能管理器,检查应用是否被加入省电黑名单,导致通知被拦截 - 查看
设置 -> 通知 -> 高级设置,确认应用未被设置为禁止通知(部分三星机型会默认限制新应用通知)
内容的提问来源于stack exchange,提问作者Julius Krams
相关产品推荐
相关产品推荐

