safwanrahman / django-webpush

Web Push Notification Package for Django
GNU General Public License v3.0
371 stars 107 forks source link

Muliple subscriptions for same Device #135

Open jeremyknoepfel opened 10 months ago

jeremyknoepfel commented 10 months ago

When a user has subscribed for push it creates an entry in the subscription info. However if his browser gets updated to a newer version a new entry gets added.

This now has the effect, that the following user gets the same push message sent 5 times to his device when using the send_notification_to_user method.

image

safwanrahman commented 10 months ago

@jeremyknoepfel Thanks. That is really interesting! I do not know how this happens, maybe chrome changed something? Can you check on Firefox as well?

jeremyknoepfel commented 9 months ago

@safwanrahman Unfortunately i didn't have time to test it on Firefox. The problem still occurs with Chrome. Isn't it possible to make the combination of auth and browser unique to each user?

SHxKM commented 8 months ago

Isn't there a mechanism to check whether the current browser is subscribed to push events, preventing this situation altogether?

jeremyknoepfel commented 6 months ago

@safwanrahman The issue still exists. I found a fix which has to be executed periodically. Currently I'm running this script every hour with celery:

from django.db.models import Count, Min
    from webpush.models import SubscriptionInfo
    # Step 1: Annotate each WebpushSubscriptionInfo with the minimum id for each group of auth and p256dh.
    duplicates = (
        SubscriptionInfo.objects
        .values('auth', 'p256dh')
        .annotate(min_id=Min('id'), count_id=Count('id'))
        .filter(count_id__gt=1)
    )
    # Step 2: Collect the IDs of the entries with the lowest id in each group.
    min_ids_to_delete = [entry['min_id'] for entry in duplicates]
    # Step 3: Delete these entries.
    SubscriptionInfo.objects.filter(id__in=min_ids_to_delete).delete()

A long term solution for the django-webpush package would be to set the auth&p256dh as unique und update the existing entry instead of creating a new one when the client browser updates.

4c1dhydr4 commented 3 months ago

hi @safwanrahman thank you for this contribution, Are there any plans to introduce a fix to the issue soon?