react-onesignal 2.0.1 → 2.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +24 -0
- package/README.md +100 -42
- package/dist/index.d.ts +105 -0
- package/dist/index.es.js +1007 -1123
- package/dist/index.es.js.map +1 -1
- package/dist/index.js +1007 -1123
- package/dist/index.js.map +1 -1
- package/index.ts +1170 -0
- package/package.json +3 -4
- package/rollup.config.js +1 -2
- package/tsconfig.json +25 -0
- package/index.d.ts +0 -53
- package/index.js +0 -957
package/LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
Modified MIT License
|
|
2
|
+
|
|
3
|
+
Copyright 2022 OneSignal
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
1. The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
2. All copies of substantial portions of the Software may only be used in connection
|
|
16
|
+
with services provided by OneSignal.
|
|
17
|
+
|
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
24
|
+
THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -28,7 +28,6 @@ Version 2.0 was recently released. Read the [Migration Guide](https://github.com
|
|
|
28
28
|
|
|
29
29
|
You can use `yarn` or `npm`.
|
|
30
30
|
|
|
31
|
-
|
|
32
31
|
### Yarn
|
|
33
32
|
|
|
34
33
|
```bash
|
|
@@ -71,9 +70,27 @@ OneSignal.init({ appId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' }).then(() => {
|
|
|
71
70
|
})
|
|
72
71
|
```
|
|
73
72
|
|
|
74
|
-
### Options
|
|
73
|
+
### Init Options
|
|
75
74
|
You can pass other [options](https://documentation.onesignal.com/docs/web-push-sdk#init) to the `init` function. Use these options to configure personalized prompt options, auto-resubscribe, and more.
|
|
76
75
|
|
|
76
|
+
**Service Worker Params**
|
|
77
|
+
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).
|
|
78
|
+
|
|
79
|
+
In this distribution, you can specify the parameters via the following:
|
|
80
|
+
|
|
81
|
+
| Field | Details |
|
|
82
|
+
|----------------------------|------------------------------------------------------------------------------------------------------------------------|
|
|
83
|
+
| `serviceWorkerParam` | Use to specify the scope, or the path the service worker has control of. Example: `{ scope: "/js/push/onesignal/" }` |
|
|
84
|
+
| `serviceWorkerPath` | The path to the service worker file. |
|
|
85
|
+
|
|
86
|
+
### Service Worker File
|
|
87
|
+
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)).
|
|
88
|
+
|
|
89
|
+
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).
|
|
90
|
+
|
|
91
|
+
**Tip:**
|
|
92
|
+
Visit `https://yoursite.com/OneSignalSDKWorker.js` in the address bar to make sure the files are being served successfully.
|
|
93
|
+
|
|
77
94
|
---
|
|
78
95
|
## OneSignal API
|
|
79
96
|
### Typescript
|
|
@@ -81,43 +98,43 @@ This package includes Typescript support.
|
|
|
81
98
|
|
|
82
99
|
```ts
|
|
83
100
|
interface OneSignal {
|
|
84
|
-
init(options
|
|
85
|
-
on(event: string, listener:
|
|
86
|
-
off(event: string, listener:
|
|
87
|
-
once(event: string, listener:
|
|
88
|
-
isPushNotificationsEnabled(callback?: Action<boolean>): Promise<boolean
|
|
89
|
-
showHttpPrompt(options?: AutoPromptOptions): void
|
|
90
|
-
registerForPushNotifications(options?: RegisterOptions): Promise<void
|
|
91
|
-
setDefaultNotificationUrl(url: string): void
|
|
92
|
-
setDefaultTitle(title: string): void
|
|
93
|
-
getTags(callback?: Action<any>): void
|
|
94
|
-
sendTag(key: string,
|
|
95
|
-
sendTags(tags: TagsObject<any>,
|
|
96
|
-
deleteTag(tag: string): Promise<Array<string
|
|
97
|
-
deleteTags(tags: Array<string>,
|
|
98
|
-
addListenerForNotificationOpened(callback?: Action<Notification>): void
|
|
99
|
-
setSubscription(newSubscription: boolean): Promise<void
|
|
100
|
-
showHttpPermissionRequest(options?: AutoPromptOptions): Promise<any
|
|
101
|
-
showNativePrompt(): Promise<void
|
|
102
|
-
showSlidedownPrompt(options?: AutoPromptOptions): Promise<void
|
|
103
|
-
showCategorySlidedown(options?: AutoPromptOptions): Promise<void
|
|
104
|
-
showSmsSlidedown(options?: AutoPromptOptions): Promise<void
|
|
105
|
-
showEmailSlidedown(options?: AutoPromptOptions): Promise<void
|
|
106
|
-
showSmsAndEmailSlidedown(options?: AutoPromptOptions): Promise<void
|
|
107
|
-
getNotificationPermission(onComplete?:
|
|
108
|
-
getUserId(callback?: Action<string | undefined | null>): Promise<string | undefined | null
|
|
109
|
-
getSubscription(callback?: Action<boolean>): Promise<boolean
|
|
110
|
-
setEmail(email: string,
|
|
111
|
-
setSMSNumber(smsNumber: string,
|
|
112
|
-
logoutEmail(): void
|
|
113
|
-
logoutSMS(): void
|
|
114
|
-
setExternalUserId(externalUserId: string | undefined | null,
|
|
115
|
-
removeExternalUserId(): Promise<void
|
|
116
|
-
getExternalUserId(): Promise<string | undefined | null
|
|
117
|
-
provideUserConsent(consent: boolean): Promise<void
|
|
118
|
-
getEmailId(callback?: Action<string | undefined>): Promise<string | null | undefined
|
|
119
|
-
getSMSId(callback?: Action<string | undefined>): Promise<string | null | undefined
|
|
120
|
-
sendOutcome(outcomeName: string,
|
|
101
|
+
init(options: IInitObject): Promise<void>;
|
|
102
|
+
on(event: string, listener: () => void): void;
|
|
103
|
+
off(event: string, listener: () => void): void;
|
|
104
|
+
once(event: string, listener: () => void): void;
|
|
105
|
+
isPushNotificationsEnabled(callback?: Action<boolean>): Promise<boolean>;
|
|
106
|
+
showHttpPrompt(options?: AutoPromptOptions): Promise<void>;
|
|
107
|
+
registerForPushNotifications(options?: RegisterOptions): Promise<void>;
|
|
108
|
+
setDefaultNotificationUrl(url: string): Promise<void>;
|
|
109
|
+
setDefaultTitle(title: string): Promise<void>;
|
|
110
|
+
getTags(callback?: Action<any>): Promise<void>;
|
|
111
|
+
sendTag(key: string, value: any, callback?: Action<Object>): Promise<Object | null>;
|
|
112
|
+
sendTags(tags: TagsObject<any>, callback?: Action<Object>): Promise<Object | null>;
|
|
113
|
+
deleteTag(tag: string): Promise<Array<string>>;
|
|
114
|
+
deleteTags(tags: Array<string>, callback?: Action<Array<string>>): Promise<Array<string>>;
|
|
115
|
+
addListenerForNotificationOpened(callback?: Action<Notification>): Promise<void>;
|
|
116
|
+
setSubscription(newSubscription: boolean): Promise<void>;
|
|
117
|
+
showHttpPermissionRequest(options?: AutoPromptOptions): Promise<any>;
|
|
118
|
+
showNativePrompt(): Promise<void>;
|
|
119
|
+
showSlidedownPrompt(options?: AutoPromptOptions): Promise<void>;
|
|
120
|
+
showCategorySlidedown(options?: AutoPromptOptions): Promise<void>;
|
|
121
|
+
showSmsSlidedown(options?: AutoPromptOptions): Promise<void>;
|
|
122
|
+
showEmailSlidedown(options?: AutoPromptOptions): Promise<void>;
|
|
123
|
+
showSmsAndEmailSlidedown(options?: AutoPromptOptions): Promise<void>;
|
|
124
|
+
getNotificationPermission(onComplete?: Action<NotificationPermission>): Promise<NotificationPermission>;
|
|
125
|
+
getUserId(callback?: Action<string | undefined | null>): Promise<string | undefined | null>;
|
|
126
|
+
getSubscription(callback?: Action<boolean>): Promise<boolean>;
|
|
127
|
+
setEmail(email: string, options?: SetEmailOptions): Promise<string | null>;
|
|
128
|
+
setSMSNumber(smsNumber: string, options?: SetSMSOptions): Promise<string | null>;
|
|
129
|
+
logoutEmail(): Promise<void>;
|
|
130
|
+
logoutSMS(): Promise<void>;
|
|
131
|
+
setExternalUserId(externalUserId: string | undefined | null, authHash?: string): Promise<void>;
|
|
132
|
+
removeExternalUserId(): Promise<void>;
|
|
133
|
+
getExternalUserId(): Promise<string | undefined | null>;
|
|
134
|
+
provideUserConsent(consent: boolean): Promise<void>;
|
|
135
|
+
getEmailId(callback?: Action<string | undefined>): Promise<string | null | undefined>;
|
|
136
|
+
getSMSId(callback?: Action<string | undefined>): Promise<string | null | undefined>;
|
|
137
|
+
sendOutcome(outcomeName: string, outcomeWeight?: number | undefined): Promise<void>;
|
|
121
138
|
}
|
|
122
139
|
```
|
|
123
140
|
|
|
@@ -127,10 +144,18 @@ See the official [OneSignal WebSDK reference](https://documentation.onesignal.co
|
|
|
127
144
|
---
|
|
128
145
|
## Advanced Usage
|
|
129
146
|
### Events and Event Listeners
|
|
130
|
-
|
|
147
|
+
Use listeners to react to OneSignal-related events:
|
|
148
|
+
|
|
149
|
+
* `subscriptionChange`
|
|
150
|
+
* `permissionPromptDisplay`
|
|
151
|
+
* `notificationPermissionChange`
|
|
152
|
+
* `popoverShown`
|
|
153
|
+
* `customPromptClick`
|
|
154
|
+
* `notificationDisplay`
|
|
155
|
+
* `notificationDismiss`
|
|
131
156
|
|
|
132
157
|
**Example**
|
|
133
|
-
```
|
|
158
|
+
```js
|
|
134
159
|
OneSignal.on('subscriptionChange', function(isSubscribed) {
|
|
135
160
|
console.log("The user's subscription state is now:", isSubscribed);
|
|
136
161
|
});
|
|
@@ -138,9 +163,42 @@ OneSignal.on('subscriptionChange', function(isSubscribed) {
|
|
|
138
163
|
|
|
139
164
|
See the [OneSignal WebSDK Reference](https://documentation.onesignal.com/docs/web-push-sdk) for all available event listeners.
|
|
140
165
|
|
|
166
|
+
## Troubleshooting
|
|
167
|
+
### `window.OneSignal already defined as 'object'!`
|
|
168
|
+
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.
|
|
169
|
+
|
|
170
|
+
## Example App
|
|
171
|
+
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.
|
|
172
|
+
|
|
141
173
|
---
|
|
174
|
+
|
|
175
|
+
## 🤝 Contributing
|
|
176
|
+
|
|
177
|
+
Contributions, issues and feature requests are welcome!<br />Feel free to check [issues page](https://github.com/OneSignal/react-onesignal/issues).
|
|
178
|
+
|
|
179
|
+
## Show your support
|
|
180
|
+
|
|
181
|
+
Give a ⭐️ if this project helped you!
|
|
182
|
+
|
|
183
|
+
## OneSignal
|
|
184
|
+
|
|
185
|
+
* [Website](https://onesignal.com)
|
|
186
|
+
* Twitter: [@onesignal](https://twitter.com/onesignal)
|
|
187
|
+
* Github: [@OneSignal](https://github.com/OneSignal)
|
|
188
|
+
* LinkedIn: [@onesignal](https://linkedin.com/company/onesignal)
|
|
189
|
+
|
|
190
|
+
## Discord
|
|
191
|
+
Reach out to us via our [Discord server](https://discord.com/invite/EP7gf6Uz7G)!
|
|
192
|
+
|
|
193
|
+
## 📝 License
|
|
194
|
+
|
|
195
|
+
Copyright © 2022 [OneSignal](https://github.com/OneSignal).<br />
|
|
196
|
+
This project is [Modified MIT](https://github.com/OneSignal/react-onesignal/blob/master/LICENSE) licensed.
|
|
197
|
+
|
|
142
198
|
## Thanks
|
|
143
199
|
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).
|
|
144
200
|
<a href="https://github.com/onesignal/react-onesignal/graphs/contributors">
|
|
145
201
|
<img src="https://user-images.githubusercontent.com/11739227/119415383-1d354700-bcb7-11eb-946d-01c40cd07010.png" />
|
|
146
|
-
</a>
|
|
202
|
+
</a>
|
|
203
|
+
|
|
204
|
+
Enjoy!
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
declare type Action<T> = (item: T) => void;
|
|
2
|
+
interface AutoPromptOptions {
|
|
3
|
+
force?: boolean;
|
|
4
|
+
forceSlidedownOverNative?: boolean;
|
|
5
|
+
slidedownPromptOptions?: IOneSignalAutoPromptOptions;
|
|
6
|
+
}
|
|
7
|
+
interface RegisterOptions {
|
|
8
|
+
modalPrompt?: boolean;
|
|
9
|
+
httpPermissionRequest?: boolean;
|
|
10
|
+
slidedown?: boolean;
|
|
11
|
+
autoAccept?: boolean;
|
|
12
|
+
}
|
|
13
|
+
interface SetSMSOptions {
|
|
14
|
+
identifierAuthHash?: string;
|
|
15
|
+
}
|
|
16
|
+
interface SetEmailOptions {
|
|
17
|
+
identifierAuthHash?: string;
|
|
18
|
+
emailAuthHash?: string;
|
|
19
|
+
}
|
|
20
|
+
interface TagsObject<T> {
|
|
21
|
+
[key: string]: T;
|
|
22
|
+
}
|
|
23
|
+
interface IOneSignalAutoPromptOptions {
|
|
24
|
+
force?: boolean;
|
|
25
|
+
forceSlidedownOverNative?: boolean;
|
|
26
|
+
isInUpdateMode?: boolean;
|
|
27
|
+
categoryOptions?: IOneSignalCategories;
|
|
28
|
+
}
|
|
29
|
+
interface IOneSignalCategories {
|
|
30
|
+
positiveUpdateButton: string;
|
|
31
|
+
negativeUpdateButton: string;
|
|
32
|
+
savingButtonText: string;
|
|
33
|
+
errorButtonText: string;
|
|
34
|
+
updateMessage: string;
|
|
35
|
+
tags: IOneSignalTagCategory[];
|
|
36
|
+
}
|
|
37
|
+
interface IOneSignalTagCategory {
|
|
38
|
+
tag: string;
|
|
39
|
+
label: string;
|
|
40
|
+
checked?: boolean;
|
|
41
|
+
}
|
|
42
|
+
interface IInitObject {
|
|
43
|
+
appId: string;
|
|
44
|
+
subdomainName?: string;
|
|
45
|
+
requiresUserPrivacyConsent?: boolean;
|
|
46
|
+
promptOptions?: object;
|
|
47
|
+
welcomeNotification?: object;
|
|
48
|
+
notifyButton?: object;
|
|
49
|
+
persistNotification?: boolean;
|
|
50
|
+
webhooks?: object;
|
|
51
|
+
autoResubscribe?: boolean;
|
|
52
|
+
autoRegister?: boolean;
|
|
53
|
+
notificationClickHandlerMatch?: string;
|
|
54
|
+
notificationClickHandlerAction?: string;
|
|
55
|
+
serviceWorkerParam?: {
|
|
56
|
+
scope: string;
|
|
57
|
+
};
|
|
58
|
+
serviceWorkerPath?: string;
|
|
59
|
+
serviceWorkerUpdaterPath?: string;
|
|
60
|
+
path?: string;
|
|
61
|
+
allowLocalhostAsSecureOrigin?: boolean;
|
|
62
|
+
[key: string]: any;
|
|
63
|
+
}
|
|
64
|
+
interface IOneSignal {
|
|
65
|
+
init(options: IInitObject): Promise<void>;
|
|
66
|
+
on(event: string, listener: (eventData?: any) => void): void;
|
|
67
|
+
off(event: string, listener: (eventData?: any) => void): void;
|
|
68
|
+
once(event: string, listener: (eventData?: any) => void): void;
|
|
69
|
+
isPushNotificationsEnabled(callback?: Action<boolean>): Promise<boolean>;
|
|
70
|
+
showHttpPrompt(options?: AutoPromptOptions): Promise<void>;
|
|
71
|
+
registerForPushNotifications(options?: RegisterOptions): Promise<void>;
|
|
72
|
+
setDefaultNotificationUrl(url: string): Promise<void>;
|
|
73
|
+
setDefaultTitle(title: string): Promise<void>;
|
|
74
|
+
getTags(callback?: Action<any>): Promise<void>;
|
|
75
|
+
sendTag(key: string, value: any, callback?: Action<Object>): Promise<Object | null>;
|
|
76
|
+
sendTags(tags: TagsObject<any>, callback?: Action<Object>): Promise<Object | null>;
|
|
77
|
+
deleteTag(tag: string): Promise<Array<string>>;
|
|
78
|
+
deleteTags(tags: Array<string>, callback?: Action<Array<string>>): Promise<Array<string>>;
|
|
79
|
+
addListenerForNotificationOpened(callback?: Action<Notification>): Promise<void>;
|
|
80
|
+
setSubscription(newSubscription: boolean): Promise<void>;
|
|
81
|
+
showHttpPermissionRequest(options?: AutoPromptOptions): Promise<any>;
|
|
82
|
+
showNativePrompt(): Promise<void>;
|
|
83
|
+
showSlidedownPrompt(options?: AutoPromptOptions): Promise<void>;
|
|
84
|
+
showCategorySlidedown(options?: AutoPromptOptions): Promise<void>;
|
|
85
|
+
showSmsSlidedown(options?: AutoPromptOptions): Promise<void>;
|
|
86
|
+
showEmailSlidedown(options?: AutoPromptOptions): Promise<void>;
|
|
87
|
+
showSmsAndEmailSlidedown(options?: AutoPromptOptions): Promise<void>;
|
|
88
|
+
getNotificationPermission(onComplete?: Action<NotificationPermission>): Promise<NotificationPermission>;
|
|
89
|
+
getUserId(callback?: Action<string | undefined | null>): Promise<string | undefined | null>;
|
|
90
|
+
getSubscription(callback?: Action<boolean>): Promise<boolean>;
|
|
91
|
+
setEmail(email: string, options?: SetEmailOptions): Promise<string | null>;
|
|
92
|
+
setSMSNumber(smsNumber: string, options?: SetSMSOptions): Promise<string | null>;
|
|
93
|
+
logoutEmail(): Promise<void>;
|
|
94
|
+
logoutSMS(): Promise<void>;
|
|
95
|
+
setExternalUserId(externalUserId: string | undefined | null, authHash?: string): Promise<void>;
|
|
96
|
+
removeExternalUserId(): Promise<void>;
|
|
97
|
+
getExternalUserId(): Promise<string | undefined | null>;
|
|
98
|
+
provideUserConsent(consent: boolean): Promise<void>;
|
|
99
|
+
getEmailId(callback?: Action<string | undefined>): Promise<string | null | undefined>;
|
|
100
|
+
getSMSId(callback?: Action<string | undefined>): Promise<string | null | undefined>;
|
|
101
|
+
sendOutcome(outcomeName: string, outcomeWeight?: number | undefined): Promise<void>;
|
|
102
|
+
[index: string]: Function;
|
|
103
|
+
}
|
|
104
|
+
declare const OneSignalReact: IOneSignal;
|
|
105
|
+
export default OneSignalReact;
|