简体   繁体   English

带有设备自定义模型的Django推送通知

[英]Django push notifications with device custom model

I have a question about Django push notifications. 我对Django推送通知有疑问。

In my Django project I have my model for mobile device. 在我的Django项目中,我有用于移动设备的模型。 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. 在此模型中,我拥有所有设备信息,例如令牌(用于发送推送通知),设备平台(iOS或Android)和其他设备信息。

Now I have implement the logic for sending of these push notifications and I would to use some library like django-push-notifications . 现在,我已经实现了发送这些推送通知的逻辑,并且我将使用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. 我已经阅读了文档,并且意识到该库已经在内部使用了针对设备的模型:GCMDevice或APNSDevice。

How can I use django-push-notification with my device model? 如何在我的设备模型中使用django-push-notification? Is there a neat way to do this? 有没有一种整洁的方式做到这一点?

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. 完成所有工作后,您可能不需要库,只需向GCM服务器发送简单的HTTP帖子即可发送GCM消息,这仅需几行代码。 If you are using the python requests library already for http stuff, it's practically a one liner. 如果您已经将python请求库用于http东西,那么它实际上是一个衬里。

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. 其中message是您的GCM消息,它遵循GCM文档的准则,如下所示。

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

auth_token will look like 'key=YOUR_GCM_KEY' auth_token类似于“ 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. 最后,如果您真的想使用一个更复杂的库,但又想保留您的模型,请考虑可以很容易地将其插入django的Python GCM

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM