Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

push notifications with django and GCM

I have a android client app, and my server is in django.

I want to implement push-notifications in the server, to notify the specific users when changes in data related to them are happening, for example.

I have found those links:

  • https://django-gcm.readthedocs.org/en/latest/quickstart.html
  • https://github.com/bogdal/django-gcm
  • https://github.com/jleclanche/django-push-notifications

Not really sure what which link I need and for what.

An example for what I want is:

A user makes a request to server and changes data, and the server sends a push notification to another user.

So what package should I install? and maybe any tutorials to follow?? I am looking for any info to help me.

thanks a lot!

--- EDIT ----

So I did as e4c5 suggested, and I have a table which contains Device ID coupled with user.

I register the device to the GCM when app is installed. I am also adding the Device_id to the table in my database (with the user field being null for now) and save the Device_id in my client app as well.

Now, when a user logs in, I send a request to my server which couples the logged in user with the Device ID.

When the user logs out - the user field is null again.

Problem is - if UserA is currently logged out (not in my database) but should receive a notification - my server will look at the database -> Devices IDs table and wouldn't find the userA in any of the user fields (because he is currently logged out).

As a result - the notification will be lost (my server would not send it to the GCM).

Best would be if when the user logs in again, he will get all the notifications that were sent to him while he was logged out.

How can I do this?

thanks!

like image 993
Ofek Agmon Avatar asked Sep 04 '15 08:09

Ofek Agmon


1 Answers

The answer is that you don't need any packages at all. GCM is really simple, all you need to do is to make an HTTP post to the url provided by google and that you can do with your favorite python http client library. If you don't have one yet, I recommend python requests. What most of the GCM libraries actually does is to add a thin layer on top of an http library such as this.

You will need to have a table to store the GCM registration ids. If people who use your app are asked to sign up for an account, your Django model might look something like this.

class Device(models.Model) :
    ANDROID = 1
    IPHONE = 2
    CHROME = 3
    OTHER = 4

    DEVICE_CHOICES = ( (ANDROID, 'Android'), (IPHONE, 'iPhone') , (CHROME,'Chrome'), (OTHER,'Others'))

    device_id = models.CharField(unique = True, max_length = 1024, default='')
    device_type = models.SmallIntegerField(choices = DEVICE_CHOICES)
    user = models.ForeignKey(User, null = True)

You might want to tap the django login and logout signals to update the user field (or clear it) when someone logs in/out

like image 140
e4c5 Avatar answered Sep 22 '22 19:09

e4c5