Execute function when receiving notification while outside the app

I have a function that saves the data it receives in Indexedb, I would like to be able to run that function every time my app receives a notification, even when it is closed. Is it possible to perform a function when a notification is received and the app is closed?