Reputation: 3976
I'm working on a python project on Google App Engine and I'm using Parse to send push notifications.
Eyerything is working fine but I noticed some lag when I have to send multiple notifications to different devices given by POST
rest request to Parse server.
To be clearer:
I have a loop with N elements, for every element I have to send a push notifications and so I have to make a POST request to Parse and every connection (of course) take about 300msec to be completed, so it is quite slow to complete entire loop.
I think should be better to change the request in async request but after searching on documentation and on Google I found no clear example how doing it with urllib2
or urlfetch
and how pass headers with Parse Key and Applicaton Id..
Working (not async) code is
parse_connection = httplib.HTTPSConnection('api.parse.com', 443)
parse_connection.connect()
parse_app_id = parse_settings.APPLICATION_ID
parse_rest_api_key = parse_settings.REST_API_KEY
parse_connection.request('POST', '/1/push',
json.dumps({
"channels": ["blabla"],
"data": {
"alert": "A",
"title":"B",
"badge": "Increment",
"category": "C",
"sound": "default",
"additionalInfos": {"X": "Custom dict"}}
}),
{
"X-Parse-Application-Id": parse_app_id,
"X-Parse-REST-API-Key": parse_rest_api_key,
"Content-Type": "application/json"
})
return json.loads(parse_connection.getresponse().read())
To make it async I think I should use urlfetch
with
rpc = urlfetch.create_rpc()
options = json.dumps({"channels": ["blabla"],
"data": {
"alert": "A",
"title": "B",
"badge": "Increment",
"category": "C",
"sound": "default",
"additionalInfos": {"X": "Custom dict"}},
})
urlfetch.make_fetch_call(rpc, "https://api.parse.com/1/push:443", options)
But I cannot find examples how adding header..any suggestion? Thanks!
Upvotes: 2
Views: 632
Reputation: 483
rpc = urlfetch.create_rpc(deadline=60)
url = "https://api.parse.com/1/push:443"
request_params = {
"channels": ["blabla"],
"data": {
"alert": "A",
"title":"B",
"badge": "Increment",
"category": "C",
"sound": "default",
"additionalInfos": {"X": "Custom dict"}}
}
headers = {
"X-Parse-Application-Id": parse_app_id,
"X-Parse-REST-API-Key": parse_rest_api_key,
"Content-Type": "application/json"
}
urlfetch.make_fetch_call(rpc,
url,
payload=json.dumps(request_params),
method=urlfetch.POST,
headers=headers)
You can always Look into the source if you need more help
Upvotes: 5