safwanrahman / django-webpush

Web Push Notification Package for Django

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Muliple subscriptions for same Device

jeremyknoepfel opened this issue · comments

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

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

@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?

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

@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.