0

我目前正在尝试使用 python-gcm 完成 Android 推送通知的实现,并且遇到了在推送通知时没有从 GCM 服务器获得响应的问题。

我已经验证我正在获取 device_id,并且正在使用的 API 密钥/发件人 ID 是正确的,并且一旦我点击 GCM 服务器,我就会收到成功的响应,但我仍然看到我的设备上没有显示推送通知.

这通常是因为我在 Google Developer Console 中设置 GCM 的方式不正确吗?

第一次实现这一点,所以我确信可能只是我错过了一些常见的东西。

下面是我设置的示例 flask-gcm 代码。我收到了成功的回复,我在想的谷歌设置方面一定有什么遗漏。

 if device.platform == 'Android':
        # data = {'param1': 'value1', 'param2': 'value2'}
        data = {'data': message}

        # Set the device id
        reg_id = device.device_id

        Logger.info('Before sending the push JSON ' + str(reg_id))

        # Pass in the reg id of who to send to
        multicast = JSONMessage([reg_id], data, dry_run=False)

        try:
            # attempt send
            res_multicast = gcm_service.send(multicast)

            for res in [res_multicast]:
                # nothing to do on success
                for reg_id, msg_id in res.success.items():
                    Logger.info("Successfully sent %s as %s" % (reg_id, msg_id))
                    Logger.info("THIS IS THE REG ID THAT WAS SENT TO: " + str(reg_id))
                    Logger.info("THIS IS THE MESSAGE ID THAT WAS SENT " + str(msg_id))
                    Logger.info("RES SUCESS ITEMS: " + str(res.success.items()))

                # update your registration ID's
                for reg_id, new_reg_id in res.canonical.items():
                    Logger.info("Replacing %s with %s in database" % (reg_id, new_reg_id))

                # probably app was uninstalled
                for reg_id in res.not_registered:
                    Logger.info("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():
                    Logger.info("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.
                    # Logger.info("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"
4

0 回答 0