Skip to main content

Introduction

A backend does not receive push notifications, it sends them. The Python SDK wraps the push notifications REST API in a typed client so your services can notify the users of your mobile and web apps without hand-writing HTTP calls.

1. Create a private key

Notifications are sent to the devices of the project your app uses, which is usually a different project from the one your backend reports errors to. In that project’s Project Settings, create a private key with the send push notifications permission.
A private key can send notifications to all of a project’s users. Keep it in an environment variable on the server and never ship it to a client.

2. Create the client

Pass the target project id and the private key to LerixNotifications:
notifications.py
With no arguments it reads LERIX_NOTIFICATIONS_PROJECT_ID and LERIX_NOTIFICATIONS_API_KEY from the environment. The client is independent of lerix.init(), so you can use it in a service that does not report errors at all.

3. Send a notification

orders.py
device_tokens are the Lerix device ids your client apps obtain from getDeviceId() and send to your backend at login. Save the returned notification_id, every other method takes it.

Broadcast to a topic

Schedule for later

Parameters

send() accepts these as keyword arguments, or as a SendNotificationInput dataclass. Platform notes for sound and image_url are in the REST API reference.

4. Manage a sent notification

Errors

Every method raises LerixApiError when the API rejects the request. code is the backend’s error code and status the HTTP status:
Invalid input such as a missing topic with send_by_topic is rejected locally with code INVALID_INPUT before any request is made. The full list of codes is in Error & success codes.

Several target projects

Create one LerixNotifications per app when one backend serves more than one: