0

I want to store the title and body of a oneSignal notification. How do i do it? i tried storing those values in UserDefaults, but when the app is terminated, total data is lost. How do I store and display notifications?

Rohit Dulam
  • 105
  • 1
  • 7

1 Answers1

1

To store data persistently on the device, you have several options, e.g. Core Data, .plist files, or like you said UserDefaults. If your app does not need a database / persistence for other things anyway, but only for storing small amounts of data, like a few notifications, then using UserDefaults is probably a good choice, because it's very simple to use. Just keep in mind that UserDefaults are intended for small amounts of data only. Storing too much here will have a bad impact on performance, especially launch time.

My guess for the bug you're describing (data is stored in UserDefaults, but lost if the app gets terminated) is, that you do not call UserDefaults.standard.synchronize() after setting the data, or at least in your app delegate's applicationWillResignActive: method. Because only this call makes sure the data is actually written to disk.

If I'm guessing wrong: please provide the code you tried.

Community
  • 1
  • 1
Goodsquirrel
  • 1,476
  • 1
  • 15
  • 29