OneSignal V2

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

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

Lovable Prompt
Lovable Prompt

Add push notification functionality to send targeted messages to users using the Despia SDK from: 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:

// 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();
};

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

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

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

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:

// 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

  • 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.

Need Help?

For additional support or questions, please contact our support team at support@despia.com

Updated on