Search code examples
androiddjangodjango-rest-frameworkandroid-notificationsdjango-rest-framework-jwt

Implementing notifications with Django Rest Framework jwt authentication


I have developed an android app which uses Django rest framework as backend with JWT Authentication.

Now I want to add notification service to it. There are many notification service providers like firebase cloud message and azure notification centre.

I need some guidance to implement this

Like, this is my user profile model:

class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete = models.CASCADE)
    is_teacher = models.BooleanField(default= False)

and I only want to send a notification to the users who are logged in as teacher.


Solution

  • For notifications in my app i was using Firebase. There is realy nice library for connecting it to django. If you wanna send notifications only to teachers, you can do it in bulk like in this example:

    from fcm_django.models import FCMDevice
    
    devices = FCMDevice.objects.all()
    
    devices.send_message(title="Title", body="Message")
    devices.send_message(title="Title", body="Message", data={"test": "test"})
    devices.send_message(data={"test": "test"})
    

    Just change FCMDevice.objects.all() to set of devices connected to teachers.