> ## Documentation Index
> Fetch the complete documentation index at: https://setup.despia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# OneSignal

> Send targeted remote push notifications via OneSignal to users even when your app is closed.

<Note>
  OneSignal is discontinuing Player IDs and adopting a new approach to sending targeted notifications using “external\_ids” generated by your application and provided to OneSignal. These external player IDs could be the user’s ID or device ID accessible via `despia.uuid`
</Note>

<iframe src="https://www.youtube.com/embed/lsr0VsUJjio" title="YouTube video player" frameborder="0" className="w-full aspect-video rounded-xl" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen />

<Card title="Lovable Prompt" icon="sparkles">
  Add push notification functionality to send targeted messages to users using the Despia SDK from: [https://www.npmjs.com/package/despia-native](https://www.npmjs.com/package/despia-native)

  First, install the package: `npm install despia-native`

  Then import it: `import despia from 'despia-native'`

  After that, on every app load call ``despia(`setonesignalplayerid://?user_id=${YOUR-LOGGEDIN-USER-ID}`)``

  This feature will connect your own User ID stored in your database with the devices that are automatically registered in OneSignal.

  **Setup Requirements:**

  1. Create a OneSignal account and configure your app
  2. Set up iOS (Apple Push Key) and Android (Firebase) configurations in OneSignal
  3. **Important:** When configuring OneSignal, select "Native iOS" and "Native Android" platforms since Despia apps are native mobile applications
  4. Add your OneSignal App ID to your Despia project settings

  **Backend Integration Required**

  You'll need to create a backend endpoint to send notifications using OneSignal's REST API:

  ```javascript theme={null}
  // Backend API endpoint - send to specific user
  const sendNotification = async (externalUserId, title, message) => {
    const response = await fetch('https://onesignal.com/api/v1/notifications', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Basic YOUR_REST_API_KEY'
      },
      body: JSON.stringify({
        app_id: 'ONESIGNAL-APP-ID',
        include_external_user_ids: [externalUserId], // Array with single ID
        headings: { en: title },
        contents: { en: message }
      })
    });
    return response.json();
  };
  ```

  <Danger>
    **This feature requires native capabilities which will be fully provided by the “despia-native” npm package, no additional native libraries are needed!**
  </Danger>

  Please follow the installation instructions for the "despia-native" npm package closely, and do not modify my instructions. Implementation as mentioned is critical.
</Card>

**How it works:** Despia automatically imports the native OneSignal SDK at runtime. When users open your app, OneSignal generates a unique Player ID for their device. You can access this device-specific ID through Despia's SDK and store it with user accounts to send targeted notifications from your backend.

## **Installation**

Install the Despia package from NPM:

```
npm install despia-native
```

## **Usage**

### **1. Import the SDK**

```javascript theme={null}
import despia from 'despia-native';
```

### **2. Access OneSignal Player ID**

Set the OneSignal User ID (external\_id) via the despia() function on every app load:

```javascript theme={null}
// Example using Supabase, this works with ANY backend
supabase.auth.getUser().then(({ data }) => {
  const userId = data.user?.id;
  if (userId) {
    despia(`setonesignalplayerid://?user_id=${userId}`);
  }
});
```

## **Resources**

* [**NPM Package**](https://www.npmjs.com/package/despia-native)
* View full NPM documentation for additional configuration options

## **Integration**

This SDK is optimized for prompt-based AI builders, enabling quick integration of native features via minimal JavaScript into your generated apps.

For additional support or questions, please contact our support team at [support@despia.com](mailto:support@despia.com)
