Angular firebase push notifications link not working

Viewed 25

I have an Angular app with a firebase Service worker to receive cloud messages from Firebase Cloud Messaging. My problem is when the notification is in background and I click it, the notification send me to my app at the specific route just when the app is closed runnnig in background, when the app is not focused it just open the app and no redirect to the route I passing.

I pass it to the fcmOptions (Code below, I´m using firebase admin to send notifications through nodejs server). I read a lot of documentation of how to redirect in web push notifications and migrate to the firebase admin after using the fcm API but it still working as I mentioned also in production.

Does anyone know how to solve this or could help me to redirect manually when the users click the notification using the notificationclick event?.

 messaging()
    .send({
      notification: {
        title,
        body: message,
      },
      webpush: {
        notification: {
          icon:
            icon ??
            'https://storage.googleapis.com/mejora-continua-bucket/logoMejora.png',
        },
        fcmOptions: {
          // link: 'https://mejora-continua-dot-mx-vianney-001.uc.r.appspot.com/admin-usuarios',
          link: '/admin-usuarios',
        },
      },
      token: to,
    })
    .then((result) => {
      console.log(result);
    })
    .catch((err) => {
      console.log(err);
    });

I receive the notification as this:

Background notification

And my firebase sw is like this:

importScripts(
  "https://www.gstatic.com/firebasejs/9.9.4/firebase-app-compat.js"
);
importScripts(
  "https://www.gstatic.com/firebasejs/9.9.4/firebase-messaging-compat.js"
);

firebase.initializeApp({
  apiKey: "myapikey",
  authDomain: "myapp.firebaseapp.com",
  databaseURL: "https://myapp.firebaseio.com",
  projectId: "myprojectid",
  storageBucket: "mystorage",
  messagingSenderId: "messageid",
  appId: "appid",
});

const messaging = firebase.messaging();

messaging.onBackgroundMessage((payload) => {
  console.log("Received background message ", payload);
});

0 Answers
Related