2015-01-04 114 views
0

當我嘗試通過GCM發送消息時,我總是收到一個空的json響應{}使用python-gcm的空json響應

我使用python-gcm,這是我的代碼:

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() 

我在做什麼錯了,我應該嘗試另一種選擇? 其他細節:

  • 我在用燒瓶。
  • 在我的開發者谷歌控制檯我有谷歌地圖

    也被激活。

+0

@MartijnPieters是瘋了,只是爲了確保我使用正確的值。 – Ricardo 2015-01-04 13:56:25

+0

很好,但是從您發佈的內容中不清楚。 :-) – 2015-01-04 13:57:35

回答

0

看起來不是posible直接使用python-gcm。 然後,根據:

,並採取記標幟dry_run=True是用於測試。 下面的代碼就像一個魅力:)

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" 
+0

嘿,我正試圖實現這一點,一切似乎都經過很好,我成功的反應回來了,但我仍然沒有看到推送發送設備。任何想法?一切似乎都設置好了,API密鑰,代碼等。 – parchambeau 2015-04-21 10:03:19

+0

@parchambeau您是否設置了dry_run = False? – Ricardo 2015-04-21 14:02:54