[Solved]-Push notifications with django and GCM


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



A new answer to reflect the updated information, requirements in the edited question.

Queuing messages to be sent to a user when he is not logged in is moving away from the realm of GCM. This is simply a matter of maintaining a copy of messages on the server.

This can be easily achieved using Redis but can also be done by adding a new django model which saves the messages. ie. you are building a store and forward system now.

Soon as the login signal is fired, it should look in this newly created table and fire off the messages (provided that they are not stale)



Even though this has been answered. I would recommend you to use a package that is actively developed and supported. There is no point in re-inventing the wheel. Also, you will get other advantages / functionalities that you may need in the future (For e.g. APNS support). I would recommend you to use either of the following:

  1. django-instapush
  2. django-push-notifications

The former is developed and maintained by myself. Can be used to send both GCM and APNS notifications to android and IOS devices respectively and also support mongoengine if that’s what you use for your django models. Here is a link to the documentation.



You don’t need all this. You can send notification to your android app by sending a get request to google. Try below code.

    notification_params['event'] = event
    notification_params['message'] = msg
    notification_params['timstamp'] = datetime.datetime.now()

    values = {
                "to": [list of android gcm registration keys],
                "collapse_key": "collapse key" ,
                "data": json.dumps(notification_params)

    headers = {
               "Authorization":"key="+GCM API key


Leave a comment