|
| 1 | +python-gcm |
| 2 | +====================== |
| 3 | + |
| 4 | +.. image:: https://secure.travis-ci.org/geeknam/python-gcm.png?branch=master |
| 5 | + :alt: Build Status |
| 6 | + :target: http://travis-ci.org/geeknam/python-gcm |
| 7 | + |
| 8 | +Python client for Google Cloud Messaging for Android (GCM) |
| 9 | + |
| 10 | +Installation |
| 11 | +------------- |
| 12 | + |
| 13 | +.. code-block:: bash |
| 14 | +
|
| 15 | + pip install python-gcm |
| 16 | +
|
| 17 | +Features |
| 18 | +------------ |
| 19 | + |
| 20 | +* Supports multicast message |
| 21 | +* Resend messages using exponential back-off |
| 22 | +* Proxy support |
| 23 | +* Easily handle errors |
| 24 | + |
| 25 | +Usage |
| 26 | +------------ |
| 27 | + |
| 28 | +RTFM `here <http://developer.android.com/guide/google/gcm/gcm.html>`__ |
| 29 | + |
| 30 | +Basic |
| 31 | + |
| 32 | +.. code-block:: python |
| 33 | +
|
| 34 | + from gcm import GCM |
| 35 | +
|
| 36 | + gcm = GCM(API_KEY) |
| 37 | + data = {'param1': 'value1', 'param2': 'value2'} |
| 38 | +
|
| 39 | + # Plaintext request |
| 40 | + reg_id = '12' |
| 41 | + gcm.plaintext_request(registration_id=reg_id, data=data) |
| 42 | +
|
| 43 | + # JSON request |
| 44 | + reg_ids = ['12', '34', '69'] |
| 45 | + response = gcm.json_request(registration_ids=reg_ids, data=data) |
| 46 | +
|
| 47 | + # Extra arguments |
| 48 | + res = gcm.json_request( |
| 49 | + registration_ids=reg_ids, data=data, |
| 50 | + collapse_key='uptoyou', delay_while_idle=True, time_to_live=3600 |
| 51 | + ) |
| 52 | +
|
| 53 | +Error handling |
| 54 | + |
| 55 | +.. code-block:: python |
| 56 | +
|
| 57 | + # Plaintext request |
| 58 | + reg_id = '12345' |
| 59 | + try: |
| 60 | + canonical_id = gcm.plaintext_request(registration_id=reg_id, data=data) |
| 61 | + if canonical_id: |
| 62 | + # Repace reg_id with canonical_id in your database |
| 63 | + entry = entity.filter(registration_id=reg_id) |
| 64 | + entry.registration_id = canonical_id |
| 65 | + entry.save() |
| 66 | + except GCMNotRegisteredException: |
| 67 | + # Remove this reg_id from database |
| 68 | + entity.filter(registration_id=reg_id).delete() |
| 69 | + except GCMUnavailableException: |
| 70 | + # Resent the message |
| 71 | +
|
| 72 | + # JSON request |
| 73 | + reg_ids = ['12', '34', '69'] |
| 74 | + response = gcm.json_request(registration_ids=reg_ids, data=data) |
| 75 | +
|
| 76 | + # Handling errors |
| 77 | + if 'errors' in response: |
| 78 | + for error, reg_ids in response['errors'].items(): |
| 79 | + # Check for errors and act accordingly |
| 80 | + if error is 'NotRegistered': |
| 81 | + # Remove reg_ids from database |
| 82 | + for reg_id in reg_ids: |
| 83 | + entity.filter(registration_id=reg_id).delete() |
| 84 | + if 'canonical' in response: |
| 85 | + for reg_id, canonical_id in response['canonical'].items(): |
| 86 | + # Repace reg_id with canonical_id in your database |
| 87 | + entry = entity.filter(registration_id=reg_id) |
| 88 | + entry.registration_id = canonical_id |
| 89 | + entry.save() |
| 90 | +
|
| 91 | +Exceptions |
| 92 | +------------ |
| 93 | +Read more on response errors `here |
| 94 | +<http://developer.android.com/guide/google/gcm/gcm.html#success>`__ |
| 95 | + |
| 96 | + |
| 97 | +* GCMMalformedJsonException |
| 98 | +* GCMConnectionException |
| 99 | +* GCMAuthenticationException |
| 100 | +* GCMTooManyRegIdsException |
| 101 | +* GCMNoCollapseKeyException |
| 102 | +* GCMInvalidTtlException |
| 103 | +* GCMMissingRegistrationException |
| 104 | +* GCMMismatchSenderIdException |
| 105 | +* GCMNotRegisteredException |
| 106 | +* GCMMessageTooBigException |
| 107 | +* GCMInvalidRegistrationException |
| 108 | +* GCMUnavailableException |
| 109 | + |
| 110 | +.. image:: http://t.qkme.me/35gjhs.jpg |
| 111 | + :alt: Gotta catch them all |
0 commit comments