简体   繁体   English

使用django和GCM推送通知

[英]push notifications with django and GCM

I have a android client app, and my server is in django. 我有一个Android客户端应用程序,我的服务器在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. 所以我按照e4c5的建议做了,我有一个包含设备ID和用户的表。

I register the device to the GCM when app is installed. 我在安装应用程序时将设备注册到GCM。 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. 我还将Device_id添加到我的数据库中的表中(用户字段现在为null )并将Device_id保存在我的客户端应用程序中。

Now, when a user logs in, I send a request to my server which couples the logged in user with the Device ID. 现在,当用户登录时,我向我的服务器发送请求,该请求将登录用户与设备ID耦合。

When the user logs out - the user field is null again. 当用户注销时 - 用户字段再次为null

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). 问题是 - 如果UserA当前已注销(不在我的数据库中)但应该收到通知 - 我的服务器将查看数据库 - >设备ID表,并且在任何用户字段中都找不到userA(因为他是目前已退出)。

As a result - the notification will be lost (my server would not send it to the GCM). 结果 - 通知将丢失(我的服务器不会将其发送到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. GCM非常简单,您只需要在google提供的URL上发布HTTP帖子 ,并且可以使用您最喜欢的python http客户端库。 If you don't have one yet, I recommend python requests . 如果你还没有,我推荐python请求 What most of the GCM libraries actually does is to add a thin layer on top of an http library such as this. 大多数GCM库实际上做的是在诸如此类的http库之上添加一个薄层。

You will need to have a table to store the GCM registration ids. 您需要有一个表来存储GCM注册ID。 If people who use your app are asked to sign up for an account, your Django model might look something like this. 如果要求使用您的应用的用户注册一个帐户,您的Django模型可能看起来像这样。

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登录和注销信号来更新用户字段(或清除它)

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. 在未登录时将要发送给用户的消息排队正在远离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. 这可以使用Redis轻松实现,但也可以通过添加保存消息的新django模型来完成。 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 eg APNS support). 此外,您将获得将来可能需要的其他优势/功能(例如APNS支持)。 I would recommend you to use either of the following: 我建议你使用以下任何一种方法:

  1. django-instapush Django的instapush
  2. django-push-notifications Django的推通知

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. 可用于分别向Android和IOS设备发送GCM和APNS通知,如果您使用的django型号也支持mongoengine。 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. 您可以通过向谷歌发送获取请求来向您的Android应用发送通知。 Try below code. 试试下面的代码。

    notification_params={}
    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 = {
               "UserAgent":"GCMServer",
               "ContentType":"application/json",
               "Authorization":"key="+GCM API key
              }

   requests.post(url="https://android.googleapis.com/gcm/send",data=values,headers=headers)

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

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