django-push-notifications in Django Rest Framework

792 Views Asked by At

I'm trying to use django-push-notifications library in my Django REST backend project. I have used Firebase Cloud Messaging. Here is the implementation;

settings.py

PUSH_NOTIFICATIONS_SETTINGS = {
    "FCM_API_KEY": "private key from FCM app"
}

views.py

user, created = User.objects.get_or_create(email=email, username=username)

if created:
    GCMDevice.objects.create(registration_id="token", cloud_message_type="FCM", user=user)

models.py

@receiver(post_save, sender=Action)
def create_new_action(sender, instance, created, **kwargs):
    if created:
        devices = GCMDevice.objects.filter(Q(user=instance.admin)|Q(user__in=instance.users.all()))
        msg = "New action is created in %s" % instance.name
        devices.send_message(msg)

My question is what should registration_id be while I'm creating the GCMDevice object for each registered user?

2

There are 2 best solutions below

0
On

The registration_id is the same as the endpoint key from the client-side PushSubscription object. which looks like this:

{
    "endpoint": "https://fcm.googleapis.com/fcm/send/...",
    "expirationTime": null,
    "keys": {
        "p256dh": "...",
        "auth": "..."
    }
}
0
On

registration_id come from your frontend

actually you must have an api that get user(frontend) fcm token or get it(fcm token) in user registration or login api's