0

I use Vue js with firebase FCM to manage the notification I am trying to translate the notification according to the language selection stored in localStorage

for example:

  firebase.messaging().onBackgroundMessage((payload) => {
    
    const { title, body, icon, ...restPayload } = payload.data;

    const fr = localStorage.language == "fr";

    const translate = () => {
      if (title == "new order") {
        return {
          title: fr?"Nouvelle commande":"طلبية جديدة",
          body: fr?`La commande a le code ${body}`:`الطلبية تحمل الرمز ${body}`,
        };
      } else {
        return body;
      }
    };
    
    const options = {
      data: restPayload,
      body: translate().body,
      icon: "img/icons/android-chrome-192x192.png",
      badge: "img/icons/badge-48x48.png",
      vibrate: [400, 100, 400, 100, 400, 100, 400],
      dir: fr?"ltr":"rtl",
      lang: fr?"fr":"ar",
    };

    return self.registration.showNotification(translate().title, options);
  });

but i can't access to localStorage, and i receive this error :

firebase-messaging-sw.js:53 Uncaught (in promise) ReferenceError: localStorage is not defined

how can i do it?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Adam
  • 89
  • 1
  • 9

0 Answers0