How to add android notification channel id for flu

2020-07-16 09:22发布

问题:

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"/>

回答1:

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,
        },
    },


回答2:

have you created default_notification_channel_id ?

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

You have this warning when you don't set the channel_id in the payload of the notification