jazzband / django-push-notifications

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

changing `active` state on the `push_notifications_gcmdevice` table #616

Closed Avramo closed 11 months ago

Avramo commented 3 years ago

Hi!

I use django with django-push-notifications. When sending push notifications, I would like to know which users did/didn't get the message.

Currently I use this code after sending the notifications to know how many were sent/not sent:

    all_users = queryset.count()
    active_fcm = GCMDevice.objects.filter(user_id__in=queryset.values_list('id', flat=True), active=True).count()
    return Response({
        'sent': active_fcm, 
        'not_sent': all_users - active_fcm
        }, status=status.HTTP_201_CREATED)

I noticed this is not 100% accurate as the active field does not always reflect the actual users' status. I removed the app and the active field was still True.

When and how does the active column in the push_notifications_gcmdevice table change?

Also what does device.send_message() return, and what is the meaning of success (ie. sent, active, or received)?

Pro1ooEgor commented 2 years ago

When and how does the active column in the push_notifications_gcmdevice table change?

Have you found an answer to this?

Avramo commented 2 years ago

Unfortunately not. I'd appreciate any leads though. Thanks

On Tue, Oct 26, 2021 at 8:07 PM Egor Novik @.***> wrote:

When and how does the active column in the push_notifications_gcmdevice table change?

Have you found an answer to this?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jazzband/django-push-notifications/issues/616#issuecomment-952138837, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANT6QH3EVGK5YBYZMAVGS23UI3N4FANCNFSM5AUBTXUQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

Pro1ooEgor commented 2 years ago

Hi. Today I found answer. It's interesting because out team found a lead in the documentation of another library (fcm-django). It says there:

Functionality:

  • automatic device pruning: devices to which notifications fail to send are marked as inactive

And I went to look at the code of current library, where found same behavior. https://github.com/jazzband/django-push-notifications/blob/master/push_notifications/gcm.py#:~:text=def%20_cm_handle_response(registration_ids,return%20response

There is a comment there

If error is NotRegistered or InvalidRegistration, then we will deactivate devices because this registration ID is no more valid and can't be used to send messages, otherwise raise error

I think it would be nice to have a description of this moment in the readme file

Avramo commented 2 years ago

Great, I'll check it out, thanks!

On Thu, Oct 28, 2021 at 3:35 PM Egor Novik @.***> wrote:

Hi. Today I found answer. It's interesting because out team found a lead in the documentation of another library (fcm-django). It says there:

Functionality:

  • automatic device pruning: devices to which notifications fail to send are marked as inactive

And I went to look at the code of current library, where found same behavior.

https://github.com/jazzband/django-push-notifications/blob/master/push_notifications/gcm.py#:~:text=def%20_cm_handle_response(registration_ids,return%20response

There is a comment there

If error is NotRegistered or InvalidRegistration, then we will deactivate devices because this registration ID is no more valid and can't be used to send messages, otherwise raise error

I think it would be nice to have a description of this moment in the readme file

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jazzband/django-push-notifications/issues/616#issuecomment-953802064, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANT6QH7VRJJJ7FZR4PDJKMDUJE7RBANCNFSM5AUBTXUQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

Avramo commented 2 years ago

So I looked at the docs you mentioned and it seems that the active field should be automatically updated after sending a message. In my original question I noted that I found this not to be the case. Does this take time to update, could that be why I was not seeing the updates happen?

If error is NotRegistered or InvalidRegistration, then we will deactivate devices

What happens if the user just deletes the app, or switches phones, would the message to them fail with one of these errors? Do I need to unregister the user? Bottom line is, how can I be sure of who got the message? Thanks

henderson2350 commented 2 years ago

Is there a way to tell if a user has ENABLED push notifications on their device? not just whether or not the device has been registered?

jamaalscarlett commented 2 years ago

That would have to be handled via your android/ios/webapp. i.e. on the device, if the user disables notifications, you should send a request to your server to update the device model

jamaalscarlett commented 11 months ago

@henderson2350 I just tried this with FCM. Even when I disable app notifications on the phone, the notification is sent successfully from the backend. The blocking is occurring on the phone, so the detection would need to occur there.