私は自分の携帯電話にメッセージを送るのに疲れています。ブラウザを介して、この操作を実行するメソッドを呼び出し、registrationId、authTokenなどをログに記録しました。これは、ローカルサーバーでテストし、これらのキーを使用してメッセージが電話に送信されたためです。
ただし、App Engineでは、urlfetch.fetch
for' https://android.clients.google.com/c2dm/send
'の結果に401エラーがあります。
または、これが認証の問題である場合。メソッドが呼び出され、App Engineサーバーのメソッドの最後でエラーが発生するため、上記の問題ではないかと思います。
C2DMサーバーにリクエストを送信する方法は次のとおりです。
params = {
'registration_id':registrationId,
'collapse_key':0,
'data.payload':encoded_msg
}
paramsByte = urllib.urlencode(params)
logging.info(registrationId)
url = 'https://android.clients.google.com/c2dm/send'
logging.info(token)
result = urlfetch.fetch(url=url,
payload=paramsByte,
method=urlfetch.POST,
headers={'Content-Type':'application/x-www-form-urlencoded',
'Authorization':'GoogleLogin auth='+token}
)
どんな助けでもいただければ幸いです。ありがとう。
アップデート
これで、クライアントは提案どおりにホスティングサーバーで実行され、「https://android.clients.google.com/c2dm/send」に接続すると401エラーが発生します。
ただし、同じトークンとregIdを使用して端末で次のコマンドを使用すると、機能します。
curl --header "認証:GoogleLogin auth = your_authenticationid" "https://android.apis.google.com/c2dm/send" -d register_id = your_registration -d "data.payload =payload" -dcollapse_key = 0
サーバーでメソッドを呼び出すクライアントコード:
$.getJSON('http://myapp.appspot.com/method?userId='+userId+'&message='+theMessage+'&callback=?',
function(data)
{
console.log(data);
});
サーバーの完全なメソッドコード:
class PushHandler(webapp.RequestHandler):
'''This method sends the message to C2DM server to send the message to the phone'''
def get(self):
logging.info('aqui dentro')
userId = self.request.get('userId')
message = self.request.get('message')
callback = self.request.get('callback')
token = getToken(self) #this is a method I've implemented to get the token from C2DM servers by passing the SenderId and Password
registrationId = ''
contactNumber = ''
# Get the registrationId to send to the C2DM server to know which
# device it may send the message
regQuery = C2DMUser.all()
regQuery.filter('userId =', int(userId))
for k in regQuery:
registrationId = k.registrationId
# Builds the json to be sent to the phone
record_to_json = {
'userId':userId,
'message':message
}
data = []
data.append(record_to_json)
jsondata = simplejson.dumps(data) # Creates the json
# Encode the JSON String
u = unicode(jsondata, "utf-8")
encoded_msg = u.encode("utf-8")
params = {
'registration_id':registrationId,
'collapse_key':0,
'data.payload':encoded_msg
}
paramsByte = urllib.urlencode(params)
url = 'https://android.clients.google.com/c2dm/send'
logging.info(token)
result = urlfetch.fetch(url=url,
payload=paramsByte,
method=urlfetch.POST,
headers={'Content-Type':'application/x-www-form-urlencoded',
'Authorization':'GoogleLogin auth='+token}
)
data = []
params_key = { 'status_code':result.status_code }
data.append(params_key)
self.response.headers['Content-Type'] = 'application/json'
jsondata = simplejson.dumps(data)
if result.status_code == 200:
logging.info(result.status_code)
self.response.out.write('' + callback + '(' + jsondata + ')') # handle the JSONP
else:
logging.info(result.status_code)
self.response.out.write(result.status_code)