我试图在Flutter制作的应用中处理FCM通知,以使用flutter_local_notifications插件显示通知以自定义通知, 但由于某些原因,通知未随插件一起显示,但默认情况下为通知。 为了验证我显示的通知是使用flutter_local_notifications插件创建的通知,我在通知标题之前添加了“ X”。 但是通知显示为好像您没有通过flutter_local_notifications处理通知一样,标题的开头没有“ X”。
这是我的代码:
我在这里配置FCM
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print('on message $message');
},
onBackgroundMessage: myBackgroundMessageHandler,
onResume: (Map<String, dynamic> message) async {
print('on resume $message');
},
onLaunch: (Map<String, dynamic> message) async {
print('on launch $message');
},
);
我的处理程序:
Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) {
if (message.containsKey('data')) {
// Handle data message
final dynamic data = message['data'];
}
if (message.containsKey('notification')) {
final dynamic notification = message['notification'];
_showNotification(notification['title'], notification['body']);
}
}
以及“ showNotification”方法:
_showNotification(String descripcion , String titulo) async {
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
var android = new AndroidInitializationSettings('@mipmap/ic_launcher');
var iOS = new IOSInitializationSettings();
var initSetttings = new InitializationSettings(android, iOS);
flutterLocalNotificationsPlugin.initialize(initSetttings,
onSelectNotification: onSelectNotification);
String groupKey = 'com.witway.proyectox';
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
'ProyectoX',
'ProyectoX',
'ProyectoX',
importance: Importance.Max,
priority: Priority.High,
groupKey: groupKey,
// setAsGroupSummary: true
);
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
NotificationDetails platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
flutterLocalNotificationsPlugin.show(0,"X"+ titulo, descripcion, platformChannelSpecifics);
}
PS:我按照本教程中的所有步骤处理了Firebase消息传递文档中的通知