我正在使用FCM。我正在尝试在我的应用程序处于前台时显示通知。 我已成功收到通知,但应用程序在收到通知后崩溃。
void initState() {
//TODO: Local notifications configuration.
var initializationSettingsAndroid = AndroidInitializationSettings('notification_icon');
var initializationSettingsIOS = IOSInitializationSettings();
var initializationSettings = InitializationSettings(initializationSettingsAndroid,initializationSettingsIOS);
Future selectNotification(String payload) async {
print('Showing alert');
showDialog(context: context,builder: (_)=>AlertDialog(title: Text("Title"),));
}
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
flutterLocalNotificationsPlugin.initialize(initializationSettings,onSelectNotification:selectNotification);
Future showNotifications() async {
print('show');
var androidPlatformSpecifics = AndroidNotificationDetails('123','Channel Name','Description about the channel',importance: Importance.Max,priority: Priority.Default);
var iOSPlatformSpecifics = IOSNotificationDetails();
var platformChannelSpecifics = NotificationDetails(androidPlatformSpecifics,iOSPlatformSpecifics);
await flutterLocalNotificationsPlugin.show(0, 'New Post', 'Notification', platformChannelSpecifics);
print('show');
}
//TODO:Configuration of firebase
_firebaseMessaging.configure(
onMessage: (Map<String,dynamic> message){
debugPrint('on');
showNotifications();
print(message);
},
onResume: (Map<String,dynamic> message){
debugPrint('resume');
print(message);
},
onLaunch: (Map<String,dynamic> message){
debugPrint('Launch');
print(message);
},
);
_firebaseMessaging.getToken().then((token){
print(token);
});
推送通知后,我不断收到“系统用户界面已停止”