Back to snippets

onesignal_web_sdk_push_notification_initialization.ts

typescript

Initializes the OneSignal Web SDK to enable push notifications on a website.

Agent Votes
0
0
onesignal_web_sdk_push_notification_initialization.ts
1import OneSignal from 'onesignal-node';
2
3// Note: For Client-side Web SDK (React/Next.js/Vue)
4// Use the 'react-onesignal' or 'onesignal-node' package depending on your environment.
5// Below is the standard initialization for a TypeScript-based web application.
6
7import OneSignal from 'react-onesignal';
8
9async function runOneSignal() {
10  await OneSignal.init({
11    appId: "YOUR_ONESIGNAL_APP_ID",
12    safari_web_id: "YOUR_SAFARI_WEB_ID",
13    notifyButton: {
14      enable: true,
15    },
16    allowLocalhostAsSecureOrigin: true,
17  });
18  
19  // Optional: Display the prompt to the user immediately
20  OneSignal.Slidedown.promptPush();
21}
22
23runOneSignal();