Django push notifications with device custom model

2019-08-13 18:23发布

问题:

I have a question about Django push notifications.

In my Django project I have my model for mobile device. In this model I have all device info like token (used to send a push notification), device platform (iOS or Android) and other device info.

Now I have implement the logic for sending of these push notifications and I would to use some library like django-push-notifications.

I have read the documentation and I realized that this library already uses internally a model with respect to devices: GCMDevice or APNSDevice.

How can I use django-push-notification with my device model? Is there a neat way to do this?

回答1:

Having done all that work, you probably don't need a library, you can send your GCM messages by making simple HTTP posts to the GCM server it's only a few lines of code. If you are using the python requests library already for http stuff, it's practically a one liner.

requests.post( 'https://gcm-http.googleapis.com/gcm/send', 
  data = json.dumps(message), 
  headers = {'Authorization': auth_token,'Content-Type': 'application/json'})

Where message is your GCM message which follows the guidelines on GCM docs and looks like this.

{ "notification": {
    "title": "Portugal vs. Denmark",
    "text": "5 to 1"
  },
 "to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx..."
}

auth_token will look like 'key=YOUR_GCM_KEY'

Lastly if you are really keen to use a more complex library but want to preserve your models, consider Python GCM which can be plugged into django quite easily.