Accessing Dexie(Indexed db) inside FCM onBackgroundMessageHandler

I am using FCM for push notifications in my web app and everything is working fine foreground and background except the fact that when my browser is closed, I am receiving FCM notifications but unable to process them. I am storing the data from FCM in an Indexed db wrapped by Dexie and my db is not updated with the FCM data values when the browser is itself closed.
However, if my app’s tab is closed but the browser is open, things work fine,
I am using the below code to do my task in my firebase service worker :

messaging.onBackgroundMessage(async function (payload) {
  console.log('Received background message ', payload);

  const notificationTitle = payload.data.title;
  const notificationOptions = {
    body: payload.data.body,
    tag: "notification-1",
    
  };

  if(!db){
    await initDb()
  }

  if (payload.data.type == 'DATA') {
   await addDataToDb(payload.data.item)
  }

  self.registration.showNotification(notificationTitle,
    notificationOptions);
});

async function initDb() {
  console.log("Initializing Dexie")
  db = await new Dexie('AppDB').open()
  Dexie.getDatabaseNames().then((names) => {
    console.log('Db names ' + names)
  });
}

DB updating code is all fine as it works as expected when the browser is still running.
Can someone please help me here?