Skip to main content

Introduction

A backend does not receive push notifications, it sends them. The .NET 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 user secrets or an environment variable on the server and never ship it to a client.

2. Register the client

Program.cs
appsettings.json
A delegate works too, and ProjectId / ApiKey fall back to LERIX_NOTIFICATIONS_PROJECT_ID / LERIX_NOTIFICATIONS_API_KEY. The client is registered as a LerixNotifications singleton and is independent of AddLerix, so a service can send notifications without reporting errors.

3. Send a notification

OrdersService.cs
DeviceTokens are the Lerix device ids your client apps obtain from getDeviceId() and send to your backend at login. Save the returned NotificationId, every other method takes it.

Broadcast to a topic

Schedule for later

Parameters

Platform notes for Sound and ImageUrl are in the REST API reference.

4. Manage a sent notification

UpdateAsync, UnsendAsync and CancelAsync return the raw JSON response as a JsonElement?; DeliveriesAsync returns one JsonElement per recipient.

Errors

Every method throws LerixApiException 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 SendByTopic 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

Instantiate LerixNotifications directly when one backend serves more than one app: