How to add android notification channel id for flutter app to fix notification while app is on background

11,546

You should create a notification channel first, the plugin: flutter_local_notifications can create and remove notification channels.

First, initialize the plugin:

Future<void> initializeLocalNotifications() async {
  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
  FlutterLocalNotificationsPlugin();
  // app_icon needs to be a added as a drawable resource to the
  // Android head project
  var initializationSettingsAndroid = AndroidInitializationSettings('app_icon');
  var initializationSettingsIOS = IOSInitializationSettings(
      onDidReceiveLocalNotification: onDidReceiveLocalNotification);
  var initializationSettings = InitializationSettings(
      initializationSettingsAndroid, initializationSettingsIOS);
  await flutterLocalNotificationsPlugin.initialize(initializationSettings,
      onSelectNotification: selectNotification);
}

Second, create notification channel using this plugin:

Future<void> _createNotificationChannel(String id, String name,
    String description) async {
  final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
  var androidNotificationChannel = AndroidNotificationChannel(
    id,
    name,
    description,
  );
  await flutterLocalNotificationsPlugin
      .resolvePlatformSpecificImplementation<
      AndroidFlutterLocalNotificationsPlugin>()
      ?.createNotificationChannel(androidNotificationChannel);
}

Now you have two options:

  1. Create a channel with the default id you already defined in AndroidManifest.xml

  2. Choose your own channel ids and send each notification to a specific channel by embedding the channel id into your FCM notification message. To do so, add channel_id tag to your notification under notification tag, under android tag.

Here is a sample notification message from Firebase Functions with custom notification channel id:

    'notification': {
        'title': your_title,
        'body': your_body,
    },
    'android': {
        'notification': {
            'channel_id': your_channel_id,
        },
    },
Share:
11,546
Mahmood Bkh
Author by

Mahmood Bkh

Updated on July 20, 2022

Comments

  • Mahmood Bkh
    Mahmood Bkh almost 2 years

    In my flutter app the function onResume and onLunch not working on android platform, while they work fine on IOS, i receive the following message on console instead of printed strings in those functions:

    "W/FirebaseMessaging(24847): Missing Default Notification Channel metadata in AndroidManifest. Default value will be used."

    onMessage function works fine, the problem is when the app is in background

    my guess is that it has something to do with android notification channel id which should be added in android manifest

    when i add that to manifest with adding the following code to AndroidManifest the message change to : (I added a strings.xml file in values and defined "default_notification_channel_id" there.)

    "Notification Channel set in AndroidManifest.xml has not been created by the app. Default value will be used."

    <meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="@string/default_notification_channel_id"/>
    

    in my console i should receive onResume and onLunch strings which i printed , but instead i receive following messages :

    "W/FirebaseMessaging(24847): Missing Default Notification Channel metadata in AndroidManifest. Default value will be used."

    "Notification Channel set in AndroidManifest.xml has not been created by the app. Default value will be used."

    <meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="@string/default_notification_channel_id"/>
    
  • Mahmood Bkh
    Mahmood Bkh over 4 years
    if you mean the channel id in firebase console , yes i tried send notification with channel id, but still does not work
  • Ekta Padaliya
    Ekta Padaliya about 3 years
    Create a channel with the default id you already defined in AndroidManifest.xml => How to do this ?
  • Ali Hussein Al-Issa
    Ali Hussein Al-Issa about 3 years
    You define a default notification channel as shown in this answer.