0

I am implementing notifications for mobile device.

I successfully setup Firebase with my project and can send push notifications to users. But my problem is on iOS when a user denies notification permissions I still need a way to have the notification show up when the user opens the app.

After a little research I found that i could do that with Django Channel Layers, but am still slightly confused on how exactly it works.

From my understanding, on the client side I setup WebSockets, open the WebSocket connection and send the data, which I am able to do.

Then on the server side I setup Django Channels, listen for the open connection and receive the data, which I am also able to do.

My question is how do I send the data to the intended user?

Here's my code:

React Native:

const ws = new WebSocket('ws://ws/notif-socket/');

useEffect(() => {
    ws.onopen = () => {
      console.log('Connected!');
    };
    ws.onerror = e => {
      console.log(e.message);
    };
  }, []);

const data = {
   to_user: user_id.toString(),
   title: 'Notification',
   body: 'User_2 liked your post',
   image: imageURL,
};

ws.send(JSON.stringify(data));

Django:

# models.py
class Notification(models.Model):
    from_user = models.ForeignKey(
        User, on_delete=models.CASCADE, related_name='from_user')
    to_user = models.ForeignKey(
        User, on_delete=models.CASCADE, related_name='to_user')
    title = models.CharField(max_length=100)
    body = models.TextField(default=None, blank=True)
    image = models.CharField(max_length=300, default=None, blank=True)
    date = models.DateTimeField(auto_now_add=True)

# consumers.py
class NotificationConsumer(WebsocketConsumer):

    def connect(self):
        self.accept()

    def receive(self, text_data):
        data = json.loads(text_data)
        print(data)

Should I be using Django Channels for this use case?

Appreciate any help, thanks!!

jdez
  • 189
  • 1
  • 10
  • Maybe have the app check for notifications from an API when opened? – nigel239 Sep 13 '22 at 18:15
  • Wouldn't that defeat the purpose of django channels though? because that's what makes it happen in real time – jdez Sep 13 '22 at 18:19
  • you can still do this through the sockets. Just make sure you have the client request it. How else will you know when they opened the app? The client will have to send/ask for something. You can implement an api in websockets. – nigel239 Sep 13 '22 at 18:21
  • You can access the user variable with `self.scope['user']`. You will still need the client to instantiate the websocket connection. – nigel239 Sep 13 '22 at 18:43
  • Thanks. Different question, but I am using Django Rest Framework Token auth. Do you know how I can use the Token auth with django channels? – jdez Sep 13 '22 at 18:55
  • You could send the token through the channel, and have the client store it locally. Then the client needs to send it for verification with every request. https://www.django-rest-framework.org/api-guide/authentication/#generating-tokens – nigel239 Sep 13 '22 at 18:58
  • I have done that but when I try self.scope['user'] I get AnonymousUser – jdez Sep 13 '22 at 19:00
  • You would need to authenticate the user first, from the token. https://stackoverflow.com/questions/43392889/how-do-you-authenticate-a-websocket-with-token-authentication-on-django-channels – nigel239 Sep 13 '22 at 19:02
  • Have you been able to resolve this? If you haven't I can help. My approach has me creating custom tokens and custom sessions. I would need to understand what your notification is for. i.e for a chat or just system notificaions. that way I can advice on how to proceed. – Durodola Opemipo Nov 10 '22 at 23:55

0 Answers0