【发布时间】:2021-05-18 05:47:49
【问题描述】:
到目前为止,我能够从 Firebase Cloud Messaging 检索单个推送通知并将其显示在屏幕上。但我想将所有推送通知保存为列表视图。因此,每次收到新的推送通知时,都会重新构建列表视图,并在顶部使用最新的推送通知进行更新。理想情况下,用户应该能够向后滚动并查看他们曾经收到的所有推送通知。
这可能吗?这是显示单个通知的代码;
import 'package:flutter/material.dart';
import 'package:fcm_config/fcm_config.dart';
Future<void> _firebaseMessagingBackgroundHandler(
RemoteMessage _notification) async {
String title = _notification.data["title_key"];
String body = _notification.data["body_key"];
FCMConfig.displayNotification(title: title, body: body);
}
void main() async { FCMConfig.init(onBackgroundMessage: _firebaseMessagingBackgroundHandler).then((value) {
FCMConfig.subscribeToTopic("test_fcm_topic");
});
runApp(MyHomePage());
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage>
with FCMNotificationMixin, FCMNotificationClickMixin {
RemoteMessage _notification;
final String serverToken ='myservertoken';
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ListTile(
title: Text("title"),
subtitle: Text(_notification?.notification?.title ?? ""),
),
ListTile(
title: Text("Body"),
subtitle: Text(
_notification?.notification?.body ?? "No notification"),
),
if (_notification != null)
ListTile(
title: Text("data"),
subtitle: Text(_notification?.data?.toString() ?? ""),
)
],
),
),
),
);
}
@override
void onNotify(RemoteMessage notification) {
_firebaseMessagingBackgroundHandler(notification);
setState(() {
_notification = notification;
});
}
@override
void onClick(RemoteMessage notification) {
setState(() {
_notification = notification;
});
print(
"Notification clicked with title: ${notification.notification.title} && body: ${notification.notification.body}");
}
}
【问题讨论】:
标签: flutter listview dart firebase-cloud-messaging