Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"/>
like image 848
Mahmood Bkh Avatar asked Sep 04 '19 07:09

Mahmood Bkh


1 Answers

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,
        },
    },
like image 63
Ali Hussein Al-Issa Avatar answered Sep 20 '22 13:09

Ali Hussein Al-Issa