Python client for Google Cloud Messaging for Android (GCM)
pip install python-gcm- Supports multicast message
- Resend messages using exponential back-off
- Proxy support
- Easily handle errors
RTFM here
Basic
fromgcmimportGCMgcm=GCM(API_KEY)
data= {'param1': 'value1', 'param2': 'value2'}
# Plaintext requestreg_id='12'gcm.plaintext_request(registration_id=reg_id, data=data)
# JSON requestreg_ids= ['12', '34', '69']
response=gcm.json_request(registration_ids=reg_ids, data=data)
# Extra argumentsres=gcm.json_request(
registration_ids=reg_ids, data=data,
collapse_key='uptoyou', delay_while_idle=True, time_to_live=3600
)Error handling
# Plaintext requestreg_id='12345'try:
canonical_id=gcm.plaintext_request(registration_id=reg_id, data=data)
ifcanonical_id:
# Repace reg_id with canonical_id in your databaseentry=entity.filter(registration_id=reg_id)
entry.registration_id=canonical_identry.save()
exceptGCMNotRegisteredException:
# Remove this reg_id from databaseentity.filter(registration_id=reg_id).delete()
exceptGCMUnavailableException:
# Resent the message# JSON requestreg_ids= ['12', '34', '69']
response=gcm.json_request(registration_ids=reg_ids, data=data)
# Handling errorsif'errors'inresponse:
forerror, reg_idsinresponse['errors'].items():
# Check for errors and act accordinglyiferroris'NotRegistered':
# Remove reg_ids from databaseforreg_idinreg_ids:
entity.filter(registration_id=reg_id).delete()
if'canonical'inresponse:
forreg_id, canonical_idinresponse['canonical'].items():
# Repace reg_id with canonical_id in your databaseentry=entity.filter(registration_id=reg_id)
entry.registration_id=canonical_identry.save()Read more on response errors here
- GCMMalformedJsonException
- GCMConnectionException
- GCMAuthenticationException
- GCMTooManyRegIdsException
- GCMNoCollapseKeyException
- GCMInvalidTtlException
- GCMMissingRegistrationException
- GCMMismatchSenderIdException
- GCMNotRegisteredException
- GCMMessageTooBigException
- GCMInvalidRegistrationException
- GCMUnavailableException

