A very simple Python client for the Pushy notification service API.
Simply install using pip:
$ pip install PushySDK
or clone this repository and run the following:
$ python setup.py install
You will need to install the following dependencies:
- requests
- six
This can be done either manually or via pip with the included requirements.txt
file as follows:
$ pip install -r requirements.txt
You will first need to signup for a Pushy account and integrate the Pushy SDK into your Android and/or iOS mobile application (please refer to the comprehensive Pushy Documentation). Once you have successfully sent a test notification from the Pushy Dashboard you are ready to write some python!
First, get your application API key from the Pushy Dashboard (Applications > Your App > API Authentication). Once you have this you can then do the following:
>>> from PushySDK import Pushy
>>> pushy=Pushy('<YOUR_API_KEY>')
You can now request information regarding a specific device (you can get device IDs from the Pushy Dashboard):
>>> pushy.deviceInfo('<YOUR_DEVICE_ID>')
This will return a python dictionary object of information about the device as follows:
{
'device': {'date': 1445207358, 'platform': 'android'},
'presence': {
'online': True,
'last_active': {'date': 1464006925, 'seconds_ago': 215}
},
pending_notifications': [
{
'date': 1464008196,
'expiration': 1466600196,
'payload': {'message': 'Hello World!'}, 'id': '5742fe0407c3674e226892f9'
}
]
}
You can also return presence information for single or multiple devices as follows:
>>> pushy.devicePresence(['<YOUR_DEVICE_ID>'])
{'presence': [
{
'online': False,
'last_active': 1429406442,
'id': 'a6f36efb913f1def30c6'
},
{
'online': True,
'last_active': 1468349965,
'id': 'fe8f7b2c12e83e5b41d2'
}
]}
To send a notification to a device or devices:
>>> data={'message':'Hello from Python and Pushy!'}
>>> pushy.push('<YOUR_DEVICE_ID>', data)
>>> pushy.push(['<YOUR_DEVICE_ID_1>', '<YOUR_DEVICE_ID_2>'], data)
To add extra data for iOS APNs notifications, a utility function exists to form the request as follows:
>>> title="Python/Pushy Notification"
>>> message='Hello from Python and Pushy!'
>>> badge=1
>>> sound="ping.aiff"
>>> apn=pushy.makeIOSNotification(message, badge, sound, title)
>>> pushy.push(['<YOUR_ANDROID_DEVICE_ID>', '<YOUR_IOS_DEVICE_ID>'], data, notification=apn)
The push()
method will return a dictionary which reports the success or failure and a unique ID for the notification which can be used to track its status:
{'success': True, 'id': '5742ea5dacf3a92e17ba7126'}
You can track a notifications status as follows:
>>> pushy.notificationStatus('<YOUR_NOTIFICATION_ID>')
{
"push": {
"date": 1464003935,
"payload": {
"message": "Hello World!"
},
"expiration": 1466595935,
"pending_devices": [
"fe8f7b2c102e883e5b41d2"
]
}
}