question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Web Push: Error when sending notification to unsubscribed browser

See original GitHub issue

Got Web Push up and running pretty well now, but there seems to be one major flaw in my implementation: If someone unsubscribes from notifications, I get a 410 error and my production app shows an Internal Server Error if I try to send them a notification.

To reproduce:

  • Register a browser for web push
  • Unregister the service worker on the browser
  • Trigger a Web Push notification to a set of devices which includes the browser which has unsubscribed

Result:

Internal Server Error: (URI)

WebPushError at (URI)
Push failed: 410 Gone
Response body:push subscription has unsubscribed or expired.

What’s the best way to avoid this error? I’m planning to put my notification sending in a Celery task to make the site more responsive, which should hide the error from the user, but I’d like to avoid this error. Does it require a fix in django-push-notifications, or is there something I should be doing app-side?

(Perhaps @goinnn encountered this? Sorry for calling on you specifically!)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jamaalscarlettcommented, Feb 19, 2020

I agree, push_messages could return something like: { 'devices': device_count, 'success': success_count, 'error': error_count, 'errors': [ { 'device': device_id, 'error': { 'code': error_code, 'msg': error_msg } } ] }

1reaction
nlittlejohnscommented, Feb 19, 2020

This code is a pretty robust solution I think:

    for device in wp_devices:
        try:
            device.send_message(message=data)
        except WebPushError as e:
            result = re.search('{(.*)}', e.args[0])
            error = json.loads(result.group(0))
            print(error)
            if error['code'] in [404, 410]:
                # remove device from db
                device.delete()

It does the following:

  1. Iterates through a queryset of Web Push devices
  2. Tries to send a notification to each device (individually)
  3. If there’s an error for that device, it checks to see if the error code suggests that the device should not be tried again (see https://autopush.readthedocs.io/en/latest/http.html#error-codes)
  4. If the error code is fatal (404 or 410), it removes the device from the DB so it’s never tried again.

This works well from my app, but perhaps it should go into django-push-notifications somewhere?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Web Push Troubleshooting - OneSignal Documentation
403 & 404 Service Worker Errors ... After you try subscribing to your site, if you see a 403 or 404 error in...
Read more >
410 push subscription has unsubscribed or expired
Our web push notification was just working fine but recently we noticed that the end points are expiring in less than one day...
Read more >
How to Unsubscribe from Push Notifications - SendPulse
Method 1. By Managing Notifications in Your Browser. Go to the browser menu and select "Settings." In the "Privacy and security" section ...
Read more >
A Website is Sending Me Push Notifications. How do I ...
To unsubscribe, follow the instructions in this article corresponding to your browser (Chrome, Firefox, Opera, Safari) and platform (Android).
Read more >
How to Unsubscribe from Push Notifications on Chrome
Method #2: Unsubscribe from the Website Sending You Notifications ... And you should be unsubscribed from any future web push notifications by ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found