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 a Web Push Notification

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

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

Send an Email Notification

Send email notifications to your users.
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 an SMS Notification

Send SMS notifications to mobile phone numbers.
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.
Лучший частный хостинг