问题
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:
- https://django-gcm.readthedocs.org/en/latest/quickstart.html
- https://github.com/bogdal/django-gcm
- https://github.com/jleclanche/django-push-notifications
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!
回答1:
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
回答2:
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. 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. 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)
回答3:
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 e.g. APNS support). I would recommend you to use either of the following:
- django-instapush
- django-push-notifications
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. Here is a link to the documentation.
回答4:
You don't need all this. You can send notification to your android app by sending a get request to google. 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)
来源:https://stackoverflow.com/questions/32393692/push-notifications-with-django-and-gcm