Programmatic Notifications

In addition to the OneSignal web portal you may use OneSignal's Server REST API to send transactional push notifications programmatically based on events or user actions on your platform.

📘

OneSignal REST API Documentation

The full documentation is available at https://documentation.onesignal.com/reference/push-notification

When sending notifications through OneSignal's API, you may target users by specifying predefined segments, filtering by parameters, or specific OneSignal IDs or other identifiers.

Send to Users

Sending notifications to all devices of a user is the most common use case when sending transactional or targeted notifications. You can easily do that using OneSignal ID, External ID or Custom alias name. See User Management for details on configuring each of these strategies

Programmatically send notification to specific users

Send to Segments

Segments are the most common way to target large groups of devices. You may specify which segments you want to target and, optionally, which ones you don't want to receive the notification. See Data Tags (Subscription Management) for details on using tags to create segments, both programmatically and by allowing users to self-subscribe.

Programatically send notification to segments

Send to Specific Devices

You can programmatically send a push notification to a specific device based on Push Subscription ID. See User Management for details on how to retrieve the Push Subscription ID for a specific device.

Programmatically send notification to specific devices

OneSignal Demo

❗️

App ID and API Key

The App ID and API key used for the demo below have been disabled for future usage. Please use your App ID and API Key available in your OneSignal dashboard under Settings > Keys & IDs.

The following example highlights how to send a push notification to a specific user, identified by the external id 'median-testuser'. The messages is localized based on the user's device settings.

cURL Command

cURL is a versatile command-line tool that allows you to transfer data to and from servers. It is widely used in various programming languages and tools for making HTTP requests. Whether you're using Python, JavaScript, or any other language, you can utilize cURL commands to interact with APIs such as the OneSignal API.

curl --location 'https://api.onesignal.com/notifications' \
--header 'Authorization: {{YOUR ONESIGNAL API KEY}}' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json'
--data '{
  "app_id": "{{YOUR ONESIGNAL APP ID}}",
  "target_channel": "push",
  "headings": {"en": "English Title", "es": "Spanish Title"},
  "contents": {"en": "English Message", "es": "Spanish Message"},
  "include_aliases": {
    "external_id": [
      "median-testuser"
      ]
  }
}'

Demo Recording

Programmatic Push Notification Example

Programmatic Push Notification Example