Reputation: 571
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.
Upvotes: 9
Views: 10239
Reputation: 28468
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;
}
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');
Upvotes: 5
Reputation: 2009
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
Upvotes: 2