Flutter iOS : Cache a notification when displayed in background

199 Views Asked by At

I am able to receive notifications in Flutter with iOS and Android and am able to store notifications in cache with the shared_preferences package in foreground and background in Android. I am able to do the same in foreground with iOS but not in background.

When I receive a notification in background on iOS, I want the app to store it in cache with shared_preferences and then display it on the screen once the user launches the app.

Inside of my Info.plist I have:

<key>UIBackgroundModes</key>
    <array>
        <string>remote-notification</string>
    </array>

I also have everything handled with background and foreground notifications since I am receiving them correctly. I just need to perform certain actions while in the background state. Specifically something like this:

 String notificationText =
      "${message.notification?.title}`~`${time}`~`${topic}`~`${message.notification?.body}";
  SharedPreferences prefs = await SharedPreferences.getInstance();
  prefs.reload();
  List<String>? notifications = prefs.getStringList('notifications');

  if (notifications == null) {
    notifications = [];
  }

  notifications.add(notificationText);
  await prefs.setStringList('notifications', notifications);
  debugPrint('Added  ${notifications.last} to the list');
  debugPrint('Here is prefs ${prefs.getStringList('notifications')}');
}
0

There are 0 best solutions below