【发布时间】:2021-07-07 19:58:41
【问题描述】:
我有一个连接到 firebase 的 Flutter 应用,我正在使用它来接收来自 Firebase Cloud Messaging 的推送通知,使用此代码。每当我调用变量widget.title、widget.body 时,它们都是空的,但是当方法收到通知时,它们具有来自 FCM 的值。我该怎么办?
class PushList extends StatefulWidget {
Map<dynamic, dynamic> notific;
String title, body;
Future<dynamic> fcmMessageReceiver() async {
FirebaseMessaging.instance.getInitialMessage().then((value) {
if (value != null) {}
});
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
if (message.notification != null) {
notificacao = {
'title': message.notification.title,
'body': message.notification.body
};
title = message.notification.title;
body = message.notification.body;
print('MENSAGEM: $notific');
}
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {});
}
PushList() {
}
@override
_PushListState createState() => _PushListState();
}
class _PushListState extends State<PushList> {
@override
Widget build(BuildContext context) {
return Scaffold(
extendBody: true,
backgroundColor: Colors.white,
appBar: null,
body: ListView(
children: [
widget.notific != null
? Card(
margin: EdgeInsets.all(10),
elevation: 4,
child: ListTile(
title: Text(
widget.title,
),
subtitle: Text(
widget.body,
),
),
)
: Container(
child: Text("U don't have new notifcs."),
),
],
),
);
}
}
【问题讨论】:
标签: firebase flutter dart firebase-cloud-messaging