Django的推送通知与装置自定义模型 [英] Django push notifications with device custom model

查看:333
本文介绍了Django的推送通知与装置自定义模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于Django的推送通知的问题。

I have a question about Django push notifications.

在我的Django项目,我有我的移动设备型号。在这个模型中我有一个令牌(用于发送推送通知),设备平台(iOS或Android)及其他设备信息的所有设备信息。

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.

现在我已经实现逻辑对于这些推送通知的发送和我会使用像某些库Django的推通知的。

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

我已阅读文档和我意识到,这个库中已经在内部使用的模型相对于设备:GCMDevice或APNSDevice

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

我如何使用Django推通知我的设备型号?是否有一个整洁的方式做到这一点?

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

推荐答案

做完所有的工作时,你也许并不需要一个库,可以通过简单的HTTP岗位到GCM服务器发送GCM的消息,这只是一个code的几行。如果您使用的是蟒蛇请求库已经针对HTTP的东西,它实际上是一个单行

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'})

其中,消息是这是继 GCM文档的指导方针和看起来像你的GCM消息这个。

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 看起来像键= YOUR_GCM_KEY

auth_token will look like 'key=YOUR_GCM_KEY'

最后,如果你真的热衷于使用更复杂的库,但要preserve你的模型,考虑的 Python的GCM 它可以很容易地插入到Django的。

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.

这篇关于Django的推送通知与装置自定义模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆