您应该首先创建一个通知通道,插件:flutter_local_notifications https://pub.dev/packages/flutter_local_notifications可以创建和删除通知渠道。
首先,初始化插件:
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);
}
其次,使用此插件创建通知通道:
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);
}
现在你有两个选择:
使用您已定义的默认 ID 创建一个频道AndroidManifest.xml
选择您自己的通道 ID,并将通道 ID 嵌入到 FCM 通知消息中,将每个通知发送到特定通道。为此,请添加channel_id
标记到您的通知下notification
标签,下android
tag.
以下是来自 Firebase Functions 的示例通知消息,其中包含自定义通知通道 ID:
'notification': {
'title': your_title,
'body': your_body,
},
'android': {
'notification': {
'channel_id': your_channel_id,
},
},