Skip to content

Commit 9aeca3e

Browse files
authored
Merge pull request #160 from OneSignal/cd_update
Generator Update
2 parents 79cb865 + 2d7c979 commit 9aeca3e

File tree

15 files changed

+7861
-96
lines changed

15 files changed

+7861
-96
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
node-version: 'lts/*'
2525
registry-url: 'https://registry.npmjs.org'
2626
- name: Install dependencies
27-
run: yarn install --frozen-lockfile
27+
run: npm ci
2828
- name: Release
2929
env:
3030
GITHUB_TOKEN: ${{ secrets.GH_WEB_SHIM_PUSH_TOKEN }}

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,4 @@ dist
1616
.env.test.local
1717
.env.production.local
1818

19-
npm-debug.log*
20-
yarn-debug.log*
21-
yarn-error.log*
19+
npm-debug.log*

.npmignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,4 @@ build
1717
.babelrc;
1818

1919
npm-debug.log*
20-
yarn-debug.log*
21-
yarn-error.log*
22-
2320
example

README.md

Lines changed: 82 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,42 @@
1616

1717
> 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).
1818
19-
* 🏠 [Homepage](https://github.com/OneSignal/react-onesignal#readme)
20-
* 🖤 [npm](https://www.npmjs.com/package/react-onesignal)
19+
- 🏠 [Homepage](https://github.com/OneSignal/react-onesignal#readme)
20+
- 🖤 [npm](https://www.npmjs.com/package/react-onesignal)
2121

2222
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.
2323

2424
You can find more information on OneSignal [here](https://onesignal.com/).
2525

2626
### Migration Guides
27+
2728
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.
2829

2930
## Contents
31+
3032
- [Install](#install)
3133
- [Usage](#usage)
3234
- [API](#onesignal-api)
3335
- [Advanced Usage](#advanced-usage)
3436

3537
---
36-
## Install
3738

38-
You can use `yarn` or `npm`.
39+
## Install
3940

40-
### Yarn
41+
### npm
4142

4243
```bash
43-
yarn add react-onesignal
44+
npm install --save react-onesignal
4445
```
4546

46-
### npm
47+
### yarn
4748

4849
```bash
49-
npm install --save react-onesignal
50+
yarn add react-onesignal
5051
```
5152

5253
---
54+
5355
## Usage
5456

5557
Initialize OneSignal with your `appId` via the `options` parameter:
@@ -63,10 +65,12 @@ OneSignal.init({ appId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' });
6365
The `init` function returns a promise that resolves when OneSignal is loaded.
6466

6567
**Examples**
68+
6669
```js
6770
await OneSignal.init({ appId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' });
6871
// do other stuff
6972
```
73+
7074
---
7175

7276
```js
@@ -75,117 +79,132 @@ OneSignal.init({ appId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' }).then(() => {
7579
setInitialized(true);
7680
OneSignal.Slidedown.promptPush();
7781
// do other stuff
78-
})
82+
});
7983
```
8084

8185
### Init Options
86+
8287
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.
8388

8489
<details>
8590
<summary>Expand to see more options</summary>
8691

87-
| Property Name | Type | Description |
88-
| ---------------------------| --------------------- | -------------------------------------------------- |
89-
| `appId` | `string` | The ID of your OneSignal app. |
90-
| `autoRegister` | `boolean` (optional) | Whether or not to automatically register the user. |
91-
| `autoResubscribe` | `boolean` (optional) | Whether or not to automatically resubscribe the user. |
92-
| `path` | `string` (optional) | The path to the OneSignal service worker file. |
93-
| `serviceWorkerPath` | `string` (optional) | The path to the OneSignal service worker script. |
94-
| `serviceWorkerUpdaterPath` | `string` (optional) | The path to the OneSignal service worker updater script. |
95-
| `subdomainName` | `string` (optional) | The subdomain of your OneSignal app. |
96-
| `allowLocalhostAsSecureOrigin` | `boolean` (optional) | Whether or not to allow localhost as a secure origin. |
97-
| `requiresUserPrivacyConsent`| `boolean` (optional) | Whether or not the user's consent is required. |
98-
| `persistNotification` | `boolean` (optional) | Whether or not notifications should persist. |
99-
| `notificationClickHandlerMatch`| `string` (optional) | The URL match pattern for notification clicks. |
100-
| `notificationClickHandlerAction`| `string` (optional)| The action to perform when a notification is clicked. |
101-
| `welcomeNotification` | `object` (optional) | The welcome notification configuration. |
102-
| `notifyButton` | `object` (optional) | The notify button configuration. |
103-
| `promptOptions` | `object` (optional) | Additional options for the subscription prompt. |
104-
| `webhooks` | `object` (optional) | The webhook configuration. |
105-
| `[key: string]` | `any` | Additional properties can be added as needed. |
92+
| Property Name | Type | Description |
93+
| -------------------------------- | -------------------- | -------------------------------------------------------- |
94+
| `appId` | `string` | The ID of your OneSignal app. |
95+
| `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. |
106111

107112
**Service Worker Params**
108113
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).
109114

110115
In this distribution, you can specify the parameters via the following:
111116

112-
| Field | Details |
113-
|----------------------------|------------------------------------------------------------------------------------------------------------------------|
114-
| `serviceWorkerParam` | Use to specify the scope, or the path the service worker has control of. Example: `{ scope: "/js/push/onesignal/" }` |
115-
| `serviceWorkerPath` | The path to the service worker file. |
117+
| Field | Details |
118+
| -------------------- | -------------------------------------------------------------------------------------------------------------------- |
119+
| `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. |
116121

117122
</details>
118123

119124
---
120125

121126
### 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)).
123129

124130
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).
125131

126132
**Tip:**
127133
Visit `https://yoursite.com/OneSignalSDKWorker.js` in the address bar to make sure the files are being served successfully.
128134

129135
---
136+
130137
### Typescript
138+
131139
This package includes Typescript support.
132140

133141
```ts
134142
interface IOneSignalOneSignal {
135-
Slidedown: IOneSignalSlidedown;
136-
Notifications: IOneSignalNotifications;
137-
Session: IOneSignalSession;
138-
User: IOneSignalUser;
139-
Debug: IOneSignalDebug;
140-
login(externalId: string, jwtToken?: string): Promise<void>;
141-
logout(): Promise<void>;
142-
init(options: IInitObject): Promise<void>;
143-
setConsentGiven(consent: boolean): Promise<void>;
144-
setConsentRequired(requiresConsent: boolean): Promise<void>;
143+
Slidedown: IOneSignalSlidedown;
144+
Notifications: IOneSignalNotifications;
145+
Session: IOneSignalSession;
146+
User: IOneSignalUser;
147+
Debug: IOneSignalDebug;
148+
login(externalId: string, jwtToken?: string): Promise<void>;
149+
logout(): Promise<void>;
150+
init(options: IInitObject): Promise<void>;
151+
setConsentGiven(consent: boolean): Promise<void>;
152+
setConsentRequired(requiresConsent: boolean): Promise<void>;
145153
}
146154
```
147155

148156
### OneSignal API
157+
149158
See the official [OneSignal WebSDK reference](https://documentation.onesignal.com/v11.0/docs/web-sdk) for information on all available SDK functions.
150159

151160
---
161+
152162
## Advanced Usage
163+
153164
### Events and Event Listeners
165+
154166
Use listeners to react to OneSignal-related events:
155167

156168
### Notifications Namespace
157-
| Event Name | Callback Argument Type |
158-
|-|-|
159-
|'click' | NotificationClickEvent|
160-
|'foregroundWillDisplay'| NotificationForegroundWillDisplayEvent
161-
| 'dismiss'| NotificationDismissEvent|
162-
|'permissionChange'| boolean|
163-
|'permissionPromptDisplay'| void|
169+
170+
| Event Name | Callback Argument Type |
171+
| ------------------------- | -------------------------------------- |
172+
| 'click' | NotificationClickEvent |
173+
| 'foregroundWillDisplay' | NotificationForegroundWillDisplayEvent |
174+
| 'dismiss' | NotificationDismissEvent |
175+
| 'permissionChange' | boolean |
176+
| 'permissionPromptDisplay' | void |
164177

165178
### Slidedown Namespace
166-
| Event Name | Callback Argument Type |
167-
|-|-|
168-
|'slidedownShown' | boolean |
179+
180+
| Event Name | Callback Argument Type |
181+
| ---------------- | ---------------------- |
182+
| 'slidedownShown' | boolean |
169183

170184
### Push Subscription Namespace
185+
171186
| Event Name | Callback Argument Type |
172-
|-|-|
173-
|'change' | boolean |
187+
| ---------- | ---------------------- |
188+
| 'change' | boolean |
174189

175190
**Example**
191+
176192
```js
177193
OneSignal.Notifications.addEventListener('click', (event) => {
178-
console.log("The notification was clicked!", event);
194+
console.log('The notification was clicked!', event);
179195
});
180196
```
181197

182198
See the [OneSignal WebSDK Reference](https://documentation.onesignal.com/v11.0/docs/web-sdk) for all available event listeners.
183199

184200
## Troubleshooting
201+
185202
### `window.OneSignal already defined as 'object'!`
203+
186204
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.
187205

188206
## Example App
207+
189208
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.
190209

191210
---
@@ -200,12 +219,13 @@ Give a ⭐️ if this project helped you!
200219

201220
## OneSignal
202221

203-
* [Website](https://onesignal.com)
204-
* Twitter: [@onesignal](https://twitter.com/onesignal)
205-
* Github: [@OneSignal](https://github.com/OneSignal)
206-
* LinkedIn: [@onesignal](https://linkedin.com/company/onesignal)
222+
- [Website](https://onesignal.com)
223+
- Twitter: [@onesignal](https://twitter.com/onesignal)
224+
- Github: [@OneSignal](https://github.com/OneSignal)
225+
- LinkedIn: [@onesignal](https://linkedin.com/company/onesignal)
207226

208227
## Discord
228+
209229
Reach out to us via our [Discord server](https://discord.com/invite/EP7gf6Uz7G)!
210230

211231
## 📝 License
@@ -214,9 +234,10 @@ Copyright © 2023 [OneSignal](https://github.com/OneSignal).<br />
214234
This project is [Modified MIT](https://github.com/OneSignal/react-onesignal/blob/master/LICENSE) licensed.
215235

216236
## Thanks
237+
217238
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).
218239
<a href="https://github.com/onesignal/react-onesignal/graphs/contributors">
219-
<img src="https://user-images.githubusercontent.com/11739227/119415383-1d354700-bcb7-11eb-946d-01c40cd07010.png" />
240+
<img src="https://user-images.githubusercontent.com/11739227/119415383-1d354700-bcb7-11eb-946d-01c40cd07010.png" />
220241
</a>
221242

222243
Enjoy!

example/.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
.env.production.local
2020

2121
npm-debug.log*
22-
yarn-debug.log*
23-
yarn-error.log*
2422

2523
dev-ssl.crt
2624
dev-ssl.key

example/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Getting Started with Create React App
1+
# Setup
22

3-
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
3+
Run `npm install` then replace the string `<YOUR_APP_ID>` with your actual app id from the OneSignal dashboard project.
44

55
## Available Scripts
66

example/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
"web-vitals": "^2.1.4"
2020
},
2121
"scripts": {
22-
"start": "yarn run prepare && react-scripts start",
23-
"prepare": "cd .. && yarn build",
22+
"start": "npm run prepare && react-scripts start",
23+
"prepare": "cd .. && npm run build",
2424
"build": "react-scripts build",
2525
"test": "react-scripts test",
2626
"eject": "react-scripts eject"

example/public/OneSignalSDKWorker.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
importScripts("https://onesignal.com/sdks/web/v16/OneSignalSDK.sw.js");

example/public/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
You can add webfonts, meta tags, or analytics to this file.
3737
The build step will place the bundled scripts into the <body> tag.
3838
39-
To begin the development, run `npm start` or `yarn start`.
40-
To create a production bundle, use `npm run build` or `yarn build`.
39+
To begin the development, run `npm start`
40+
To create a production bundle, use `npm run build`
4141
-->
4242
</body>
4343
</html>

example/src/App.tsx

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
import logo from './logo.svg';
2-
import './App.css';
3-
import OneSignal from 'react-onesignal';
1+
import { useEffect } from "react";
2+
import OneSignal from "react-onesignal";
3+
import "./App.css";
4+
import logo from "./logo.svg";
45

56
function App() {
6-
try {
7+
useEffect(() => {
78
OneSignal.init({
8-
appId: "f5d104e9-7462-4bf7-adc8-4ca501cb55b2",
9+
appId: "<YOUR_APP_ID>",
910
}).then(() => {
10-
OneSignal.Debug.setLogLevel('trace');
11-
OneSignal.login("rodrigo");
12-
OneSignal.User.addAlias("myAlias", "1");
11+
OneSignal.Debug.setLogLevel("trace");
1312
});
14-
} catch (e) {
15-
console.log(e);
16-
}
13+
}, []);
1714

1815
return (
1916
<div className="App">

0 commit comments

Comments
 (0)