Hisham Aburass
Hisham Aburass

Reputation: 625

Google Cloud Messaging (GCM) not working with firefox

I have this Service Worker that receives notification well with Chrome, but it's not receiving with firefox.

the Push listener is not fired at all in firefox (by debugging it),

PS: the service worker is successfully registered, but it's not receiving notification.

what's the problem with my code?

self.addEventListener('install', function (event) {
event.waitUntil(self.skipWaiting());
});

self.addEventListener('activate', function (event) {
console.log('Activated', event);
});

self.addEventListener('push', function (event) {
event.waitUntil(
    fetch('/path', {
        credentials: 'include',
        method: 'post',
    })
    .then(function (response) {
        return response.json()
            .then(function (data) {
                return self.registration.showNotification(data.title, {
                    body: data.body,
                    icon: '/images/image.png',
                });
            });
    })
    .catch(function (error) {
        console.error('wrong', error);
    })
);
});

Upvotes: 0

Views: 386

Answers (2)

Francisco Jordano
Francisco Jordano

Reputation: 885

You will need to share your code in the client for registering for push notifications.

With that said, once you register for push notifications, you will receive a subscription with an endpoint. In Firefox that endpoint will never be a GCM url, but a push server provided by Mozilla:

navigator.serviceWorker.ready
      .then((reg) => reg.pushManager.subscribe({ userVisibleOnly: true }))
      .then((subscription) => {
          const endpoint = subscription.endpoint;
          // endpoint will have different server values when using this code in Chrome or Firefox.

Here are key notes to be considered:

  • Are you registering for push notifications correctly in firefox?
  • Check the url for the endpoint you will need to reach for performing the push notification, is a mozilla server one?
  • Setup breakpoints just after the push listener to verify that you receive the push.

Upvotes: 1

abielita
abielita

Reputation: 13494

Based from this documentation, if you are using the Channel Messaging API to comunicate with the service worker, set up a new message channel (MessageChannel.MessageChannel()) and send port2 over to the service worker by calling Worker.postMessage() on the service worker, in order to open up the communication channel. You should also set up a listener to respond to messages sent back from the service worker.

Make sure that you followed these steps on how to set up the GCM properly. You can also check this related link: GCM Equivalent for Firefox

Hope this helps!

Upvotes: 1

Related Questions