Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

92
Views
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?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!