Flutter local notifications Icon not showing

3,472

Solution 1

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

Solution 2

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');
Share:
3,472
GeekSilva
Author by

GeekSilva

Updated on December 19, 2022

Comments

  • GeekSilva
    GeekSilva over 1 year

    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

    folder structure

    Here is my code to initialization

    initialization code

    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.