0

只要应用程序在前台运行,一切都会完美运行。

这部分用于处理后台firebase消息并准备通知对象并保存收到的通知。这部分将 json 编码的通知消息写入共享首选项

在后台或应用程序终止

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))}");
   });
}

日志

Notification Message Shared //Some json encoded object
Notification Message //Some json encoded object
Notification Message //Some json encoded object

在前台

@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();
 }

日志

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)

自定义共享首选项

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);
   }
}

现在的问题是我得到了不同的 SharedPref 实例,这就是为什么我得到空结果,或者我做错了什么。在前台一切都很完美。

4

0 回答 0