Django here (here is the code)을 사용하여 Android 기기에 푸시 알림을 보내는 방법을 찾았습니다. 장고를 사용하여 C2DM (android)에 푸시 알림을 게시
은 그래서를 채택 내 코드는 다음과 같습니다def sendAndroidPushNotification(registration_id, collapse_key, a, b) :
try:
auth = getNewAndroidAuthorizationToken() # this works I'm fetching new token so this is up to date (its length is 267 characters)
push_request = urllib2.Request("https://android.apis.google.com/c2dm/send")
data = urllib.urlencode({'data.testA' : a,
'data.testB' : b,
'collapse_key' : collapse_key,
'registration_id': registration_id
})
push_request.add_data(data)
push_request.add_header('Authorization', 'GoogleLogin auth=' + auth)
push_request.add_header('Content-Type', 'application/x-www-form-urlencoded')
push_request.add_header('Content-Length', len(data))
urllib2.build_opener().open(push_request)
except urllib2.HTTPError as e:
print 'got exception during push notification'
print 'Reason: "{0}" code: {1}'.format(e.reason, e.code)
pass
이 나에게 오류를 줄 : "이유 :"허가되지 않은 "코드 : 401"(이 403이었다 어떤 점에서). httplib.HTTPSConnection 대신을 사용하는 버전 urllib2.Request에 동일한 문제가 있습니다.
마치 거의 code shown here과 같아서 완전히 혼란 스럽습니다. 내가 뭘 잘못하고있어?
편집 : 그냥 경우에, 여기에 내가 (그것이 잘 작동하는지 모양), 어쩌면 나의 해석이 잘못된 인증 토큰을 가져 오는 방법입니다
:
def getNewAndroidAuthorizationToken() :
request = urllib2.Request("https://www.google.com/accounts/ClientLogin")
data = urllib.urlencode({'accountType' : 'HOSTED_OR_GOOGLE',
'Email' : '[email protected]',
'Passwd' : 'asdjsdfa',
'service' : 'ac2dm',
'source' : 'com.mycompany.mypackage',})
request.add_data(data)
content = urllib2.build_opener().open(request)
lines = content.readlines()
for line in lines :
if line.find("Auth=")==0 :
return line[5:]
return