简体   繁体   中英

Flutter local notifications Icon not showing

I am facing a problem that is very common, but no solution I saw helped me. So, I'm trying to show local notifications.

Everything works except the icon. Following the package's documentation, I added an icon to the drawable, but it doesn't work.

Here is the structure of my folder and the image I want to show

文件夹结构

Here is my code to initialization

初始化代码

I tried many things. I created icons with transparency (using https://romannurik.github.io/AndroidAssetStudio/ ) and added to the drawable, added the PNG as in the first image.

It only works when I copy the standard icon to the drawable, which is the Flutter logo.

Documentation of this package is not so clear about it. You need to change name app_icon to the icon file from your res/drawable folder to have an effect of your custom icon. In your case one example can be ic_launcher.png . Specific line in your code for a change is:

const AndroidInitializationSettings initializationSettingsAndroid =
    AndroidInitializationSettings('app_icon');

Note: If your notification icon gets grey then follow this link for solution: https://stackoverflow.com/a/63746521/15236786

There a few thing I had to know to solve this:

Initialization

flutter_local_notification needs to be "initialized". Here's a function I use to initialize in my main function:

void main() async {
  // Need to "ensureInitialized" before initializing `flutter_local_notifications`
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp(await initializeFlutterLocalNotifications()));
}

Future<FlutterLocalNotificationsPlugin>
    initializeFlutterLocalNotifications() async {
  final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
// initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
  // 'mipmap/ic_launcher' taken from https://github.com/MaikuB/flutter_local_notifications/issues/32#issuecomment-389542800
  const AndroidInitializationSettings initializationSettingsAndroid =
      AndroidInitializationSettings('mipmap/ic_launcher');
  final IOSInitializationSettings initializationSettingsIOS =
      IOSInitializationSettings();
  final MacOSInitializationSettings initializationSettingsMacOS =
      MacOSInitializationSettings();
  final InitializationSettings initializationSettings = InitializationSettings(
      android: initializationSettingsAndroid,
      iOS: initializationSettingsIOS,
      macOS: initializationSettingsMacOS);
  await flutterLocalNotificationsPlugin.initialize(initializationSettings);
  return flutterLocalNotificationsPlugin;
}

Default Icon path:

For AndroidInitializationSettings 's defaultIcon parameter, use mipmap/ic_launcher . Not app_icon, ic_launcher.png, etc. I discovered this from Github: Icon not working on Android

const AndroidInitializationSettings initializationSettingsAndroid =
      AndroidInitializationSettings('mipmap/ic_launcher');
 NotificationDetails(
        android: AndroidNotificationDetails(
          channel.id,
          channel.name,
          channelDescription: channel.description,
          icon: '@mipmap/ic_notification_no_bg',
          styleInformation: BigTextStyleInformation(''),
        ),
      ));

You need to provide a icon which does not have any background in AndroidNotificationDetails so App will pick this icon for Android version 11 for other versions it will pick default launcher Icon

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM