Everything works perfectly as long as app is running foreground.
This part is for handling background firebase message and preparing notification object and saving the received notification. This part writes json encoded notification message to shared pref
In Background or App Terminated
Future<dynamic> onBackgroundMessage(Map<String, dynamic> message) async
{
//Custom Notification Object
NotificationMessage test=NotificationMessage().fromMap(message);
//Shared Pref Custom
PsSharedPreferences.instance.futureShared.then((value)
{
value.setString(Const.VALUE_HOLDER_NOTIFICATION_MESSAGE, json.encode(test.toJson()));
PsSharedPreferences.instance.replaceNotificationMessage(json.encode(test.toJson()));
print("Notification Message "+PsSharedPreferences.instance.getNotificationMessage());
print("Notification Message "+value.getString(Const.VALUE_HOLDER_NOTIFICATION_MESSAGE));
});
//Core Shared Pref
Future<SharedPreferences> futureShared = SharedPreferences.getInstance();
futureShared.then((SharedPreferences shared)
{
NotificationMessage notificationMessage=NotificationMessage().fromMap(message);
shared.setString(Const.VALUE_HOLDER_NOTIFICATION_MESSAGE,json.encode(notificationMessage.toJson()));
print("Notification Message Shared ${json.encode(shared.getString(Const.VALUE_HOLDER_NOTIFICATION_MESSAGE))}");
});
}
Logs
Notification Message Shared //Some json encoded object
Notification Message //Some json encoded object
Notification Message //Some json encoded object
In Foreground
@override
void initState()
{
print("Notification Message "+PsSharedPreferences.instance.getNotificationMessage());
print("Notification Message "+PsSharedPreferences.instance.shared.getString(
Const.VALUE_HOLDER_NOTIFICATION_MESSAGE)
);
PsSharedPreferences.instance.futureShared.then((value)
{
//Not Working with reload as well
value.reload();
print("Notification Message "+value.getString(Const.VALUE_HOLDER_NOTIFICATION_MESSAGE));
print("Notification Message "+PsSharedPreferences.instance.getNotificationMessage());
});
Future<SharedPreferences> futureShared = SharedPreferences.getInstance();
futureShared.then((SharedPreferences shared)
{
print("Notification Message Shared
${json.encode(shared.getString(Const.VALUE_HOLDER_NOTIFICATION_MESSAGE))}");
notificationMessage=NotificationMessage.fromJson(json.decode(
shared.getString(Const.VALUE_HOLDER_NOTIFICATION_MESSAGE))
);
});
super.initState();
}
Logs
Notification Message //Nothing
Notification Message //Nothing
Notification Message //Nothing
Notification Message //Nothing
Notification Message Shared //Nothing
[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: FormatException: Unexpected end of input (at character 1)
Custom Shared Pref
class PsSharedPreferences
{
PsSharedPreferences()
{
Utils.psPrint('init PsSharePerference $hashCode');
futureShared = SharedPreferences.getInstance();
futureShared.then((SharedPreferences shared)
{
this.shared = shared;
});
}
Future<SharedPreferences> futureShared;
SharedPreferences shared;
// Singleton instance
static final PsSharedPreferences _singleton = PsSharedPreferences();
// Singleton accessor
static PsSharedPreferences get instance => _singleton;
Future<dynamic> replaceNotificationMessage(String message) async
{
await shared.setString(Const.VALUE_HOLDER_NOTIFICATION_MESSAGE, message);
}
String getNotificationMessage()
{
return shared.getString(Const.VALUE_HOLDER_NOTIFICATION_MESSAGE);
}
}
Now the question is am i getting different instances of SharedPref, which is why I am getting empty result, or I have done something wrong. And In foreground everything is perfect.