Android 使用python gcm的空json响应
当我试图通过GCM发送消息时,我总是得到一个空的json响应Android 使用python gcm的空json响应,android,python,python-2.7,google-cloud-messaging,Android,Python,Python 2.7,Google Cloud Messaging,当我试图通过GCM发送消息时,我总是得到一个空的json响应{} 我正在使用,这是我的代码: from gcm import GCM API_KEY = "AldaSyAwnh5jAAAAAAABvBTyqDdMITCoEE8GLZ" my_gcm = GCM(api_key=API_KEY) data = {'param1': 'value1', 'param2': 'value2'} # JSON request #reg_ids = ['12', '34', '69'] reg_id
{}
我正在使用,这是我的代码:
from gcm import GCM
API_KEY = "AldaSyAwnh5jAAAAAAABvBTyqDdMITCoEE8GLZ"
my_gcm = GCM(api_key=API_KEY)
data = {'param1': 'value1', 'param2': 'value2'}
# JSON request
#reg_ids = ['12', '34', '69']
reg_ids = ['ALA91bE9QSkVKcmAAAAAAda8UsNs27R-29pJHDqwIiwqxSAStmhpFo2hKD0SipoCjgANvFJ7trdQZuJCjMaUAAAA6zmETYDncD9YTiVZ61eG1pwXdOJC7mozlt76OoyM81OasWi9_ibGklNfhWGhJpg']
response = my_gcm.json_request(registration_ids=reg_ids, data=data)
# Handling errors
if 'errors' in response:
for error, reg_ids in response['errors'].items():
# Check for errors and act accordingly
if error is 'NotRegistered':
# Remove reg_ids from database
for reg_id in reg_ids:
entity.filter(registration_id=reg_id).delete()
if 'canonical' in response:
for reg_id, canonical_id in response['canonical'].items():
# Repace reg_id with canonical_id in your database
entry = entity.filter(registration_id=reg_id)
entry.registration_id = canonical_id
entry.save()
我做错了什么,我应该尝试另一种选择吗?
其他详情:
- 我用的是烧瓶李>
- 在我的开发者谷歌控制台中,我有谷歌地图 也激活了
dry_run=True
用于测试。
下面的代码就像一个符咒:)
@MartijnPieters是一个分散的,只是为了确保我使用了正确的值。很好,但从你发布的内容来看,这并不清楚。:-)嘿,伙计,我正在尝试实现这一点,一切似乎都很顺利,我得到了成功的回复,但我仍然没有看到推送发送到设备。有什么想法吗?一切似乎都设置得很好,API密钥、代码等等@Parchambou您是否设置了dry_run=False?
from flask import Flask
app = Flask(__name__)
from flask.ext.gcm import GCM
API_KEY = "AldaSyAwnh5jAAAAAAABvBTyqDdMITCoEE8GLZ"
gcm = GCM()
app.config['GCM_KEY'] = API_KEY
gcm.init_app(app)
data = {'param1': 'value1', 'param2': 'value2'}
multicast = JSONMessage(["ALA91bE9QSkVKcmAAAAAAda8UsNs27R-29pJHDqwIiwqxSAStmhpFo2hKD0SipoCjgANvFJ7trdQZuJCjMaUAAAA6zmETYDncD9YTiVZ61eG1pwXdOJC7mozlt76OoyM81OasWi9_ibGklNfhWGhJpg"], data, collapse_key='my.key', dry_run=False)
try:
# attempt send
res_multicast = gcm.send(multicast)
for res in [res_multicast]:
# nothing to do on success
for reg_id, msg_id in res.success.items():
print "Successfully sent %s as %s" % (reg_id, msg_id)
# update your registration ID's
for reg_id, new_reg_id in res.canonical.items():
print "Replacing %s with %s in database" % (reg_id, new_reg_id)
# probably app was uninstalled
for reg_id in res.not_registered:
print "Removing %s from database" % reg_id
# unrecoverably failed, these ID's will not be retried
# consult GCM manual for all error codes
for reg_id, err_code in res.failed.items():
print "Removing %s because %s" % (reg_id, err_code)
# if some registration ID's have recoverably failed
if res.needs_retry():
# construct new message with only failed regids
retry_msg = res.retry()
# you have to wait before attemting again. delay()
# will tell you how long to wait depending on your
# current retry counter, starting from 0.
print "Wait or schedule task after %s seconds" % res.delay(retry)
# retry += 1 and send retry_msg again
except GCMAuthenticationError:
# stop and fix your settings
print "Your Google API key is rejected"
except ValueError, e:
# probably your extra options, such as time_to_live,
# are invalid. Read error message for more info.
print "Invalid message/option or invalid GCM response"
print e.args[0]
except Exception:
# your network is down or maybe proxy settings
# are broken. when problem is resolved, you can
# retry the whole message.
print "Something wrong with requests library"