jazzband / django-push-notifications

Send push notifications to mobile devices through GCM or APNS in Django.
MIT License
2.26k stars 613 forks source link

Sending message in bulk raising GCM error when it should not #683

Open sauravkhakurel opened 1 year ago

sauravkhakurel commented 1 year ago

The docs of the package state that GCM Error is not raised when using bulk notifications. But the error is raised when I send bulk notifications using the following code from which I am not able to calculate the number of devices that the notification was delivered to.

devices = GCMDevice.objects.filter(user__in=users)
result = devices.send_message(
            notification.notification_text, tag=uuid4().hex, extra=extra
        )

Is there any possible fix for this? Thanks

jamaalscarlett commented 1 year ago

@sauravkhakurel can you please post the error you are seeing?

jamaalscarlett commented 1 year ago

@sauravkhakurel bump

sauravkhakurel commented 1 year ago

Here is the error log from the Huey container. I have attached the full log as well.

INFO 2023-09-30 14:45:09,513 api 30 140351916693312 Executing super_krishak.notifications.tasks.notify_users_by_participation: ecd22786-0623-4108-90d5-018ed8cbeb0b @2023-09-30 09:00:00 ERROR 2023-09-30 14:45:09,525 tasks 31 140351916693312 GCMError occurred while sending message.

The code that is used for task enqueuing and execution

@huey.db_task(retries=0)
def notify_users_by_participation(
    title, extra, quiz_id=None, participation_status=None
):
    users = User.objects.all()

    if quiz_id and participation_status:
        if participation_status == QuizParticipationStatus.PARTICIPATED:
            users = users.filter(user_quiz_answers__quiz_id=quiz_id).distinct()
        elif participation_status == QuizParticipationStatus.NOT_PARTICIPATED:
            users = users.exclude(user_quiz_answers__quiz_id=quiz_id).distinct()

    devices = GCMDevice.objects.filter(user__in=users).distinct("user")

    try:
        devices.send_message(title, extra=extra)
    except GCMError:
        logging.error("GCMError occurred while sending message.")
        pass
    except Exception as e:
        logging.error(f"Error sending message: {e}")
        pass

Function to schedule daily notifications

@huey.periodic_task(crontab(hour=3))
def schedule_daily_notifications():
    notifications = ScheduledNotification.objects.all()
    now = timezone.now()
    quiz = Quiz.ongoing().first()

    if not quiz:
        return

    for notification in notifications:
        push_time = notification.push_time
        eta = now.replace(
            hour=push_time.hour, minute=push_time.minute, second=0, microsecond=0
        )
        extra = {
            "type": "quiz",
            "quiz_id": quiz.id,
            "status": notification.participation_status,
        }

        notify_users_by_participation.schedule(
            args=(
                notification.notification_text,
                extra,
                quiz.id,
                notification.participation_status,
            ),
            eta=eta,
        )

Another unusual behavior that I am getting is, even if there are only 4 notifications in the ScheduledNotifications model. The task gets enqueued and executed automatically multiple times within a very short amount of time. I still am unable to pinpoint the cause of this unexpected behavior. If there is something wrong with my code then please do point that out as well. I have attached the Huey logs as well if you want to have a look. Thanks huey-logs.odt