Reputation: 172
I am trying to save push notification title and body using shared_preference and and trying to get that saved data in my card view or listtile meaning i wanna show the list of notifications inside my app which i saved locally using shared preference:
static List<String?> detail = [];
FirebaseMessaging.onMessage.listen((message) async{
if(message.notification!=null){
// print(message.notification!.body);
// print(message.notification!.title);
final title = message.notification?.title;
final body = message.notification?.body;
SharedPreferences prefs = await SharedPreferences.getInstance();
final String notificationData = json.encode({"title":title,"body":body});
detail.add(notificationData);
final data = prefs.setString('notificationData', detail.toString());
print(data);
print(detail);
}
LocalNotificationService.display(message);
});
FirebaseMessaging.onMessageOpenedApp.listen((message) async{
final routeFromMessage = message.data["routeKey"];
// RemoteNotification? notification = message.notification;
final title = message.notification?.title;
final body = message.notification?.body;
SharedPreferences prefs = await SharedPreferences.getInstance();
final String notificationData = json.encode({"title":title,"body":body});
detail.add(notificationData);
final data = prefs.setString('notificationData', detail.toString());
print(data);
print(detail);
Navigator.of(context).pushNamed(routeFromMessage);
});
The data is being saved but my old notification is being replace by the new once and only the new once are showing
I am trying to get my saved data like this:
Future<String?> getNotification() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.getString("notificationData");
}
I wanna get all the list of my notification inside a card view or list view:
Card(
child: FutureBuilder(
future: getNotification(),
builder: (context, notificationSnapshot) {
if (notificationSnapshot.connectionState == ConnectionState.none &&
notificationSnapshot.hasData) {
return const Text("No Notification to Show");
}
return ListTile(
title: Text("$detail"),
);
},
),
)
Upvotes: 2
Views: 1308
Reputation: 247
step 1
first get detail
list value
step 2
then append notificationData
in detail
list
so your list value can't replace with newer one. hope its helpful
Upvotes: 1