Use local storage to remember that the user doesn't care about loosing messages. They can always disable battery optimization in the app settings if they change their mind.