react-onesignal 2.0.0-beta1 → 2.0.3
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/MigrationGuide.md +78 -0
- package/README.md +39 -88
- 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 +6 -5
- package/rollup.config.js +1 -2
- package/tsconfig.json +25 -0
- package/index.d.ts +0 -53
- package/index.js +0 -957
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
|
|
2
|
+
# Migration Guide
|
|
3
|
+
Review the updated documentation in the [README](https://github.com/OneSignal/react-onesignal/blob/master/README.md).
|
|
4
|
+
|
|
5
|
+
## Version 2
|
|
6
|
+
Version 2.0 includes breaking changes. Make sure to follow this migration guide carefully, review the updated documentation, and test your application thoroughly.
|
|
7
|
+
|
|
8
|
+
## Key Changes
|
|
9
|
+
- Update your OneSignal initialization function (see below)
|
|
10
|
+
- Remove the initialization hook (now handled for you automagically)
|
|
11
|
+
- Event listeners are now set up using the `on` function
|
|
12
|
+
- New functions supporting newer OneSignal features
|
|
13
|
+
|
|
14
|
+
### OneSignal Initialization
|
|
15
|
+
The initialization function has been changed to match 1:1 with the underlying OneSignal WebSDK. The function has been renamed `init`. The `appId` is now set via the single config options argument passed to the `init` function:
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
import OneSignal from 'react-onesignal';
|
|
19
|
+
|
|
20
|
+
OneSignal.init({ appId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' });
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Furthermore, there is no longer a need to wrap the initialization in the `useOneSignalSetup` hook. The order of functions relative to the OneSignal initialization no longer matters (although we still recommend initializing OneSignal in a top-level component).
|
|
24
|
+
|
|
25
|
+
### Event Listeners
|
|
26
|
+
Event listeners can now be added directly via the `on` function. This function takes an event string and a callback.
|
|
27
|
+
|
|
28
|
+
### New Features
|
|
29
|
+
We have added new functions to support the latest OneSignal functions and features.
|
|
30
|
+
|
|
31
|
+
### Typings
|
|
32
|
+
Typings are included in the package automatically. See below for a full list of the functions included in this package:
|
|
33
|
+
```ts
|
|
34
|
+
interface OneSignal {
|
|
35
|
+
init(options?: any): Promise<void>
|
|
36
|
+
on(event: string, listener: Function): void
|
|
37
|
+
off(event: string, listener: Function): void
|
|
38
|
+
once(event: string, listener: Function): void
|
|
39
|
+
isPushNotificationsEnabled(callback?: Action<boolean>): Promise<boolean>
|
|
40
|
+
showHttpPrompt(options?: AutoPromptOptions): void
|
|
41
|
+
registerForPushNotifications(options?: RegisterOptions): Promise<void>
|
|
42
|
+
setDefaultNotificationUrl(url: string): void
|
|
43
|
+
setDefaultTitle(title: string): void
|
|
44
|
+
getTags(callback?: Action<any>): void
|
|
45
|
+
sendTag(key: string, value: any, callback?: Action<Object>): Promise<Object | null>
|
|
46
|
+
sendTags(tags: TagsObject<any>, callback?: Action<Object>): Promise<Object | null>
|
|
47
|
+
deleteTag(tag: string): Promise<Array<string>>
|
|
48
|
+
deleteTags(tags: Array<string>, callback?: Action<Array<string>>): Promise<Array<string>>
|
|
49
|
+
addListenerForNotificationOpened(callback?: Action<Notification>): void
|
|
50
|
+
setSubscription(newSubscription: boolean): Promise<void>
|
|
51
|
+
showHttpPermissionRequest(options?: AutoPromptOptions): Promise<any>
|
|
52
|
+
showNativePrompt(): Promise<void>
|
|
53
|
+
showSlidedownPrompt(options?: AutoPromptOptions): Promise<void>
|
|
54
|
+
showCategorySlidedown(options?: AutoPromptOptions): Promise<void>
|
|
55
|
+
showSmsSlidedown(options?: AutoPromptOptions): Promise<void>
|
|
56
|
+
showEmailSlidedown(options?: AutoPromptOptions): Promise<void>
|
|
57
|
+
showSmsAndEmailSlidedown(options?: AutoPromptOptions): Promise<void>
|
|
58
|
+
getNotificationPermission(onComplete?: Function): Promise<NotificationPermission>
|
|
59
|
+
getUserId(callback?: Action<string | undefined | null>): Promise<string | undefined | null>
|
|
60
|
+
getSubscription(callback?: Action<boolean>): Promise<boolean>
|
|
61
|
+
setEmail(email: string, options?: SetEmailOptions): Promise<string|null>
|
|
62
|
+
setSMSNumber(smsNumber: string, options?: SetSMSOptions): Promise<string | null>
|
|
63
|
+
logoutEmail(): void
|
|
64
|
+
logoutSMS(): void
|
|
65
|
+
setExternalUserId(externalUserId: string | undefined | null, authHash?: string): Promise<void>
|
|
66
|
+
removeExternalUserId(): Promise<void>
|
|
67
|
+
getExternalUserId(): Promise<string | undefined | null>
|
|
68
|
+
provideUserConsent(consent: boolean): Promise<void>
|
|
69
|
+
getEmailId(callback?: Action<string | undefined>): Promise<string | null | undefined>
|
|
70
|
+
getSMSId(callback?: Action<string | undefined>): Promise<string | null | undefined>
|
|
71
|
+
sendOutcome(outcomeName: string, outcomeWeight?: number | undefined): Promise<void>
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Summary
|
|
76
|
+
Consider the above changes when migrating to version 2 and make sure to thoroughly test your application.
|
|
77
|
+
|
|
78
|
+
Enjoy!
|
package/README.md
CHANGED
|
@@ -14,6 +14,9 @@ OneSignal is the world's leader for Mobile Push Notifications, Web Push, and In-
|
|
|
14
14
|
|
|
15
15
|
You can find more information on OneSignal [here](https://onesignal.com/).
|
|
16
16
|
|
|
17
|
+
### Migration Guide
|
|
18
|
+
Version 2.0 was recently released. Read the [Migration Guide](https://github.com/OneSignal/react-onesignal/blob/master/MigrationGuide.md) here if you're coming from a version 1 release of the SDK.
|
|
19
|
+
|
|
17
20
|
## Contents
|
|
18
21
|
- [Install](#install)
|
|
19
22
|
- [Usage](#usage)
|
|
@@ -41,26 +44,26 @@ npm install --save react-onesignal
|
|
|
41
44
|
---
|
|
42
45
|
## Usage
|
|
43
46
|
|
|
44
|
-
Initialize OneSignal with your
|
|
47
|
+
Initialize OneSignal with your `appId` via the `options` parameter:
|
|
45
48
|
|
|
46
49
|
```js
|
|
47
50
|
import OneSignal from 'react-onesignal';
|
|
48
51
|
|
|
49
|
-
OneSignal.
|
|
52
|
+
OneSignal.init({ appId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' });
|
|
50
53
|
```
|
|
51
54
|
|
|
52
|
-
The `
|
|
55
|
+
The `init` function returns a promise that resolves when OneSignal is loaded.
|
|
53
56
|
|
|
54
57
|
**Examples**
|
|
55
58
|
```js
|
|
56
|
-
await OneSignal.
|
|
59
|
+
await OneSignal.init({ appId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' });
|
|
57
60
|
// do other stuff
|
|
58
61
|
```
|
|
59
62
|
---
|
|
60
63
|
|
|
61
64
|
```js
|
|
62
65
|
const [initialized, setInitialized] = useState(false);
|
|
63
|
-
OneSignal.
|
|
66
|
+
OneSignal.init({ appId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' }).then(() => {
|
|
64
67
|
setInitialized(true);
|
|
65
68
|
OneSignal.showSlidedownPrompt().then(() => {
|
|
66
69
|
// do other stuff
|
|
@@ -68,51 +71,38 @@ OneSignal.initialize('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx').then(() => {
|
|
|
68
71
|
})
|
|
69
72
|
```
|
|
70
73
|
|
|
71
|
-
### Options
|
|
72
|
-
You can pass
|
|
74
|
+
### Init Options
|
|
75
|
+
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.
|
|
73
76
|
|
|
74
|
-
|
|
77
|
+
**Service Worker Params**
|
|
78
|
+
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).
|
|
75
79
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
position?: 'bottom-left' | 'bottom-right';
|
|
91
|
-
showCredit?: boolean;
|
|
92
|
-
prenotify?: boolean;
|
|
93
|
-
theme?: 'default' | 'inverse';
|
|
94
|
-
offset?: {
|
|
95
|
-
bottom?: string;
|
|
96
|
-
right?: string;
|
|
97
|
-
left?: string;
|
|
98
|
-
},
|
|
99
|
-
text?: {
|
|
100
|
-
[key: string]: string;
|
|
101
|
-
};
|
|
102
|
-
colors?: {
|
|
103
|
-
[key: string]: string;
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
```
|
|
80
|
+
In this distribution, you can specify the parameters via the following:
|
|
81
|
+
|
|
82
|
+
| Field | Details |
|
|
83
|
+
|----------------------------|------------------------------------------------------------------------------------------------------------------------|
|
|
84
|
+
| `serviceWorkerParam` | Use to specify the scope, or the path the service worker has control of. Example: `{ scope: "/js/push/onesignal/" }` |
|
|
85
|
+
| `serviceWorkerPath` | The path to the service worker file. |
|
|
86
|
+
|
|
87
|
+
### Service Worker File
|
|
88
|
+
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)).
|
|
89
|
+
|
|
90
|
+
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).
|
|
91
|
+
|
|
92
|
+
**Tip:**
|
|
93
|
+
Visit `https://yoursite.com/OneSignalSDKWorker.js` in the address bar to make sure the files are being served successfully.
|
|
108
94
|
|
|
109
95
|
---
|
|
110
96
|
## OneSignal API
|
|
111
|
-
|
|
97
|
+
### Typescript
|
|
98
|
+
This package includes Typescript support.
|
|
112
99
|
|
|
113
100
|
```ts
|
|
114
101
|
interface OneSignal {
|
|
115
|
-
|
|
102
|
+
init(options?: any): Promise<void>
|
|
103
|
+
on(event: string, listener: Function): void
|
|
104
|
+
off(event: string, listener: Function): void
|
|
105
|
+
once(event: string, listener: Function): void
|
|
116
106
|
isPushNotificationsEnabled(callback?: Action<boolean>): Promise<boolean>
|
|
117
107
|
showHttpPrompt(options?: AutoPromptOptions): void
|
|
118
108
|
registerForPushNotifications(options?: RegisterOptions): Promise<void>
|
|
@@ -152,58 +142,19 @@ interface OneSignal {
|
|
|
152
142
|
### OneSignal API
|
|
153
143
|
See the official [OneSignal WebSDK reference](https://documentation.onesignal.com/docs/web-push-sdk) for information on all available SDK functions.
|
|
154
144
|
|
|
155
|
-
### Typescript
|
|
156
|
-
This package includes Typescript support.
|
|
157
|
-
|
|
158
145
|
---
|
|
159
146
|
## Advanced Usage
|
|
160
147
|
### Events and Event Listeners
|
|
161
148
|
You can also listen for native OneSignal events like `subscriptionChange`.
|
|
162
149
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
Each object in the array should contain:
|
|
166
|
-
* `listener` -- (optional) Default value: `'on'`.
|
|
167
|
-
Some events can be listened for via multiple listeners (e.g. `.on()`, `.once()`).
|
|
168
|
-
[Check the docs](https://documentation.onesignal.com/docs/web-push-sdk) to see which listeners listen for your event.
|
|
169
|
-
Example: `'on'` | `'once'`
|
|
170
|
-
|
|
171
|
-
* `event` -- Name of the event being listened for.
|
|
172
|
-
Example: `'subscriptionChange'`
|
|
173
|
-
|
|
174
|
-
* `callback` -- Callback function for event.
|
|
175
|
-
Example: `(value) => { console.log(value); }`
|
|
176
|
-
|
|
177
|
-
For documentation on events and event listeners, check out the [Web Push SDK docs](https://documentation.onesignal.com/docs/web-push-sdk).
|
|
178
|
-
|
|
179
|
-
```js
|
|
180
|
-
const events = [
|
|
181
|
-
{
|
|
182
|
-
listener: 'once',
|
|
183
|
-
event: 'subscriptionChange',
|
|
184
|
-
callback: (isSubscribed) => {
|
|
185
|
-
if (true === isSubscribed) {
|
|
186
|
-
console.log('The user subscription state is now:', isSubscribed);
|
|
187
|
-
}
|
|
188
|
-
},
|
|
189
|
-
},
|
|
190
|
-
{
|
|
191
|
-
event: 'notificationDisplay',
|
|
192
|
-
callback: (event) => {
|
|
193
|
-
console.warn('OneSignal notification displayed:', event);
|
|
194
|
-
},
|
|
195
|
-
},
|
|
196
|
-
{
|
|
197
|
-
event: 'notificationDismiss',
|
|
198
|
-
callback: (event) => {
|
|
199
|
-
console.warn('OneSignal notification dismissed:', event);
|
|
200
|
-
},
|
|
201
|
-
},
|
|
202
|
-
];
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
OneSignal.initialize(applicationId, options, events);
|
|
150
|
+
**Example**
|
|
206
151
|
```
|
|
152
|
+
OneSignal.on('subscriptionChange', function(isSubscribed) {
|
|
153
|
+
console.log("The user's subscription state is now:", isSubscribed);
|
|
154
|
+
});
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
See the [OneSignal WebSDK Reference](https://documentation.onesignal.com/docs/web-push-sdk) for all available event listeners.
|
|
207
158
|
|
|
208
159
|
---
|
|
209
160
|
## Thanks
|
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: () => void): void;
|
|
67
|
+
off(event: string, listener: () => void): void;
|
|
68
|
+
once(event: string, listener: () => 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;
|