Simplepush Blog

Actionable Notification with Callback in Python

Do you need to write a Python script that needs some user input but you don’t want to create a whole frontend or ssh into your server all the time? An actionable notification sent from your script might be the perfect solution.

Actionable notifications are push notifications with action buttons associated to them. Selecting an action usually has some immediate effect.

Simplepush is an app for iOS and Android which enables you to send actionable notifications to your devices through an API. With the Simplepush Python library you can send actionable notifications and get a callback directly in Python once an action is selected. Scroll down to see how easy it is.

Simplepush is free up to five actionable notifications per month and 100 push notifications (without actions) per month. For $9.99/year you get unlimited actionable notifications.

Requirements

We will assume that you already installed Python3 and Pip3. Python2 is not supported.

1) pip3 install simplepush

2) Install Simplepush on your Android or iOS device. After you launch the app for the first time, a personal key is presented to you. This key is necessary to send push notifications to your device.

Sending an Actionable Notification

The following Python code shows how to send an actionable notification to a device that’s associated to the key YourKey (you need to replace YourKey with the key you received when installing the app).

In addition to that the code defines a callback that fires once an action was selected.

import simplepush

def callback(action_selected, action_selected_at, action_delivered_at, feedback_id):
  print(action_selected)

simplepush.send(key='YourKey', message='Actionable notification', actions=['yes', 'no', 'maybe'], feedback_callback=callback)

If you run our example code and replaced the key with your key, you will immediately see a notification popping up on your device with an yes, no and maybe action.

Actionable Push Notification with three Actions

If you select one of the three actions, the callback function will be called and the selected action is printed to the console.

As you can see from the example code there are several additional arguments passed to the callback function:

  • action_selected - the name of the selected action
  • action_selected_at - timestamp of the time when the action was selected
  • action_delivered_at - timestamp of the time when the information reached the Simplepush backend (this can differ from action_selected_at since the device could be offline when the selection happens)
  • feedback_id - the underlying id that can be used to manually query the action information from the Feedback API

If you don’t want to block your whole Python program while waiting for an action to be selected, you can use the asynchronous version of the send function:

# For end-to-end encrypted notifications use simplepush.async_send_encrypted(...)
simplepush.async_send(key='YourKey', message='Actionable notification', actions=['yes', 'no'], feedback_callback=callback)

It is also important to note that there is a default timeout of 60 seconds associated to the feedback_callback. This means that an FeedbackActionTimeout exception will be thrown if no action has been selected at the moment of the timeout. To adjust the timeout, set the feedback_callback_timeout argument to the value that you want.

# Set timeout to 120 seconds
simplepush.send(key='YourKey', message='Actionable notification', actions=['yes', 'no'], feedback_callback=callback, feedback_callback_timeout=120)

You can find more information on how Simplepush Actions work in this blog post or on the Simplepush API site.

Share on:
Imprint