用django和GCM推送通知 [英] push notifications with django and GCM

查看:185
本文介绍了用django和GCM推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android客户端应用程序,我的服务器在django。



我想在服务器中实现推送通知,以便在更改时通知具体的用户例如,与他们相关的数据正在发生。



我找到了这些链接:





不知道我需要什么链接为了什么。



我想要的一个例子是:



用户发出请求服务器和更改数据,并且服务器向其他用户发送推送通知。



那么我应该安装什么包?也许任何教程要遵循?
我正在寻找任何信息来帮助我。



非常感谢!



---编辑----



所以我做了e4c5建议,我有一个包含Device ID加上用户的表。 p>

当安装应用程序时,我将设备注册到GCM。
我还将Device_id添加到我的数据库中的表中(现在用户字段为 null ),并将Device_id保存在我的客户端应用程序中。



现在,当用户登录时,我向我的服务器发送请求,将该登录用户与设备ID相结合。



当用户注销时 - 用户字段再次为 null



问题是 - 如果UserA当前已注销(不在我的数据库中),但应收到通知 - 我的服务器将查看数据库 - > Devices IDs表,并且找不到userA在任何用户字段(因为他当前已经注销)。



结果 - 通知将丢失(我的服务器不会发送最好是如果用户再次登录,他将获得发送给他的所有通知,同时也可以通过以下方式发送给所有的通知:



他已经退出了。



我该怎么办? o这个?



谢谢!

解决方案

答案是你根本不需要任何软件包。 GCM非常简单,您只需要在网址上 HTTP post 即可由google提供,您可以使用自己喜欢的python http客户端库。如果您还没有,建议您 python请求。大多数GCM库实际上都是在这样的http库之上添加一个薄层。



您将需要一个表来存储GCM注册ids。如果使用您的应用程序的用户注册了一个帐户,您的Django模型可能看起来像这样。

 类设备(models.Model):
ANDROID = 1
IPHONE = 2
CHROME = 3
OTHER = 4

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

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

您可能想点击 django登录和注销信号更新用户字段(或清除它)有人登录/退出


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:

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!

解决方案

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

这篇关于用django和GCM推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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