在我的应用程序中,我实现了持久AppBar并通过导航抽屉替换上下文,如下所示。我不知道这是怎么回事。我正在同时初始化配置和本地通知,因此我不确定这是否是由于冲突导致的,因为在实现持久appBar之前它运行良好。我现在没有收到任何回调的通知。我在这里做错了什么?这是我的代码:
class Root extends StatefulWidget {
@override
_RootState createState() => _RootState();
}
class _RootState extends State<Root> {
Widget body = Home();
String title = "Home";
Widget action;
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
new FlutterLocalNotificationsPlugin();
FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
@override
void initState() {
localNoficationInitializer();
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
showNotification(
message['notification']['title'], message['notification']['body']);
print("onMessage: $message");
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
},
);
super.initState();
}
void localNoficationInitializer() async {
await flutterLocalNotificationsPlugin.initialize(
InitializationSettings(
AndroidInitializationSettings('@mipmap/ic_launcher'),
IOSInitializationSettings()),
onSelectNotification: onSelectNotification);
}
void showNotification(String title, String body) async {
await _notification(title, body);
}
Future<void> _notification(String title, String body) async {
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'onMessage', 'Notification', 'Local Notification',
importance: Importance.Max,
priority: Priority.High,
sound: 'sound',
ticker: 'test');
var iOSChannelSpecifics = IOSNotificationDetails();
var platformChannelSpecifics = NotificationDetails(
androidPlatformChannelSpecifics, iOSChannelSpecifics);
await flutterLocalNotificationsPlugin
.show(0, title, body, platformChannelSpecifics, payload: 'test');
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title??"Home"),
),
drawer: Drawer(
child: Stack(
children: [
ListView(
children: [
ListTile(
dense: true,
title: Text('Home',
style: Theme.of(context).textTheme.subhead),
leading: Icon(Icons.home, color: iconColor),
onTap: () {
Navigator.of(context).pop();
setState(() {
body = Home();
title = "Home";
});
},
),
ListTile(
dense: true,
title: Text('Downloads',
style: Theme.of(context).textTheme.subhead),
leading: Icon(Icons.archive, color: iconColor),
onTap: () {
Navigator.pop(context);
setState(() {
body = Downloads();
title = "Downloads";
});
}),
ListTile(
dense: true,
title: Text('LogOut',
style: Theme.of(context).textTheme.subhead),
leading: Icon(
AntDesign.logout,
color: iconColor,
),
onTap: () {
showDialog(
context: context,
child: new AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(6)),
title: new Text('Logout'),
content: new Text(
'You are going to be logged out of this application. Are you sure?'),
actions: <Widget>[
new FlatButton(
onPressed: () => Navigator.pop(context),
child: new Text('No'),
),
new FlatButton(
onPressed: () => logout(context),
child: new Text('Yes'),
),
],
),
);
}),
],
),
],
),
),
body: body ?? Home());
}
Future onSelectNotification(String payload) async {
showDialog(
context: context,
builder: (_) {
return new AlertDialog(
title: Text("PayLoad"),
content: Text("Payload : $payload"),
);
},
);
}
}