pushManager.subscribe() randomly fails with AbortError

Viewed 269

This is happening on Chrome only (application doesn't support any other browsers for now) and on recent versions.

I can't reproduce this issue, but it regularly shows up in our monitoring dashboard for random users.

Error message: "AbortError: Registration failed - push service error"

The following code block runs on page load:

const currentNotificationsPermission = await navigator.permissions.query({ name: 'notifications' });
currentNotificationsPermission.onchange = function onchange() {
  // Update the internal state, this can happen after user interaction (clicking a "Allow notifications" button)
};

This runs on pageload (when the internal state is granted), or whenever the internal state changes to granted after a user gesture:

navigator.serviceWorker.register('/serviceworker.js', { scope: '/myScope' });
navigator.serviceWorker.ready.then((registration) => {
  const subscribeOptions = {
    userVisibleOnly: true,
    applicationServerKey: 'myKey',
  };

  return registration.pushManager.subscribe(subscribeOptions);
}).then((pushSubscription) => {
  // Send pushSubscription to server, doesn't run in case of AbortError
}).catch((err) => { 
  console.log(err); // AbortError, randomly
});
2 Answers

I guess you can't check chrome://gcm-internals/ if you can't reproduce the problem.

One possibility is that some of your users may have Chrome settings or add-ons that prevent the use of GCM, but the error message is not quite in line with that.

My guess is that this is a case of TOO_MANY_REGISTRATIONS where a device has too many individual registrations with FCM. It might be that some of your users have too many client apps, or one of their client apps may be registering itself over and over again, resulting in many registrations.

According to the Push API spec, this error can happen in a few ways for the subscribe method: https://w3c.github.io/push-api/#dom-pushmanager

  1. If there was an error retrieving the push subscription associated with the Service Worker (returns AbortError).
  2. If there was an error setting subscription as the result of running the create a push subscription steps given options (returns AbortError).

These are both internal processes to the browser as they involve interacting with the internal Push Service.

As noted here, the push service is not something available to developers: https://developers.google.com/web/fundamentals/push-notifications/how-push-works#who_and_what_is_the_push_service.

There have been some historical errors due to this service for Chrome. I can see some posts for this as far back as Chrome 59.

For now you might want to protect this line of code with a try/catch and log details where you can access them registration.pushManager.subscribe(subscribeOptions);

It might simply need a retry when this fails or it's somehow running into issues due to an older version of Chrome.

Related