lynx   »   [go: up one dir, main page]

Installation & Configuration

Install the OneSignal TypeScript SDK and configure it for your application.
npm install @onesignal/node-onesignal
import Onesignal from '@onesignal/node-onesignal';

const configuration = Onesignal.createConfiguration({
    restApiKey: '<YOUR_REST_API_KEY>'
    organizationApiKey: '<YOUR_ORGANIZATION_API_KEY>',
});

const apiInstance = new Onesignal.DefaultApi(configuration);

Send messages

Send push, email, and SMS messages to your users via the OneSignal API. For more details on how to configure messages, see the Sending messages with the OneSignal API guide.

Send push notifications

Send push notifications to web and mobile Subscriptions.
const notification = new OneSignal.Notification();
notification.app_id = 'YOUR_APP_ID';
notification.contents = {
  en: 'Hello from your app!'
};
notification.headings = {
  en: 'Push Notification'
};
notification.included_segments = ['All'];

const { id } = await client.createNotification(notification);
console.log('Notification sent with ID:', id);

Send email

Send emails to your email Subscriptions.
const notification = new OneSignal.Notification();
notification.app_id = 'YOUR_APP_ID';
notification.contents = {
  en: 'This is your email message content'
};
notification.headings = {
  en: 'Email Subject Line'
};
notification.email_subject = 'Important Update';
notification.email_body = '<h1>Hello!</h1><p>This is an HTML email.</p>';
notification.included_segments = ['All'];
notification.channel_for_external_user_ids = 'email';

const { id } = await client.createNotification(notification);
console.log('Email notification sent with ID:', id);

Send SMS

Send SMS “text” messages to SMS Subscriptions.
const notification = new OneSignal.Notification();
notification.app_id = 'YOUR_APP_ID';
notification.contents = {
  en: 'Your SMS message content here'
};
notification.included_segments = ['All'];
notification.channel_for_external_user_ids = 'sms';
notification.sms_from = '+1234567890'; // Your SMS sender number

const { id } = await client.createNotification(notification);
console.log('SMS notification sent with ID:', id);
To find out about the other Server SDK endpoints, please check the complete API reference.
Лучший частный хостинг