web-push-notifications 3.44.6 → 3.44.8
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/core/Pushwoosh.d.ts +199 -0
- package/core/Pushwoosh.types.d.ts +213 -0
- package/core/Safari.types.d.ts +17 -0
- package/core/constants.d.ts +41 -0
- package/core/events.types.d.ts +55 -0
- package/core/functions.d.ts +5 -0
- package/core/legacyEventsMap.d.ts +5 -0
- package/core/logger.d.ts +14 -0
- package/core/modules/EventBus/EventBus.d.ts +10 -0
- package/core/storage.d.ts +27 -0
- package/helpers/logger.d.ts +11 -0
- package/helpers/pwlogger/Logger.constants.d.ts +21 -0
- package/helpers/pwlogger/Logger.d.ts +37 -0
- package/helpers/pwlogger/Logger.types.d.ts +44 -0
- package/helpers/pwlogger/handlers/handler-console/handler-console.d.ts +2 -0
- package/{src/helpers/unescape.ts → helpers/unescape.d.ts} +1 -18
- package/models/InboxMessages.d.ts +82 -0
- package/models/InboxMessages.types.d.ts +83 -0
- package/models/NotificationPayload.d.ts +39 -0
- package/models/NotificationPayload.types.d.ts +63 -0
- package/modules/Api/Api.d.ts +45 -0
- package/modules/Api/Api.types.d.ts +7 -0
- package/modules/ApiClient/ApiClient.d.ts +29 -0
- package/modules/ApiClient/ApiClient.types.d.ts +188 -0
- package/modules/Data/Data.d.ts +76 -0
- package/modules/DateModule.d.ts +27 -0
- package/modules/InboxMessagesPublic.d.ts +64 -0
- package/modules/PlatformChecker/PlatformChecker.d.ts +45 -0
- package/modules/PlatformChecker/PlatformChecker.types.d.ts +5 -0
- package/modules/storage/Storage.d.ts +63 -0
- package/{src/modules/storage/Storage.types.ts → modules/storage/Storage.types.d.ts} +7 -25
- package/modules/storage/Store.d.ts +22 -0
- package/modules/storage/migrations/26-11-2018.d.ts +2 -0
- package/modules/storage/migrations/MigrationExecutor.d.ts +9 -0
- package/modules/storage/migrations/Migrations.d.ts +15 -0
- package/modules/storage/migrations/constants.d.ts +6 -0
- package/modules/storage/migrations/helpers.d.ts +7 -0
- package/modules/storage/migrations/initial.d.ts +2 -0
- package/modules/storage/version.d.ts +2 -0
- package/npm.js +2 -0
- package/npm.js.map +1 -0
- package/package.json +1 -1
- package/service-worker.d.ts +1 -0
- package/service-worker.js +2 -0
- package/service-worker.js.map +1 -0
- package/services/PushService/PushService.types.d.ts +64 -0
- package/services/PushService/drivers/PushServiceDefault/PushServiceDefault.d.ts +29 -0
- package/{src/services/PushService/drivers/PushServiceDefault/PushServiceDefault.types.ts → services/PushService/drivers/PushServiceDefault/PushServiceDefault.types.d.ts} +1 -1
- package/services/PushService/drivers/PushServiceSafari/PushServiceSafari.d.ts +21 -0
- package/services/PushService/drivers/PushServiceSafari/PushServiceSafari.types.d.ts +4 -0
- package/widget-inbox.js +2 -0
- package/widget-inbox.js.map +1 -0
- package/widget-subscribe-popup.js +2 -0
- package/widget-subscribe-popup.js.map +1 -0
- package/widget-subscription-button.js +2 -0
- package/widget-subscription-button.js.map +1 -0
- package/widget-subscription-prompt.d.ts +2 -0
- package/widget-subscription-prompt.js +2 -0
- package/widget-subscription-prompt.js.map +1 -0
- package/widgets/Inbox/InboxWidget.d.ts +49 -0
- package/widgets/Inbox/constants.d.ts +7 -0
- package/widgets/Inbox/helpers.d.ts +4 -0
- package/widgets/Inbox/inbox_widget.types.d.ts +34 -0
- package/widgets/Inbox/widgetTemplates.d.ts +4 -0
- package/widgets/SubscribePopup/SubscribePopup.d.ts +20 -0
- package/widgets/SubscribePopup/constants.d.ts +6 -0
- package/widgets/SubscribePopup/helpers.d.ts +1 -0
- package/widgets/SubscribePopup/popupTemplates.d.ts +9 -0
- package/widgets/SubscribePopup/types/subscribe-popup.d.ts +43 -0
- package/widgets/SubscriptionButton/bell.d.ts +2 -0
- package/widgets/SubscriptionButton/constants.d.ts +7 -0
- package/widgets/SubscriptionButton/index.d.ts +90 -0
- package/widgets/SubscriptionButton/positioning.d.ts +11 -0
- package/widgets/SubscriptionButton/subscribe_widget.types.d.ts +35 -0
- package/widgets/SubscriptionPrompt/SubscriptionPromptWidget.constants.d.ts +1 -0
- package/widgets/SubscriptionPrompt/SubscriptionPromptWidget.d.ts +14 -0
- package/widgets/SubscriptionPrompt/SubscriptionPromptWidget.helpers.d.ts +3 -0
- package/widgets/SubscriptionPrompt/SubscriptionPromptWidget.types.d.ts +19 -0
- package/widgets/SubscriptionPrompt/constants.d.ts +21 -0
- package/widgets/SubscriptionPrompt/helpers.d.ts +5 -0
- package/worker/global.d.ts +16 -0
- package/worker/notification.d.ts +9 -0
- package/worker/worker.types.d.ts +4 -0
- package/.editorconfig +0 -11
- package/.gitlab-ci.yml +0 -193
- package/babel.config.js +0 -7
- package/ci/cdn/Dockerfile +0 -12
- package/ci/dev/Dockerfile +0 -30
- package/ci/dev/rootfs/entrypoint.sh +0 -18
- package/ci/dev/rootfs/entrypoint.sh.d/nginx.sh +0 -6
- package/ci/dev/rootfs/entrypoint.sh.d/supervisor.sh +0 -5
- package/ci/dev/rootfs/etc/nginx/_real_ip.conf +0 -2
- package/ci/dev/rootfs/etc/nginx/conf.d/default.conf +0 -20
- package/ci/dev/rootfs/etc/supervisor.d/nginx.ini +0 -11
- package/ci/github/Dockerfile +0 -59
- package/ci/github/release-zip.js +0 -61
- package/ci/npm/Dockerfile +0 -22
- package/config/config.js +0 -24
- package/config/configBuilder.js +0 -126
- package/config/helpers.js +0 -9
- package/config/index.js +0 -1
- package/develop/README.md +0 -42
- package/develop/favicon.png +0 -0
- package/develop/index.html +0 -511
- package/eslint.config.mjs +0 -114
- package/public/index.d.ts +0 -337
- package/scripts/zip.js +0 -26
- package/src/core/Pushwoosh.ts +0 -768
- package/src/core/Pushwoosh.types.ts +0 -254
- package/src/core/Safari.types.ts +0 -26
- package/src/core/constants.ts +0 -58
- package/src/core/events.types.ts +0 -46
- package/src/core/functions.ts +0 -33
- package/src/core/legacyEventsMap.ts +0 -64
- package/src/core/logger.ts +0 -64
- package/src/core/modules/EventBus/EventBus.ts +0 -66
- package/src/core/storage.ts +0 -254
- package/src/helpers/logger.ts +0 -81
- package/src/helpers/pwlogger/Logger.constants.ts +0 -31
- package/src/helpers/pwlogger/Logger.ts +0 -218
- package/src/helpers/pwlogger/Logger.types.ts +0 -66
- package/src/helpers/pwlogger/handlers/handler-console/handler-console.ts +0 -40
- package/src/models/InboxMessages.ts +0 -202
- package/src/models/InboxMessages.types.ts +0 -111
- package/src/models/NotificationPayload.ts +0 -216
- package/src/models/NotificationPayload.types.ts +0 -65
- package/src/modules/Api/Api.ts +0 -386
- package/src/modules/Api/Api.types.ts +0 -7
- package/src/modules/ApiClient/ApiClient.ts +0 -153
- package/src/modules/ApiClient/ApiClient.types.ts +0 -222
- package/src/modules/Data/Data.ts +0 -345
- package/src/modules/DateModule.ts +0 -53
- package/src/modules/InboxMessagesPublic.ts +0 -222
- package/src/modules/PlatformChecker/PlatformChecker.ts +0 -170
- package/src/modules/PlatformChecker/PlatformChecker.types.ts +0 -5
- package/src/modules/storage/Storage.ts +0 -164
- package/src/modules/storage/Store.ts +0 -104
- package/src/modules/storage/migrations/26-11-2018.ts +0 -25
- package/src/modules/storage/migrations/MigrationExecutor.ts +0 -31
- package/src/modules/storage/migrations/Migrations.ts +0 -41
- package/src/modules/storage/migrations/constants.ts +0 -8
- package/src/modules/storage/migrations/helpers.ts +0 -16
- package/src/modules/storage/migrations/initial.ts +0 -47
- package/src/modules/storage/version.ts +0 -2
- package/src/pushwoosh-web-notifications.ts +0 -47
- package/src/pushwoosh-widget-inbox.ts +0 -8
- package/src/pushwoosh-widget-subscribe-popup.ts +0 -9
- package/src/pushwoosh-widget-subscription-button.ts +0 -8
- package/src/pushwoosh-widget-subscription-prompt.ts +0 -6
- package/src/service-worker.ts +0 -455
- package/src/services/PushService/PushService.types.ts +0 -74
- package/src/services/PushService/drivers/PushServiceDefault/PushServiceDefault.ts +0 -235
- package/src/services/PushService/drivers/PushServiceSafari/PushServiceSafari.ts +0 -125
- package/src/services/PushService/drivers/PushServiceSafari/PushServiceSafari.types.ts +0 -4
- package/src/widget-subscription-prompt.ts +0 -33
- package/src/widgets/Inbox/InboxWidget.ts +0 -564
- package/src/widgets/Inbox/constants.ts +0 -49
- package/src/widgets/Inbox/css/inboxWidgetStyle.css +0 -274
- package/src/widgets/Inbox/helpers.ts +0 -63
- package/src/widgets/Inbox/inbox.d.ts +0 -9
- package/src/widgets/Inbox/inbox_widget.types.ts +0 -41
- package/src/widgets/Inbox/widgetTemplates.ts +0 -55
- package/src/widgets/SubscribePopup/SubscribePopup.ts +0 -241
- package/src/widgets/SubscribePopup/constants.ts +0 -66
- package/src/widgets/SubscribePopup/helpers.ts +0 -11
- package/src/widgets/SubscribePopup/popupTemplates.ts +0 -24
- package/src/widgets/SubscribePopup/styles/popup.css +0 -226
- package/src/widgets/SubscribePopup/types/subscribe-popup.ts +0 -68
- package/src/widgets/SubscriptionButton/assets/css/main.css +0 -205
- package/src/widgets/SubscriptionButton/bell.ts +0 -67
- package/src/widgets/SubscriptionButton/constants.ts +0 -28
- package/src/widgets/SubscriptionButton/index.ts +0 -377
- package/src/widgets/SubscriptionButton/positioning.ts +0 -165
- package/src/widgets/SubscriptionButton/subscribe_widget.types.ts +0 -53
- package/src/widgets/SubscriptionPrompt/SubscriptionPromptWidget.constants.ts +0 -1
- package/src/widgets/SubscriptionPrompt/SubscriptionPromptWidget.helpers.ts +0 -110
- package/src/widgets/SubscriptionPrompt/SubscriptionPromptWidget.ts +0 -102
- package/src/widgets/SubscriptionPrompt/SubscriptionPromptWidget.types.ts +0 -23
- package/src/widgets/SubscriptionPrompt/constants.ts +0 -22
- package/src/widgets/SubscriptionPrompt/helpers.ts +0 -42
- package/src/widgets/widgets.d.ts +0 -4
- package/src/worker/global.ts +0 -36
- package/src/worker/notification.ts +0 -34
- package/src/worker/worker.types.ts +0 -4
- package/test/__helpers__/apiHelpers.ts +0 -22
- package/test/__helpers__/keyValueHelpers.ts +0 -15
- package/test/__helpers__/platformHelpers.ts +0 -54
- package/test/__helpers__/sinonHelpers.ts +0 -7
- package/test/__helpers__/storageHelpers.ts +0 -56
- package/test/__mocks__/apiRequests.ts +0 -26
- package/test/__mocks__/idbMock.ts +0 -12
- package/test/__mocks__/idbObjectStoreMock.ts +0 -38
- package/test/__mocks__/inboxMessages.ts +0 -292
- package/test/__mocks__/models/inboxModel.ts +0 -71
- package/test/__mocks__/modules/apiClientModule.ts +0 -18
- package/test/__mocks__/modules/dateModule.ts +0 -34
- package/test/__mocks__/modules/inboxParamsModule.ts +0 -21
- package/test/__mocks__/modules/paramsBuilder.ts +0 -12
- package/test/__mocks__/modules/paramsModule.ts +0 -35
- package/test/__mocks__/modules/payloadBuilderModule.ts +0 -15
- package/test/__mocks__/modules/storageModule.ts +0 -58
- package/test/__mocks__/navigator.ts +0 -38
- package/test/__mocks__/notification.ts +0 -84
- package/test/__mocks__/pushwoosh.ts +0 -12
- package/test/__mocks__/userAgents +0 -8
- package/test/functions.test.ts +0 -22
- package/test/ignore-html.js +0 -6
- package/test/mocha.opts +0 -6
- package/test/modules/DateModule/unit.test.ts +0 -80
- package/test/modules/storage/Storage/unit.test.ts +0 -180
- package/test/modules/storage/Store/unit.test.ts +0 -192
- package/testRegister.js +0 -24
- package/tsconfig.json +0 -31
- package/webpack.config.js +0 -163
- /package/{src/core/modules/EventBus/index.ts → core/modules/EventBus/index.d.ts} +0 -0
- /package/{src/helpers/pwlogger/index.ts → helpers/pwlogger/index.d.ts} +0 -0
- /package/{src/modules/PlatformChecker/index.ts → modules/PlatformChecker/index.d.ts} +0 -0
- /package/{src/npm.ts → npm.d.ts} +0 -0
- /package/{src/services/PushService/PushService.ts → services/PushService/PushService.d.ts} +0 -0
- /package/{src/widget-inbox.ts → widget-inbox.d.ts} +0 -0
- /package/{src/widget-subscribe-popup.ts → widget-subscribe-popup.d.ts} +0 -0
- /package/{src/widget-subscription-button.ts → widget-subscription-button.d.ts} +0 -0
- /package/{src/widgets/Inbox/index.ts → widgets/Inbox/index.d.ts} +0 -0
- /package/{src/widgets/SubscribePopup/index.ts → widgets/SubscribePopup/index.d.ts} +0 -0
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
export interface IPushServiceSubscriptionKeys {
|
|
2
|
-
pushToken?: string;
|
|
3
|
-
publicKey?: string;
|
|
4
|
-
authToken?: string;
|
|
5
|
-
endpoint?: string;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export abstract class IPushService {
|
|
9
|
-
/**
|
|
10
|
-
* Get current status of permission for send notifications.
|
|
11
|
-
*
|
|
12
|
-
* @return { NotificationPermission }
|
|
13
|
-
*/
|
|
14
|
-
public abstract getPermission(): NotificationPermission;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Check is permission granted.
|
|
18
|
-
*
|
|
19
|
-
* @return { boolean }
|
|
20
|
-
*/
|
|
21
|
-
public abstract checkIsPermissionGranted(): boolean;
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Check is permission default.
|
|
25
|
-
*
|
|
26
|
-
* @return { boolean }
|
|
27
|
-
*/
|
|
28
|
-
public abstract checkIsPermissionDefault(): boolean;
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Check is manual unsubscribed for push notifications.
|
|
32
|
-
*/
|
|
33
|
-
public abstract checkIsManualUnsubscribed(): Promise<boolean>;
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Request permission for send notifications.
|
|
37
|
-
*
|
|
38
|
-
* @return { Promise<void> }
|
|
39
|
-
*/
|
|
40
|
-
public abstract askPermission(): Promise<void>;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Get subscription keys for send push notifications.
|
|
44
|
-
*
|
|
45
|
-
* @return { Promise<IPushServiceSubscriptionKeys> }
|
|
46
|
-
*/
|
|
47
|
-
public abstract getTokens(): Promise<IPushServiceSubscriptionKeys>;
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Subscribe for push notifications.
|
|
51
|
-
*
|
|
52
|
-
* @return { Promise<void> }
|
|
53
|
-
*/
|
|
54
|
-
public abstract subscribe(): Promise<void>;
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Unsubscribe for push notifications.
|
|
58
|
-
*
|
|
59
|
-
* @return { Promise<void> }
|
|
60
|
-
*/
|
|
61
|
-
public abstract unsubscribe(): Promise<void>;
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Check can receive notifications from Pushwoosh.
|
|
65
|
-
*
|
|
66
|
-
* @return { Promise<boolean> }
|
|
67
|
-
*/
|
|
68
|
-
public abstract checkIsRegister(): Promise<boolean>;
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Check is change sender configuration
|
|
72
|
-
*/
|
|
73
|
-
public abstract checkIsNeedResubscribe(): Promise<boolean>;
|
|
74
|
-
}
|
|
@@ -1,235 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type IPushServiceDefaultConfig,
|
|
3
|
-
} from './PushServiceDefault.types';
|
|
4
|
-
import * as CONSTANTS from '../../../../core/constants';
|
|
5
|
-
import { v4 } from '../../../../core/functions';
|
|
6
|
-
import { Logger } from '../../../../core/logger';
|
|
7
|
-
import { type Api } from '../../../../modules/Api/Api';
|
|
8
|
-
import { type Data } from '../../../../modules/Data/Data';
|
|
9
|
-
import {
|
|
10
|
-
type IPushService,
|
|
11
|
-
type IPushServiceSubscriptionKeys,
|
|
12
|
-
} from '../../PushService.types';
|
|
13
|
-
|
|
14
|
-
export class PushServiceDefault implements IPushService {
|
|
15
|
-
private readonly api: Api;
|
|
16
|
-
private readonly data: Data;
|
|
17
|
-
private readonly config: IPushServiceDefaultConfig;
|
|
18
|
-
private registration?: ServiceWorkerRegistration;
|
|
19
|
-
|
|
20
|
-
constructor(api: Api, data: Data, config: IPushServiceDefaultConfig) {
|
|
21
|
-
this.api = api;
|
|
22
|
-
this.data = data;
|
|
23
|
-
this.config = config;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
public getPermission(): NotificationPermission {
|
|
27
|
-
return Notification.permission;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
public checkIsPermissionGranted(): boolean {
|
|
31
|
-
return this.getPermission() === CONSTANTS.PERMISSION_GRANTED;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
public checkIsPermissionDefault(): boolean {
|
|
35
|
-
return this.getPermission() === CONSTANTS.PERMISSION_PROMPT;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
public async checkIsManualUnsubscribed(): Promise<boolean> {
|
|
39
|
-
return this.data.getStatusManualUnsubscribed();
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
public async askPermission(): Promise<void> {
|
|
43
|
-
await Notification.requestPermission();
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
public async getTokens(): Promise<IPushServiceSubscriptionKeys> {
|
|
47
|
-
return this.data.getTokens();
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
public async subscribe(subscription?: PushSubscription): Promise<void> {
|
|
51
|
-
const currentSubscription = subscription || await this.trySubscribe();
|
|
52
|
-
const isPermissionGranted = this.checkIsPermissionGranted();
|
|
53
|
-
|
|
54
|
-
if (!isPermissionGranted) {
|
|
55
|
-
Logger.error('You must have permission granted before subscribe!');
|
|
56
|
-
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const pushToken = this.getPushToken(currentSubscription);
|
|
61
|
-
const _p256dn = currentSubscription.getKey('p256dh');
|
|
62
|
-
const _auth = currentSubscription.getKey('auth');
|
|
63
|
-
|
|
64
|
-
if (!_p256dn || !_auth) {
|
|
65
|
-
throw new Error('Can\'t get subscription keys!');
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const p256dh = btoa(String.fromCharCode.apply(String, new Uint8Array(_p256dn))); // eslint-disable-line prefer-spread
|
|
69
|
-
const auth = btoa(String.fromCharCode.apply(String, new Uint8Array(_auth))); // eslint-disable-line prefer-spread
|
|
70
|
-
|
|
71
|
-
await this.data.setTokens({
|
|
72
|
-
publicKey: p256dh,
|
|
73
|
-
pushToken: pushToken,
|
|
74
|
-
authToken: auth,
|
|
75
|
-
endpoint: currentSubscription.endpoint,
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
// register device into pushwoosh
|
|
79
|
-
await this.api.registerDevice();
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
public async unsubscribe(): Promise<void> {
|
|
83
|
-
// get service worker registration
|
|
84
|
-
const registration = await this.getServiceWorkerRegistration();
|
|
85
|
-
|
|
86
|
-
// get current subscription
|
|
87
|
-
const subscription = await registration.pushManager.getSubscription();
|
|
88
|
-
|
|
89
|
-
// remove tokens
|
|
90
|
-
await this.data.setTokens({});
|
|
91
|
-
|
|
92
|
-
// set info to database, that the device IS manual unsubscribed
|
|
93
|
-
await this.data.setStatusManualUnsubscribed(true);
|
|
94
|
-
|
|
95
|
-
// unregister device in pushwoosh
|
|
96
|
-
await this.api.unregisterDevice();
|
|
97
|
-
|
|
98
|
-
if (!subscription) {
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// remove subscription
|
|
103
|
-
await subscription.unsubscribe();
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
public async checkIsRegister(): Promise<boolean> {
|
|
107
|
-
return this.api.checkDeviceSubscribeForPushNotifications();
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
public async checkIsNeedResubscribe(): Promise<boolean> {
|
|
111
|
-
// check change permission status
|
|
112
|
-
const lastPermission = await this.data.getLastPermissionStatus();
|
|
113
|
-
const permission = this.getPermission();
|
|
114
|
-
|
|
115
|
-
if (lastPermission !== permission) {
|
|
116
|
-
await this.data.setLastPermissionStatus(permission);
|
|
117
|
-
|
|
118
|
-
return true;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// check if pushTokens not equal from pushSubscription and store
|
|
122
|
-
const credentials = await this.getCredentials();
|
|
123
|
-
const pushTokenFromSubscription = this.getPushToken(credentials);
|
|
124
|
-
|
|
125
|
-
const subscriptionTokensFromStore = await this.data.getTokens();
|
|
126
|
-
const pushTokenFromStore = subscriptionTokensFromStore && subscriptionTokensFromStore.pushToken || '';
|
|
127
|
-
|
|
128
|
-
const isEqualPushTokens = pushTokenFromSubscription === pushTokenFromStore;
|
|
129
|
-
|
|
130
|
-
const isVapidChanged = await this.data.getIsVapidChanged();
|
|
131
|
-
|
|
132
|
-
return !isEqualPushTokens || isVapidChanged;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
private async getServiceWorkerRegistration(): Promise<ServiceWorkerRegistration> {
|
|
136
|
-
if (!this.registration) {
|
|
137
|
-
await this.registerServiceWorker();
|
|
138
|
-
|
|
139
|
-
const url = await this.data.getServiceWorkerUrl();
|
|
140
|
-
this.registration = await navigator.serviceWorker.getRegistration(url);
|
|
141
|
-
|
|
142
|
-
await (this.registration as ServiceWorkerRegistration).update();
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
if (!this.registration) {
|
|
146
|
-
throw new Error(`Internal Error: Can't register service worker!`);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
return this.registration;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
private async registerServiceWorker(): Promise<void> {
|
|
153
|
-
const url = await this.data.getServiceWorkerUrl();
|
|
154
|
-
const scope = await this.data.getServiceWorkerScope();
|
|
155
|
-
const sdkVersion = await this.data.getSdkVersion();
|
|
156
|
-
const serviceWorkerVersion = await this.data.getServiceWorkerVersion();
|
|
157
|
-
|
|
158
|
-
// add clean cache get parameter only if
|
|
159
|
-
// sdk version and service worker version is not equals
|
|
160
|
-
let cleanCache = '';
|
|
161
|
-
|
|
162
|
-
if (sdkVersion !== serviceWorkerVersion) {
|
|
163
|
-
cleanCache = `?cache_clean=${v4()}`;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
await navigator
|
|
167
|
-
.serviceWorker
|
|
168
|
-
.register(`${url}${cleanCache}`, {
|
|
169
|
-
scope,
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
private async trySubscribe(): Promise<PushSubscription> {
|
|
174
|
-
try {
|
|
175
|
-
return await this.subscribePushManager();
|
|
176
|
-
} catch (error) {
|
|
177
|
-
console.error(error);
|
|
178
|
-
// if get subscription filed
|
|
179
|
-
// try unsubscribe and resubscribe again
|
|
180
|
-
// it may be if changed vapid or sender id
|
|
181
|
-
await this.unsubscribe();
|
|
182
|
-
return this.subscribePushManager();
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
private async subscribePushManager(): Promise<PushSubscription> {
|
|
187
|
-
// get service worker registration
|
|
188
|
-
const registration = await this.getServiceWorkerRegistration();
|
|
189
|
-
const applicationServerKey = await this.getApplicationServerKey();
|
|
190
|
-
|
|
191
|
-
return registration.pushManager.subscribe({
|
|
192
|
-
userVisibleOnly: true,
|
|
193
|
-
applicationServerKey: applicationServerKey ? this.urlBase64ToUint8Array(applicationServerKey) : null,
|
|
194
|
-
});
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
private async getCredentials(): Promise<PushSubscription | null> {
|
|
198
|
-
const registration = await this.getServiceWorkerRegistration();
|
|
199
|
-
return await registration.pushManager.getSubscription();
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
private getPushToken(subscription: PushSubscription | null): string {
|
|
203
|
-
if (!subscription) {
|
|
204
|
-
return '';
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
return subscription.endpoint;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
private async getApplicationServerKey(): Promise<string | undefined> {
|
|
211
|
-
const deviceType = await this.data.getDeviceType();
|
|
212
|
-
|
|
213
|
-
// if not chrome
|
|
214
|
-
if (deviceType !== 11) {
|
|
215
|
-
return;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
return await this.data.getApplicationServerKey();
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
private urlBase64ToUint8Array(base64String: string): Uint8Array {
|
|
222
|
-
const padding = '='.repeat((4 - base64String.length % 4) % 4);
|
|
223
|
-
const base64 = (base64String + padding)
|
|
224
|
-
.replace(/-/g, '+')
|
|
225
|
-
.replace(/_/g, '/');
|
|
226
|
-
|
|
227
|
-
const rawData = atob(base64);
|
|
228
|
-
const outputArray = new Uint8Array(rawData.length);
|
|
229
|
-
|
|
230
|
-
for (let i = 0; i < rawData.length; ++i) {
|
|
231
|
-
outputArray[i] = rawData.charCodeAt(i);
|
|
232
|
-
}
|
|
233
|
-
return outputArray;
|
|
234
|
-
}
|
|
235
|
-
}
|
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
import { type IPushServiceSafariConfig } from './PushServiceSafari.types';
|
|
2
|
-
import * as CONSTANTS from '../../../../core/constants';
|
|
3
|
-
import { Logger } from '../../../../core/logger';
|
|
4
|
-
import { type Api } from '../../../../modules/Api/Api';
|
|
5
|
-
import { type Data } from '../../../../modules/Data/Data';
|
|
6
|
-
import {
|
|
7
|
-
type IPushService,
|
|
8
|
-
type IPushServiceSubscriptionKeys,
|
|
9
|
-
} from '../../PushService.types';
|
|
10
|
-
|
|
11
|
-
export class PushServiceSafari implements IPushService {
|
|
12
|
-
private readonly api: Api;
|
|
13
|
-
private readonly data: Data;
|
|
14
|
-
private readonly config: IPushServiceSafariConfig;
|
|
15
|
-
|
|
16
|
-
constructor(
|
|
17
|
-
api: Api,
|
|
18
|
-
data: Data,
|
|
19
|
-
config: IPushServiceSafariConfig,
|
|
20
|
-
) {
|
|
21
|
-
this.api = api;
|
|
22
|
-
this.config = config;
|
|
23
|
-
this.data = data;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
public getPermission(): NotificationPermission {
|
|
27
|
-
const { permission } = this.getPermissionInfo();
|
|
28
|
-
|
|
29
|
-
return permission;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
public checkIsPermissionGranted(): boolean {
|
|
33
|
-
return this.getPermission() === CONSTANTS.PERMISSION_GRANTED;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
public checkIsPermissionDefault(): boolean {
|
|
37
|
-
return this.getPermission() === CONSTANTS.PERMISSION_PROMPT;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
public async checkIsManualUnsubscribed(): Promise<boolean> {
|
|
41
|
-
return await this.data.getStatusManualUnsubscribed();
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
public async askPermission(): Promise<void> {
|
|
45
|
-
const application = await this.data.getApplicationCode();
|
|
46
|
-
const hwid = await this.data.getHwid();
|
|
47
|
-
const payload = {
|
|
48
|
-
application,
|
|
49
|
-
hwid,
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
return new Promise((resolve) => {
|
|
53
|
-
// safari send payload by apns to entrypoint 'https://cp.pushwoosh.com/json/1.3/safari'
|
|
54
|
-
// and then take push token to browser
|
|
55
|
-
safari.pushNotification.requestPermission(
|
|
56
|
-
this.config.entrypoint || 'https://cp.pushwoosh.com/json/1.3/safari',
|
|
57
|
-
this.config.webSitePushId,
|
|
58
|
-
payload,
|
|
59
|
-
() => resolve(),
|
|
60
|
-
);
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
public getTokens(): Promise<IPushServiceSubscriptionKeys> {
|
|
65
|
-
return this.data.getTokens();
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
public async subscribe(): Promise<void> {
|
|
69
|
-
const isPermissionGranted = this.checkIsPermissionGranted();
|
|
70
|
-
|
|
71
|
-
if (!isPermissionGranted) {
|
|
72
|
-
Logger.error('You must have permission granted before subscribe!');
|
|
73
|
-
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
const { deviceToken } = this.getPermissionInfo();
|
|
78
|
-
|
|
79
|
-
await this.data.setTokens({
|
|
80
|
-
pushToken: deviceToken,
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
await this.api.registerDevice();
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
public async unsubscribe(): Promise<void> {
|
|
87
|
-
// remove tokens
|
|
88
|
-
await this.data.setTokens({});
|
|
89
|
-
|
|
90
|
-
// set info to database, that the device IS manual unsubscribed
|
|
91
|
-
await this.data.setStatusManualUnsubscribed(true);
|
|
92
|
-
|
|
93
|
-
// unregister device in pushwoosh
|
|
94
|
-
await this.api.unregisterDevice();
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
public async checkIsRegister(): Promise<boolean> {
|
|
98
|
-
return this.api.checkDeviceSubscribeForPushNotifications();
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
public async checkIsNeedResubscribe(): Promise<boolean> {
|
|
102
|
-
// check web site id
|
|
103
|
-
const savedWebSitePushId = await this.data.getWebSitePushId();
|
|
104
|
-
const isExistSavedWebSitePushId = typeof savedWebSitePushId !== 'undefined';
|
|
105
|
-
const isChangeWebSitePushId = isExistSavedWebSitePushId && this.config.webSitePushId !== savedWebSitePushId;
|
|
106
|
-
|
|
107
|
-
await this.data.setWebSitePushId(this.config.webSitePushId);
|
|
108
|
-
|
|
109
|
-
// check change permission status
|
|
110
|
-
const lastPermission = await this.data.getLastPermissionStatus();
|
|
111
|
-
const permission = this.getPermission();
|
|
112
|
-
|
|
113
|
-
if (lastPermission !== permission) {
|
|
114
|
-
await this.data.setLastPermissionStatus(permission);
|
|
115
|
-
|
|
116
|
-
return true;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
return isChangeWebSitePushId;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
private getPermissionInfo(): IPushServiceSafariInfo {
|
|
123
|
-
return safari.pushNotification.permission(this.config.webSitePushId);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { type Pushwoosh } from './core/Pushwoosh';
|
|
2
|
-
import {
|
|
3
|
-
SUBSCRIPTION_WIDGET_USE_CASE_DEFAULT,
|
|
4
|
-
SUBSCRIPTION_WIDGET_USE_CASE_NOT_SET,
|
|
5
|
-
} from './widgets/SubscriptionPrompt/constants';
|
|
6
|
-
import { checkCanShowByCapping, getWidgetConfig, updateCappingParams } from './widgets/SubscriptionPrompt/helpers';
|
|
7
|
-
import { SubscriptionPromptWidget } from './widgets/SubscriptionPrompt/SubscriptionPromptWidget';
|
|
8
|
-
|
|
9
|
-
export const createWidget = async (globalPW: Pushwoosh) => {
|
|
10
|
-
const features = await globalPW.data.getFeatures();
|
|
11
|
-
const widgetConfig = getWidgetConfig(features);
|
|
12
|
-
const canShowByCapping = await checkCanShowByCapping(widgetConfig, globalPW);
|
|
13
|
-
if (!canShowByCapping) {
|
|
14
|
-
return;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const currentPromptUseCase = features['subscription_prompt']?.['use_case'];
|
|
18
|
-
const autoSubscribe = 'autoSubscribe' in globalPW.initParams ? !!globalPW.initParams.autoSubscribe : true;
|
|
19
|
-
|
|
20
|
-
// show subscription prompt widget
|
|
21
|
-
const isDefaultUseCase = currentPromptUseCase === SUBSCRIPTION_WIDGET_USE_CASE_DEFAULT;
|
|
22
|
-
const isNotSetUseCase = currentPromptUseCase === SUBSCRIPTION_WIDGET_USE_CASE_NOT_SET && autoSubscribe;
|
|
23
|
-
|
|
24
|
-
// show subscription prompt widget
|
|
25
|
-
if (isDefaultUseCase || isNotSetUseCase) {
|
|
26
|
-
const subscriptionPromptWidget = new SubscriptionPromptWidget(globalPW);
|
|
27
|
-
subscriptionPromptWidget.init(widgetConfig);
|
|
28
|
-
subscriptionPromptWidget.show();
|
|
29
|
-
globalPW.moduleRegistry.subscriptionPrompt = subscriptionPromptWidget;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
await updateCappingParams(globalPW);
|
|
33
|
-
};
|