Skip to content

Latest commit

History

68 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

python-gcm

Build StatusCode Health

Python client for Google Cloud Messaging for Android (GCM)

Installation

pip install python-gcm

Features

  • Supports multicast message
  • Resend messages using exponential back-off
  • Proxy support
  • Easily handle errors

Usage

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()

Exceptions

Read more on response errors here

  • GCMMalformedJsonException
  • GCMConnectionException
  • GCMAuthenticationException
  • GCMTooManyRegIdsException
  • GCMNoCollapseKeyException
  • GCMInvalidTtlException
  • GCMMissingRegistrationException
  • GCMMismatchSenderIdException
  • GCMNotRegisteredException
  • GCMMessageTooBigException
  • GCMInvalidRegistrationException
  • GCMUnavailableException

Gotta catch them all

About

Python client for Google Cloud Messaging for Android (GCM)

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors