Python Setup


Send push notifications to your Python app with Pushy.

At any time, you may refer to our Python demo implementation file for a working demo.

Create an App

Note: No credit card needed. Upgrade to the Pro plan when you're ready by visiting the Billing page.


Note: If you have already created an app in the Pushy Dashboard for another platform, simply configure your existing dashboard app with your Web Push Hostname with your company domain name in the App Settings tab and proceed to the next step.

If you're targeting other platforms, please fill in their respective app identifier fields.


Click Create and proceed to the next step.

Install the SDK

Run the following command in the root directory of your project to install the latest version of the pushy-python package from PyPI:

pip3 install pushy-python

Note: The SDK is compatible with Python 3.2 or later.

Modify Python App

Import the package in one of your .py files like so:

import pushy

Invoke pushy.listen() to initialize the SDK:

# Initialize Pushy
pushy.listen()
Register Devices

Users need to be uniquely identified to receive push notifications.

Every user is assigned a unique device token that you can use to push it at any given time. Once the user has been assigned a device token, it should be stored in your application's backend database.


Invoke pushy.register() to register devices for notifications:

try:
    # Register the device for push notifications
    deviceToken = pushy.register({ 'appId': 'YOUR_APP_ID' })
    
    # Persist token in your backend database
except Exception as err:
    # Print error to console
    print('Pushy registration error: ' + str(err))

Note: Please make sure to replace YOUR_APP_ID with your Pushy App ID in the Pushy Dashboard -> Click your app -> App Settings -> App ID.

Listen for Notifications

Invoke pushy.setNotificationListener() to handle incoming push notifications:

# Define a method to process incoming notifications
def backgroundNotificationListener(data):
    # Print "message" payload value to console
    print('Received notification: ' + str(data))

    # Execute any custom logic here

# Pass listener to the Pushy Python SDK
pushy.setNotificationListener(backgroundNotificationListener)

Feel free to modify this sample code to suit your own needs.


Note: You may currently only register one notification listener at a time for your app.

Parse Notification Data

Any payload data that you send with your push notifications is made available to your app via the data parameter of your notification listener.

If you were to send a push notification with the following payload:

{"id": 1, "success": true, "message": "Hello World"}

Then you'd be able to retrieve each value from within your notification listener callback like so:

id = data['id'] // number
success = data['success'] // bool
message = data['message'] // string

Note: Unlike GCM / FCM, we do not stringify your payload data, except if you supply JSON objects or arrays.

Subscribe to Topics

Optionally subscribe the user to one or more topics to target multiple users with a shared interest when sending notifications.

Depending on your app's notification criteria, you may be able to leverage topics to simply the process of sending the same notification to multiple users. If your app only sends personalized notifications, skip this step and simply target individual users by their unique device tokens.


Add the following code to subscribe the device to a Pub/Sub topic:

# Make sure the user is registered
if pushy.isRegistered():
    try:
        # Subscribe the user to a topic
        pushy.subscribe('news')

        # Notify subscribe successful
        print('Subscribed to topic successfully')
    except Exception as err:
        # Print error to console
        print('Subscribe failed: ' + str(err))

Note: Replace news with your own case-sensitive topic name that matches the following regular expression: [a-zA-Z0-9-_.]+.


You can then notify multiple users subscribed to a certain topic by specifying the topic name (prefixed with /topics/) as the to parameter in the Send Notifications API.


Last but not least, optionally invoke pushy.loop_forever() to keep your Python app running and listening to notifications once the main thread has completed execution:

# Avoid program termination (keeps your app from terminating
# until you call pushy.disconnect())

pushy.loop_forever()
Send Test Notification

Run your Python app, copy and paste the generated device token from the console, and select your app to send a test push notification:

Note: You can specify a topic instead of a device token (i.e. /topics/news). Also, if your app is not automatically detected, please manually copy the Secret API Key from the Dashboard and paste it into the form.

Did you receive the notification? If not, reach out, we'll be glad to help.


Congratulations on implementing Pushy in your Python app!

To start sending push notifications to your users, start persisting device tokens in your backend, and invoke the Send Notifications API when you want to send a notification. Follow our step-by-step guide: