2

I am using the flutter_local_notification package for showing the notification. For example, I want to change the background color of notifications to blue, but using the color property in notification detail changes the text color of the App name only.

Code:

AndroidNotificationDetails(
      'CHANNEL_ID',
      'CHANNEL_NAME',
      channelDescription: 'CHANNEL_DESCRIPTION',
      importance: Importance.max,
      priority: Priority.high,
        icon: 'logo',
        sound: RawResourceAndroidNotificationSound('notification_sound'),
      styleInformation: DefaultStyleInformation(true, true),
      playSound: true,
        color: light_blue_color,
        largeIcon: const DrawableResourceAndroidBitmap('logo'),
        colorized: true,
    );
Sania Developer
  • 130
  • 1
  • 9

1 Answers1

3

For anyone interested in the where the answer to this question came from, please refer to the pull request in https://github.com/MaikuB/flutter_local_notifications/pull/1459.

In order to change the background color of a notification using flutter_local_notifications, you need to utilize the Android's foreground service which is not included in the FlutterLocalNotification plugins manifest.

Open android->app->main->AndroidManifest.xml and make sure you add the service and permission as follows:

<application
   <service 
     android:name="com.dexterous.flutterlocalnotifications.ForegroundService"
     android:exported="false"
     android:stopWithTask="false"/>
</application>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

With that added you can call the service to customize the background color of your notification.

Future<void> _startForegroundServiceWithBlueBackgroundNotification() async{
const AndroidNotificationDetails androidPlatformChannelSpecifics =
AndroidNotificationDetails(
  'your channel id',
  'your channel name',
  channelDescription: 'color background channel description',
  importance: Importance.max,
  priority: Priority.high,
  color: Colors.blue,
  colorized: true,
);

/// only using foreground service can color the background
await flutterLocalNotificationsPlugin
    .resolvePlatformSpecificImplementation<
    AndroidFlutterLocalNotificationsPlugin>()
    ?.startForegroundService(
    1, 'colored background text title', 'colored background text body',
    notificationDetails: androidPlatformChannelSpecifics,
    payload: 'item x');

}

thenry
  • 85
  • 1
  • 12