### Project Setup and Installation Source: https://github.com/onesignal/react-onesignal/blob/main/example/README.md Instructions for setting up the React OneSignal project. This involves installing dependencies using npm and replacing a placeholder application ID with the actual ID from the OneSignal dashboard. ```bash npm install # Replace with your actual app id ``` -------------------------------- ### npm Start Script Source: https://github.com/onesignal/react-onesignal/blob/main/example/README.md Runs the React application in development mode. It opens the app in a browser at http://localhost:3000 and enables hot reloading for edits. Lint errors are displayed in the console. ```bash npm start ``` -------------------------------- ### Install react-onesignal with yarn Source: https://github.com/onesignal/react-onesignal/blob/main/README.md Installs the react-onesignal package using yarn. This is an alternative package manager for adding the library to your project. ```bash yarn add react-onesignal ``` -------------------------------- ### Install react-onesignal with npm Source: https://github.com/onesignal/react-onesignal/blob/main/README.md Installs the react-onesignal package using npm. This is the standard way to add the library to your project's dependencies. ```bash npm install --save react-onesignal ``` -------------------------------- ### npm Build Script Source: https://github.com/onesignal/react-onesignal/blob/main/example/README.md Builds the React application for production. It optimizes the build for performance, minifies the code, and includes filenames with hashes for efficient deployment. ```bash npm run build ``` -------------------------------- ### Set Log Level Source: https://github.com/onesignal/react-onesignal/blob/main/MigrationGuide.md Example of setting the log level for debugging in the OneSignal SDK. ```javascript OneSignal.Debug.setLogLevel("trace"); ``` -------------------------------- ### Opt-in to Push Notifications Source: https://github.com/onesignal/react-onesignal/blob/main/MigrationGuide.md Shows how to opt the current user into receiving push notifications using the OneSignal SDK. ```javascript OneSignal.User.PushSubscription.optIn(); ``` -------------------------------- ### Display Slidedown Prompt Source: https://github.com/onesignal/react-onesignal/blob/main/MigrationGuide.md Demonstrates how to display the OneSignal slidedown prompt to ask the user for notification permissions. ```javascript await OneSignal.Slidedown.promptPush(); ``` -------------------------------- ### OneSignal API Documentation Source: https://github.com/onesignal/react-onesignal/blob/main/MigrationGuide.md Comprehensive list of OneSignal methods available in the react-onesignal package, including initialization, event handling, user management, and notification features. ```APIDOC OneSignal: init(options?: any): Promise Initializes the OneSignal SDK with provided options. on(event: string, listener: Function): void Adds an event listener for a specified event. off(event: string, listener: Function): void Removes an event listener. once(event: string, listener: Function): void Adds a one-time event listener. isPushNotificationsEnabled(callback?: Action): Promise Checks if push notifications are enabled for the user. showHttpPrompt(options?: AutoPromptOptions): void Displays the HTTP prompt for notification permission. registerForPushNotifications(options?: RegisterOptions): Promise Registers the user for push notifications. setDefaultNotificationUrl(url: string): void Sets the default URL for notifications. setDefaultTitle(title: string): void Sets the default title for notifications. getTags(callback?: Action): void Retrieves all tags associated with the user. sendTag(key: string, value: any, callback?: Action): Promise Sends a single tag to OneSignal. sendTags(tags: TagsObject, callback?: Action): Promise Sends multiple tags to OneSignal. deleteTag(tag: string): Promise> Deletes a single tag from OneSignal. deleteTags(tags: Array, callback?: Action>): Promise> Deletes multiple tags from OneSignal. addListenerForNotificationOpened(callback?: Action): void Adds a listener for when a notification is opened. setSubscription(newSubscription: boolean): Promise Sets the user's subscription status. showHttpPermissionRequest(options?: AutoPromptOptions): Promise Shows the HTTP permission request prompt. showNativePrompt(): Promise Shows the native prompt for notification permission. showSlidedownPrompt(options?: AutoPromptOptions): void Shows the slidedown prompt for notification permission. showCategorySlidedown(options?: AutoPromptOptions): void Shows the category slidedown prompt. showSmsSlidedown(options?: AutoPromptOptions): void Shows the SMS slidedown prompt. showEmailSlidedown(options?: AutoPromptOptions): void Shows the email slidedown prompt. showSmsAndEmailSlidedown(options?: AutoPromptOptions): void Shows the SMS and email slidedown prompt. getNotificationPermission(onComplete?: Function): Promise Gets the current notification permission status. getUserId(callback?: Action): Promise Retrieves the current user's ID. getSubscription(callback?: Action): Promise Retrieves the current subscription status. setEmail(email: string, options?: SetEmailOptions): Promise Sets the user's email address. setSMSNumber(smsNumber: string, options?: SetSMSOptions): Promise Sets the user's SMS number. logoutEmail(): void Logs out the user's email. logoutSMS(): void Logs out the user's SMS. setExternalUserId(externalUserId: string | undefined | null, authHash?: string): Promise Sets an external user ID for the user. removeExternalUserId(): Promise Removes the external user ID. getExternalUserId(): Promise Retrieves the external user ID. provideUserConsent(consent: boolean): Promise Provides user consent for notifications. getEmailId(callback?: Action): Promise Retrieves the user's email ID. getSMSId(callback?: Action): Promise Retrieves the user's SMS ID. sendOutcome(outcomeName: string, outcomeWeight?: number | undefined): Promise Sends an outcome event to OneSignal. ``` -------------------------------- ### npm Test Script Source: https://github.com/onesignal/react-onesignal/blob/main/example/README.md Launches the test runner in an interactive watch mode. This script is used for running tests and provides more information on how to manage tests. ```bash npm test ``` -------------------------------- ### OneSignal Debug Namespace API Source: https://github.com/onesignal/react-onesignal/blob/main/MigrationGuide.md Allows control over the logging level for debugging purposes. ```APIDOC Debug Namespace: setLogLevel(logLevel: string) Turns on logging with the given log level. Supported levels: "trace", "debug", "info", "warn", "error". ``` -------------------------------- ### OneSignal Slidedown Namespace API Source: https://github.com/onesignal/react-onesignal/blob/main/MigrationGuide.md Provides methods to display various subscription prompts (push, SMS, email) and manage event listeners for the slidedown. ```APIDOC Slidedown Namespace: promptPush(options: AutoPromptOptions) Displays the notification permission prompt. promptPushCategories(options: AutoPromptOptions) Displays the notification permission prompt for notification categories. promptSms(options: AutoPromptOptions) Displays the SMS subscription prompt. promptEmail(options: AutoPromptOptions) Displays the email subscription prompt. promptSmsAndEmail(options: AutoPromptOptions) Displays the SMS and email subscription prompts. addEventListener(event: "slidedownShown", listener: (wasShown: boolean) => void) Adds an event listener for the `slidedownShown` event. removeEventListener(event: "slidedownShown", listener: (wasShown: boolean) => void) Removes an event listener for the `slidedownShown` event. ``` -------------------------------- ### OneSignal Initialization (v2) Source: https://github.com/onesignal/react-onesignal/blob/main/MigrationGuide.md Updates the OneSignal initialization function to 'init' and accepts a single config object for appId. This replaces the previous initialization hook. ```javascript import OneSignal from 'react-onesignal'; OneSignal.init({ appId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' }); ``` -------------------------------- ### Add Notification Click Listener Source: https://github.com/onesignal/react-onesignal/blob/main/README.md Example of how to add an event listener to the 'click' event for OneSignal notifications in a React application. ```javascript OneSignal.Notifications.addEventListener('click', (event) => { console.log('The notification was clicked!', event); }); ``` -------------------------------- ### Request Notification Permission Source: https://github.com/onesignal/react-onesignal/blob/main/MigrationGuide.md Requests push notification permission from the user using the native browser prompt. This is an asynchronous operation. ```javascript await OneSignal.Notifications.requestPermission(); ``` -------------------------------- ### Service Worker File Update Source: https://github.com/onesignal/react-onesignal/blob/main/MigrationGuide.md Updates the service worker file path for the OneSignal SDK. This change is necessary for the new SDK version. ```javascript importScripts("https://onesignal.com/sdks/OneSignalSDKWorker.js"); ``` ```javascript importScripts("https://onesignal.com/sdks/web/v16/OneSignalSDK.sw.js"); ``` -------------------------------- ### Notifications Namespace API Source: https://github.com/onesignal/react-onesignal/blob/main/MigrationGuide.md Provides methods and properties for interacting with OneSignal notifications, including setting defaults, checking support, managing permissions, and handling events. ```APIDOC Notifications Namespace: requestPermission(): Promise Description: Requests push notifications permission via the native browser prompt. Sync/Async: async setDefaultUrl(url: string): Promise Description: Sets the default URL for notifications. Parameters: - url (string): The URL to set as default. Sync/Async: async setDefaultTitle(title: string): Promise Description: Sets the default title for notifications. Parameters: - title (string): The title to set as default. Sync/Async: async isPushSupported(): boolean Description: Returns true if the current browser supports web push. Sync/Async: sync permission: boolean Description: Returns true if your site has permission to display notifications. permissionNative: "default" | "granted" | "denied" Description: Returns browser's native notification permission status. Possible values: - "default": end-user has not accept or decided yet - "granted": permission granted - "denied": permission denied addEventListener(event: string, callback: (arg: any) => {}): void Description: Adds an event listener for notification events. Supported Events: - "click" - "foregroundWillDisplay" - "dismiss" - "permissionPromptDisplay" - "permissionChange" (argument type: boolean) Parameters: - event (string): The name of the event to listen for. - callback (function): The function to execute when the event is triggered. Sync/Async: sync removeEventListener(callback: () => {}): void Description: Removes the specified event listener. Parameters: - callback (function): The event listener function to remove. Sync/Async: sync ``` -------------------------------- ### OneSignal Push Subscription Namespace API Source: https://github.com/onesignal/react-onesignal/blob/main/MigrationGuide.md Manages the user's push notification subscription status, including opting in/out and listening for subscription changes. ```APIDOC Push Subscription Namespace: id: string Gets the current user's ID. token: string Gets the current user's push notification token. optedIn: boolean Gets a boolean value indicating whether the current user is subscribed to push notifications. optIn(): Promise Subscribes the current user to push notifications. optOut(): Promise Unsubscribes the current user from push notifications. addEventListener(event: "change", listener: (change: SubscriptionChangeEvent) => void) Adds an event listener for the `change` event. removeEventListener(event: "change", listener: (change: SubscriptionChangeEvent) => void) Removes an event listener for the `change` event. ``` -------------------------------- ### OneSignal User Namespace API Source: https://github.com/onesignal/react-onesignal/blob/main/MigrationGuide.md Provides an overview of the User namespace methods in the OneSignal SDK for managing user aliases, emails, SMS numbers, and tags. All user functions are synchronous. ```APIDOC OneSignal.User: addAlias(label: string, id: string) Adds a new alias for the current user. addAliases(aliases: { [key: string]: string }) Adds multiple aliases for the current user. removeAlias(label: string) Removes an alias for the current user. removeAliases(labels: string[]) Removes multiple aliases for the current user. addEmail(email: string) Adds an email address for the current user. removeEmail(email: string) Removes an email address for the current user. addSms(smsNumber: string) Adds an SMS number for the current user. removeSms(smsNumber: string) Removes an SMS number for the current user. addTag(key: string, value: string) Adds a tag for the current user. addTags(tags: { [key: string]: string }) Adds multiple tags for the current user. removeTag(key: string) Removes a tag for the current user. removeTags(keys: string[]) Removes multiple tags for the current user. Example: OneSignal.User.addAlias("my_alias", "1234"); ``` -------------------------------- ### External User ID Update Source: https://github.com/onesignal/react-onesignal/blob/main/MigrationGuide.md Replaces the usage of `OneSignal.setExternalId` with `OneSignal.login` for identifying users. `OneSignal.logout()` should be used when previously clearing the external ID. ```javascript OneSignal.setExternalId("myId"); ``` ```javascript OneSignal.login("myId"); ``` -------------------------------- ### npm Eject Script Source: https://github.com/onesignal/react-onesignal/blob/main/example/README.md A one-way operation that removes the single build dependency (like Create React App) from the project. This provides full control over configuration files (webpack, Babel, ESLint) but cannot be undone. ```bash npm run eject ``` -------------------------------- ### Initialize OneSignal Source: https://github.com/onesignal/react-onesignal/blob/main/README.md Initializes the OneSignal SDK with your application ID. The init function returns a promise that resolves once OneSignal is loaded and ready to use. ```javascript import OneSignal from 'react-onesignal'; OneSignal.init({ appId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' }); ``` ```javascript await OneSignal.init({ appId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' }); // do other stuff ``` ```javascript const [initialized, setInitialized] = useState(false); OneSignal.init({ appId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' }).then(() => { setInitialized(true); OneSignal.Slidedown.promptPush(); // do other stuff }); ``` -------------------------------- ### OneSignal Init Options Source: https://github.com/onesignal/react-onesignal/blob/main/README.md Configuration options for the OneSignal SDK initialization. These options control aspects like user consent, service worker paths, and notification behavior. ```apidoc OneSignal.init(options: { appId: string; autoRegister?: boolean; autoResubscribe?: boolean; path?: string; serviceWorkerPath?: string; serviceWorkerUpdaterPath?: string; subdomainName?: string; allowLocalhostAsSecureOrigin?: boolean; requiresUserPrivacyConsent?: boolean; persistNotification?: boolean; notificationClickHandlerMatch?: string; notificationClickHandlerAction?: string; welcomeNotification?: object; notifyButton?: object; promptOptions?: object; webhooks?: object; [key: string]: any; }): Promise ``` -------------------------------- ### Robots.txt Configuration Source: https://github.com/onesignal/react-onesignal/blob/main/example/public/robots.txt This snippet defines the robots.txt file for the project, specifying crawling rules for user agents. It allows all user agents to crawl all paths. ```robots.txt User-agent: * Disallow: ``` -------------------------------- ### OneSignal TypeScript Interface Source: https://github.com/onesignal/react-onesignal/blob/main/README.md TypeScript interface defining the OneSignal SDK object and its methods, including init, login, logout, and consent management. ```typescript interface IOneSignalOneSignal { Slidedown: IOneSignalSlidedown; Notifications: IOneSignalNotifications; Session: IOneSignalSession; User: IOneSignalUser; Debug: IOneSignalDebug; login(externalId: string, jwtToken?: string): Promise; logout(): Promise; init(options: IInitObject): Promise; setConsentGiven(consent: boolean): Promise; setConsentRequired(requiresConsent: boolean): Promise; } ``` -------------------------------- ### Service Worker Parameters Source: https://github.com/onesignal/react-onesignal/blob/main/README.md Parameters for customizing the OneSignal service worker, including its scope and file path. ```apidoc serviceWorkerParam: { scope: string }; serviceWorkerPath: string; ``` -------------------------------- ### OneSignal Event Listeners Source: https://github.com/onesignal/react-onesignal/blob/main/README.md This section details how to add event listeners to react to various OneSignal events such as notification clicks, foreground display, dismissals, and permission changes. It also covers slidedown and push subscription events. ```APIDOC Notifications Namespace: 'click': Callback Argument Type: NotificationClickEvent 'foregroundWillDisplay': Callback Argument Type: NotificationForegroundWillDisplayEvent 'dismiss': Callback Argument Type: NotificationDismissEvent 'permissionChange': Callback Argument Type: boolean 'permissionPromptDisplay': Callback Argument Type: void Slidedown Namespace: 'slidedownShown': Callback Argument Type: boolean Push Subscription Namespace: 'change': Callback Argument Type: boolean ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.