How To Add Android Notification Channel Id For Flutter App To Fix Notification While App Is On Background
Solution 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 projectvar 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:
Create a channel with the default id you already defined in
AndroidManifest.xml
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 undernotification
tag, underandroid
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,
},
},
Solution 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
Post a Comment for "How To Add Android Notification Channel Id For Flutter App To Fix Notification While App Is On Background"