Skip to main content

インストールと構成

OneSignal TypeScript SDKをインストールし、アプリケーション用に構成します。
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);

メッセージを送信する

OneSignal API経由でユーザーにプッシュ、メール、SMSメッセージを送信します。メッセージの構成方法の詳細については、OneSignal APIでメッセージを送信するガイドを参照してください。

プッシュ通知を送信する

Webおよびモバイルのサブスクリプションにプッシュ通知を送信します。
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);

メールを送信する

メールのサブスクリプションにメールを送信します。
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);

SMSを送信する

SMSのサブスクリプションにSMS「テキスト」メッセージを送信します。
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);
他のServer SDKエンドポイントについては、完全なAPIリファレンスを確認してください。