python - GCM python 服务器端实现 : Error=MissingRegistration

标签 python google-app-engine google-cloud-messaging

我正在服务器端实现 GCM 推送服务,并面临 GCM 响应错误 => Error=MissingRegistration

有人知道为什么会出现这个错误吗?

示例代码是:

    def make_request(self, data, is_json=True):

        headers = {
            'Authorization': 'key=%s' % self.api_key,
        }
        # Default Content-Type is defaulted to application/x-www-form-urlencoded;charset=UTF-8
        if is_json:
            headers['Content-Type'] = 'application/json'

        if not is_json:
            data = urllib.urlencode(data)
        req = urllib2.Request(GCM_URL, data, headers)

        try:
            response = urllib2.urlopen(req).read()
        except urllib2.HTTPError as e:
            if e.code == 400:
                raise GCMMalformedJsonException("The request could not be parsed as JSON")
            elif e.code == 401:
                raise GCMAuthenticationException("There was an error authenticating the sender account")
        except urllib2.URLError as e:
            raise GCMConnectionException("There was an internal error in the GCM server while trying to process the request")

        if is_json:
            response = json.loads(response)
        return response

if __name__ == '__main__':
data = {
    'param1': '1',
    'param2': '2'
}
_gcm = GCM('AIzaSyC0lft1_11RGk2aMyt6hoNolC3in5rN50Y')
res = _gcm.construct_payload(
    registration_ids=['APA91bF9MFLWBhZ7y4F87z0D8enceLxOD1GLl92cQSF9lp5Z-VlhxX'], data=data, collapse_key='foo',
    delay_while_idle=True, time_to_live=3600, is_json=True
)
payload = json.loads(res)

result = _gcm.make_request(payload, False)

最佳答案

问题已解决,此代码有效

 def make_request(self):
            json_data = {"collapse_key" : "msg", 
                         "data" : {
                                   "data": "xyz",
                       }, 
                    "registration_ids": ['APA91bGi13Rg2l_*******beNOGxxP25o0hmtpg'],
            }


            url = 'https://android.googleapis.com/gcm/send'
            myKey = "AIza********N50Y" 
            data = json.dumps(json_data)
            headers = {'Content-Type': 'application/json', 'Authorization': 'key=%s' % myKey}
            req = urllib2.Request(url, data, headers)
            f = urllib2.urlopen(req)
            response = json.loads(f.read())


            self.response.out.write(json.dumps(response,sort_keys=True, indent=2) )    

关于python - GCM python 服务器端实现 : Error=MissingRegistration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14495794/

相关文章:

python Fabric : How to retrieve a filelist of a dir

javascript - 如何在django应用程序中实现ajax?

android - 使用 https ://fcm. googleapis.com/fcm/send 发送 Firebase 数据消息时注册无效?

android - Google Cloud Messaging ,如何控制消息队列?

python - Python 中的 C _IOR 函数等效于什么?

javascript - Pygame解释器

python - 使用 vspipe 的 Python main(argv) 函数中的 Vapoursynth 输出节点

python - SSLHandshakeError - Google App Engine

google-app-engine - Web 应用程序中的 Paypal 集成

c# - Android应用程序和Winform应用程序之间发送和接收消息