Send Push Notification Using Pushy (Python)

July 2, 2019

I wanted to send alert messages from my Python application to a few Android devices when certain event triggered. The requirement is time sensitive: preferbly notification is received within 5 minutes. I doubt Firebase Cloud Messaging is fast enough, SMS is probably too costly, thus I wanted to give Pushy a spin since it is marketed as Lightning-fast, highly-reliable push notification delivery.

NOTE: Other options to experiement include WhatsApp Business API or Telegram API.

Notes about Pushy

def send_alert(to, message):
    PUSHY_SECRET_API_KEY = 'e56b....'
    params = {
        'to': to,
        'data': {
            'message': message
        }
    }

    url = f"https://api.pushy.me/push?api_key={PUSHY_SECRET_API_KEY}"
    r = requests.post(url, json=params)
    if r.status_code == 200:
        # print(json.dumps(r.json(), indent=2))
        data = r.json()
        print(json.dumps(data, indent=2))
    else:
        print(r.json()['error'])
        r.raise_for_status()

Usage

PUSHY_DEVICE_TOKEN = '6b35...'
send_alert(to=PUSHY_DEVICE_TOKEN, message='Hello')
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.