simplegcm

simplegcm.gcm.

This module implements the Google Cloud Service API.

copyright:
  1. 2015 by Martin Alderete.
license:

BSD License, see LICENSE for more details.

exception simplegcm.gcm.GCMException[source]

Exception related to GCM service.

class simplegcm.gcm.Message(to=None, registration_ids=None, data=None, notification=None, options=None)[source]

GCM Message to send.

Parameters:
  • to (str) – A registation token or a topic (multicast)
  • registration_ids (list) – Registration device tokens
  • data (dict) – Custom data to send
  • notification (Notification) – Notification to send
  • options (Options) – Options for the message

Note

Messages MUST contain ‘to’ or ‘registration_ids’ at least

body

Return the payload which repesents the message.

Return type:dict
classmethod build_retry_message(message, registration_ids)[source]

Return a new Message using the given message as base.

Returns:A new message.
Return type:Message
notification_class

alias of Notification

options_class

alias of Options

class simplegcm.gcm.Notification(title=None, body=None, icon=None, sound=None, badge=None, tag=None, color=None, click_action=None, body_loc_key=None, body_loc_args=None, title_loc_key=None, title_loc_args=None)[source]

Notificication.

Parameters:
  • title (str) – Notification title.
  • body (str) – Notification body.
  • icon (str) – Notification icon.
  • sound (str) – Sound to be played.
  • badge (str) – Badge to be used.
  • tag (bool) – Indicates whether each notification results in a new entry or not.
  • color (str) – Color of the icon.
  • click_action (str) – Action binded to notification click.
  • body_loc_key (str) – Corresponds to “loc-key” in APNS payload.
  • body_loc_args (list) – Arguments. Corresponds to “loc-args” in APNS payload.
  • title_loc_key (str) – Corresponds to “title-loc-key” in APNS payload.
  • title_loc_args (list) – Arguments. Corresponds to “title-loc-args” in APNS payload.

Note

All of the above parameters are platform dependent. See https://developers.google.com/cloud-messaging/server-ref#table1b

class simplegcm.gcm.Result(canonicals=None, multicast_id=None, success=None, failure=None, unregistered=None, unavailables=None, backoff=None, message=None, raw_result=None)[source]

Response from GCM.

Parameters:
  • canonicals (dict) – Map with old token and new token
  • multicast_id (int) – Unique ID (number) identifying the multicast message.
  • success (dict) – Map registration_id with message_id successfully sent.
  • failure (dict) – Map registration_id with error message.
  • unregistered (list) – List with registration_ids not registered.
  • unavailables (list) – List with registration_ids to re-send.
  • backoff (int) – Estimated time to wait before retry.
  • message (Message) – Related message.
  • raw_result (dict) – JSON returned by GCM server.
get_retry_message()[source]

Return a new Message.

Returns:A new message to retry or None.
Return type:Message or None
class simplegcm.gcm.Options(collapse_key=None, priority=None, content_available=None, delay_while_idle=None, time_to_live=None, delivery_receipt_requested=None, dry_run=None, restricted_package_name=None)[source]

Options.

Parameters:
  • collapse_key (str) – Identifies a group of messages.
  • priority (int) – Messages priority.
  • content_available (bool) – Flag to wakes up an inactivce device (iOS).
  • delay_while_idle (bool) – Flag to sends when the device becomes available.
  • time_to_live (int) – How long the message should be store in GCM.
  • delivery_receipt_requested (bool) – Flag to confirm a delivered message.
  • restricted_package_name (str) – Specifies the package name of the application.
  • dry_run (bool) – Flag to sets testing mode.

Note

All of the above parameters are optionals. See https://developers.google.com/cloud-messaging/server-ref#table1

class simplegcm.gcm.Sender(api_key=None, url=None)[source]

GCM Sender.

Example:

>>> import simplegcm
>>> sender = simplegcm.Sender(api_key='your_api_key')
>>> r_ids = ['ABC', 'HJK']
>>> data = {'score': 5.1}
>>> opt = {'dry_run': True}
>>> message = simplegcm.Message(registration_ids=r_ids,
                                data=data, options=opt)
>>> ret = sender.send(message)
>>> retry_msg = ret.get_retry_message()
>>> if retry_msg:
>>>     print('Retry')
>>>     ret = g.send(retry_msg)
>>> else:
>>>     print('All sent!')
Parameters:
  • api_key (str) – Service’s API key
  • url (str) – Service’s URL
result_class

alias of Result

send(message)[source]

Send a message.

Parameters:message – A Message
Returns:Result object
Return type:Result
Raises:GCMException – If there was an error.