You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+82-61Lines changed: 82 additions & 61 deletions
Original file line number
Diff line number
Diff line change
@@ -16,40 +16,42 @@
16
16
17
17
> This is a JavaScript module that can be used to easily include [OneSignal](https://onesignal.com/) code in a website or app in practically any JS front-end codebase (not limited to React).
OneSignal is the world's leader for Mobile Push Notifications, Web Push, and In-App Messaging. It is trusted by 2 million+ businesses to send 9 billion Push Notifications per day.
23
23
24
24
You can find more information on OneSignal [here](https://onesignal.com/).
25
25
26
26
### Migration Guides
27
+
27
28
Versions 3.0 were recently released and include breaking changes. See the [Migration Guide](https://github.com/OneSignal/react-onesignal/blob/main/MigrationGuide.md) to update your implementation.
28
29
29
30
## Contents
31
+
30
32
-[Install](#install)
31
33
-[Usage](#usage)
32
34
-[API](#onesignal-api)
33
35
-[Advanced Usage](#advanced-usage)
34
36
35
37
---
36
-
## Install
37
38
38
-
You can use `yarn` or `npm`.
39
+
## Install
39
40
40
-
### Yarn
41
+
### npm
41
42
42
43
```bash
43
-
yarn add react-onesignal
44
+
npm install --save react-onesignal
44
45
```
45
46
46
-
### npm
47
+
### yarn
47
48
48
49
```bash
49
-
npm install --save react-onesignal
50
+
yarn add react-onesignal
50
51
```
51
52
52
53
---
54
+
53
55
## Usage
54
56
55
57
Initialize OneSignal with your `appId` via the `options` parameter:
You can pass other [options](https://documentation.onesignal.com/v11.0/docs/web-sdk#initializing-the-sdk) to the `init` function. Use these options to configure personalized prompt options, auto-resubscribe, and more.
|`autoRegister`|`boolean` (optional) | Whether or not to automatically register the user.|
96
+
|`autoResubscribe`|`boolean` (optional) | Whether or not to automatically resubscribe the user.|
97
+
|`path`|`string` (optional) | The path to the OneSignal service worker file.|
98
+
|`serviceWorkerPath`|`string` (optional) | The path to the OneSignal service worker script.|
99
+
|`serviceWorkerUpdaterPath`|`string` (optional) | The path to the OneSignal service worker updater script. |
100
+
|`subdomainName`|`string` (optional) | The subdomain of your OneSignal app.|
101
+
|`allowLocalhostAsSecureOrigin`|`boolean` (optional) | Whether or not to allow localhost as a secure origin.|
102
+
|`requiresUserPrivacyConsent`|`boolean` (optional) | Whether or not the user's consent is required.|
103
+
|`persistNotification`|`boolean` (optional) | Whether or not notifications should persist.|
104
+
|`notificationClickHandlerMatch`|`string` (optional) | The URL match pattern for notification clicks.|
105
+
|`notificationClickHandlerAction`|`string` (optional)| The action to perform when a notification is clicked.|
106
+
|`welcomeNotification`|`object` (optional) | The welcome notification configuration.|
107
+
|`notifyButton`|`object` (optional) | The notify button configuration.|
108
+
|`promptOptions`|`object` (optional) | Additional options for the subscription prompt.|
109
+
|`webhooks`|`object` (optional) | The webhook configuration.|
110
+
|`[key: string]`|`any`| Additional properties can be added as needed.|
106
111
107
112
**Service Worker Params**
108
113
You can customize the location and filenames of service worker assets. You are also able to specify the specific scope that your service worker should control. You can read more [here](https://documentation.onesignal.com/docs/onesignal-service-worker-faq#sdk-parameter-reference-for-service-workers).
109
114
110
115
In this distribution, you can specify the parameters via the following:
|`serviceWorkerParam`| Use to specify the scope, or the path the service worker has control of. Example: `{ scope: "/js/push/onesignal/" }`|
120
+
|`serviceWorkerPath`| The path to the service worker file. |
116
121
117
122
</details>
118
123
119
124
---
120
125
121
126
### Service Worker File
122
-
If you haven't done so already, you will need to add the [OneSignal Service Worker file](https://github.com/OneSignal/OneSignal-Website-SDK/files/7585231/OneSignal-Web-SDK-HTTPS-Integration-Files.zip) to your site ([learn more](https://documentation.onesignal.com/docs/web-push-quickstart#step-6-upload-files)).
127
+
128
+
If you haven't done so already, you will need to add the [OneSignal Service Worker file](https://github.com/OneSignal/OneSignal-Website-SDK/files/11480764/OneSignalSDK-v16-ServiceWorker.zip) to your site ([learn more](https://documentation.onesignal.com/docs/web-push-quickstart#step-6-upload-files)).
123
129
124
130
The OneSignal SDK file must be publicly accessible. You can put them in your top-level root or a subdirectory. However, if you are placing the file not on top-level root make sure to specify the path via the service worker params in the init options (see section above).
125
131
126
132
**Tip:**
127
133
Visit `https://yoursite.com/OneSignalSDKWorker.js` in the address bar to make sure the files are being served successfully.
console.log("The notification was clicked!", event);
194
+
console.log('The notification was clicked!', event);
179
195
});
180
196
```
181
197
182
198
See the [OneSignal WebSDK Reference](https://documentation.onesignal.com/v11.0/docs/web-sdk) for all available event listeners.
183
199
184
200
## Troubleshooting
201
+
185
202
### `window.OneSignal already defined as 'object'!`
203
+
186
204
You will get this error if you initialize twice. Make sure you are only initializing one time. When wrapped with `React.StrictMode`, your app might be rendering twice.
187
205
188
206
## Example App
207
+
189
208
This repo includes an `example` React application implementing OneSignal. It was created using `create-react-app`. The app uses this repository's root level directory as the `react-onesignal` package and will bundle any changes on every run.
190
209
191
210
---
@@ -200,12 +219,13 @@ Give a ⭐️ if this project helped you!
This project is [Modified MIT](https://github.com/OneSignal/react-onesignal/blob/master/LICENSE) licensed.
215
235
216
236
## Thanks
237
+
217
238
Special thanks to [pedro-lb](https://github.com/pedro-lb) and others for work on the project this package is [based on](https://github.com/pedro-lb/react-onesignal).
0 commit comments