Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

service worker notificationclick event doesn't focus or open my website in tab

I use FCM push notification in my website and I want user can come into my website when He or She clicks on the push notification. Please notes that The user may be in other app or in other browser tab and I want when the user gets fcm notification the user will be able to come into my website by a click on the notification in the Firefox browser. For this reason, I used notificationclick event which Is available in the service worker. The code that I used is this:

self.addEventListener('notificationclick', event => {
  console.log('On notification click: ', event.notification.tag);
  event.notification.close();

  // This looks to see if the current is already open and
  // focuses if it is
  event.waitUntil(
    clients
      .matchAll({
        type: 'window'
      })
      .then(clientList => {
        for (let i = 0; i < clientList.length; i++) {
          const client = clientList[i];
          if (client.url === '/' && 'focus' in client) return client.focus();
        }
        if (clients.openWindow) return clients.openWindow('/');
      })
  );
});

It doesn't openwindow or focuses on my website tab. To debug the code I printed the clientList variable and It is an array with zero length.

The error that I get in the browser is this

On notification click: 
InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable firebase-messaging-sw.js:92

The above error refers to this line of code:

event.waitUntil(

OS: Mac firefox:63.0.3 reactjs:16.2.0

like image 603
Hussein Ojaghi Avatar asked Dec 03 '18 13:12

Hussein Ojaghi


2 Answers

Move your notificationclick handler BEFORE your line of code with messaging = firebase.messaging();. The FCM JS SDK installs its own global notificationclick handler and its e.waitUntil() call manages to (somehow) break the Firefox event object. If you install your global handler first, then it gets called first and so it will actually work. However, it will probably break the FCM handler in some obscure fashion.

This is, IMO, a bug in Firefox itself with regards to Service Workers rather than FCM, but FCM certainly contributes to the problem.

Relevant:

https://github.com/firebase/quickstart-js/issues/282 (same FCM bug)

https://bugzilla.mozilla.org/show_bug.cgi?id=1468935 (same FCM + Firefox bug, bad fix)

https://bugzilla.mozilla.org/show_bug.cgi?id=1313096 (the Firefox dev team doesn't have a way to test the callback in its Service Worker test suite, which makes this part of Firefox ripe for bugs)

https://bugzilla.mozilla.org/show_bug.cgi?id=1489800 (same browser bug, different product)

like image 61
Coolio Joe Avatar answered Oct 18 '22 17:10

Coolio Joe


If the goal is to open a specific URL when the notification is clicked, there may be an easier way. You can sent a "click_action" field in the FCM notification payload.

Your payload would look something like this:

  "notification" : {
    "body" : "Sample body",
    "title" : "Sample title",
    "click_action": "http://google.com"
  }

And when the notification is clicked, it will open google.com in this example.

like image 36
Neel Rao Avatar answered Oct 18 '22 19:10

Neel Rao