@aws-amplify/notifications 2.0.1-console-preview.5f040a2.0 → 2.0.1-console-preview.23dc225.0
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/lib/common/AWSPinpointProviderCommon/index.d.ts +0 -1
- package/lib/common/AWSPinpointProviderCommon/index.js +14 -19
- package/lib/common/AWSPinpointProviderCommon/index.js.map +1 -1
- package/lib/common/eventListeners/index.d.ts +2 -2
- package/lib/common/eventListeners/index.js +3 -1
- package/lib/common/eventListeners/index.js.map +1 -1
- package/lib/common/eventListeners/types.d.ts +3 -0
- package/lib/common/index.d.ts +1 -1
- package/lib/inAppMessaging/errors/validation.d.ts +2 -1
- package/lib/inAppMessaging/errors/validation.js +5 -0
- package/lib/inAppMessaging/errors/validation.js.map +1 -1
- package/lib/inAppMessaging/index.d.ts +1 -1
- package/lib/inAppMessaging/index.js +8 -0
- package/lib/inAppMessaging/index.js.map +1 -1
- package/lib/inAppMessaging/providers/index.d.ts +1 -1
- package/lib/inAppMessaging/providers/index.js +8 -0
- package/lib/inAppMessaging/providers/index.js.map +1 -1
- package/lib/inAppMessaging/providers/pinpoint/apis/dispatchEvent.d.ts +26 -0
- package/lib/inAppMessaging/providers/pinpoint/apis/dispatchEvent.js +69 -0
- package/lib/inAppMessaging/providers/pinpoint/apis/dispatchEvent.js.map +1 -0
- package/lib/inAppMessaging/providers/pinpoint/apis/identifyUser.d.ts +1 -4
- package/lib/inAppMessaging/providers/pinpoint/apis/identifyUser.js +5 -5
- package/lib/inAppMessaging/providers/pinpoint/apis/identifyUser.js.map +1 -1
- package/lib/inAppMessaging/providers/pinpoint/apis/index.d.ts +8 -0
- package/lib/inAppMessaging/providers/pinpoint/apis/index.js +16 -0
- package/lib/inAppMessaging/providers/pinpoint/apis/index.js.map +1 -1
- package/lib/inAppMessaging/providers/pinpoint/apis/initializeInAppMessaging.d.ts +12 -0
- package/lib/inAppMessaging/providers/pinpoint/apis/initializeInAppMessaging.js +61 -0
- package/lib/inAppMessaging/providers/pinpoint/apis/initializeInAppMessaging.js.map +1 -0
- package/lib/inAppMessaging/providers/pinpoint/apis/notifyMessageInteraction.d.ts +16 -0
- package/lib/inAppMessaging/providers/pinpoint/apis/notifyMessageInteraction.js +27 -0
- package/lib/inAppMessaging/providers/pinpoint/apis/notifyMessageInteraction.js.map +1 -0
- package/lib/inAppMessaging/providers/pinpoint/apis/onMessageActionTaken.d.ts +18 -0
- package/lib/inAppMessaging/providers/pinpoint/apis/onMessageActionTaken.js +27 -0
- package/lib/inAppMessaging/providers/pinpoint/apis/onMessageActionTaken.js.map +1 -0
- package/lib/inAppMessaging/providers/pinpoint/apis/onMessageDismissed.d.ts +18 -0
- package/lib/inAppMessaging/providers/pinpoint/apis/onMessageDismissed.js +27 -0
- package/lib/inAppMessaging/providers/pinpoint/apis/onMessageDismissed.js.map +1 -0
- package/lib/inAppMessaging/providers/pinpoint/apis/onMessageDisplayed.d.ts +18 -0
- package/lib/inAppMessaging/providers/pinpoint/apis/onMessageDisplayed.js +27 -0
- package/lib/inAppMessaging/providers/pinpoint/apis/onMessageDisplayed.js.map +1 -0
- package/lib/inAppMessaging/providers/pinpoint/apis/onMessageReceived.d.ts +18 -0
- package/lib/inAppMessaging/providers/pinpoint/apis/onMessageReceived.js +27 -0
- package/lib/inAppMessaging/providers/pinpoint/apis/onMessageReceived.js.map +1 -0
- package/lib/inAppMessaging/providers/pinpoint/apis/setConflictHandler.d.ts +32 -0
- package/lib/inAppMessaging/providers/pinpoint/apis/setConflictHandler.js +66 -0
- package/lib/inAppMessaging/providers/pinpoint/apis/setConflictHandler.js.map +1 -0
- package/lib/inAppMessaging/providers/pinpoint/apis/syncMessages.d.ts +1 -3
- package/lib/inAppMessaging/providers/pinpoint/apis/syncMessages.js +20 -18
- package/lib/inAppMessaging/providers/pinpoint/apis/syncMessages.js.map +1 -1
- package/lib/inAppMessaging/providers/pinpoint/index.d.ts +1 -1
- package/lib/inAppMessaging/providers/pinpoint/index.js +8 -0
- package/lib/inAppMessaging/providers/pinpoint/index.js.map +1 -1
- package/lib/inAppMessaging/providers/pinpoint/types/index.d.ts +2 -1
- package/lib/inAppMessaging/providers/pinpoint/types/index.js +2 -0
- package/lib/inAppMessaging/providers/pinpoint/types/index.js.map +1 -1
- package/lib/inAppMessaging/providers/pinpoint/types/inputs.d.ts +33 -2
- package/lib/inAppMessaging/providers/pinpoint/types/outputs.d.ts +17 -0
- package/lib/inAppMessaging/providers/pinpoint/types/outputs.js +5 -0
- package/lib/inAppMessaging/providers/pinpoint/types/outputs.js.map +1 -0
- package/lib/inAppMessaging/providers/pinpoint/types/types.d.ts +4 -1
- package/lib/inAppMessaging/providers/pinpoint/types/types.js +6 -6
- package/lib/inAppMessaging/providers/pinpoint/types/types.js.map +1 -1
- package/lib/inAppMessaging/providers/pinpoint/utils/helpers.d.ts +17 -0
- package/lib/inAppMessaging/providers/pinpoint/utils/helpers.js +260 -0
- package/lib/inAppMessaging/providers/pinpoint/utils/helpers.js.map +1 -0
- package/lib/inAppMessaging/providers/pinpoint/utils/index.d.ts +1 -0
- package/lib/inAppMessaging/providers/pinpoint/utils/index.js +4 -0
- package/lib/inAppMessaging/providers/pinpoint/utils/index.js.map +1 -1
- package/lib/inAppMessaging/providers/pinpoint/utils/messageProcessingHelpers.d.ts +6 -0
- package/lib/inAppMessaging/providers/pinpoint/utils/messageProcessingHelpers.js +247 -0
- package/lib/inAppMessaging/providers/pinpoint/utils/messageProcessingHelpers.js.map +1 -0
- package/lib/inAppMessaging/providers/pinpoint/utils/resolveCredentials.d.ts +1 -6
- package/lib/inAppMessaging/types/index.d.ts +1 -1
- package/lib/inAppMessaging/utils/index.d.ts +1 -0
- package/lib/inAppMessaging/utils/index.js +9 -0
- package/lib/inAppMessaging/utils/index.js.map +1 -0
- package/lib/inAppMessaging/utils/statusHelpers.d.ts +13 -0
- package/lib/inAppMessaging/utils/statusHelpers.js +25 -0
- package/lib/inAppMessaging/utils/statusHelpers.js.map +1 -0
- package/lib-esm/common/AWSPinpointProviderCommon/index.d.ts +0 -1
- package/lib-esm/common/AWSPinpointProviderCommon/index.js +15 -20
- package/lib-esm/common/AWSPinpointProviderCommon/index.js.map +1 -1
- package/lib-esm/common/eventListeners/index.d.ts +2 -2
- package/lib-esm/common/eventListeners/index.js +3 -1
- package/lib-esm/common/eventListeners/index.js.map +1 -1
- package/lib-esm/common/eventListeners/types.d.ts +3 -0
- package/lib-esm/common/index.d.ts +1 -1
- package/lib-esm/inAppMessaging/errors/validation.d.ts +2 -1
- package/lib-esm/inAppMessaging/errors/validation.js +5 -0
- package/lib-esm/inAppMessaging/errors/validation.js.map +1 -1
- package/lib-esm/inAppMessaging/index.d.ts +1 -1
- package/lib-esm/inAppMessaging/index.js +1 -1
- package/lib-esm/inAppMessaging/index.js.map +1 -1
- package/lib-esm/inAppMessaging/providers/index.d.ts +1 -1
- package/lib-esm/inAppMessaging/providers/index.js +1 -1
- package/lib-esm/inAppMessaging/providers/index.js.map +1 -1
- package/lib-esm/inAppMessaging/providers/pinpoint/apis/dispatchEvent.d.ts +26 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/apis/dispatchEvent.js +66 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/apis/dispatchEvent.js.map +1 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/apis/identifyUser.d.ts +1 -4
- package/lib-esm/inAppMessaging/providers/pinpoint/apis/identifyUser.js +5 -5
- package/lib-esm/inAppMessaging/providers/pinpoint/apis/identifyUser.js.map +1 -1
- package/lib-esm/inAppMessaging/providers/pinpoint/apis/index.d.ts +8 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/apis/index.js +8 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/apis/index.js.map +1 -1
- package/lib-esm/inAppMessaging/providers/pinpoint/apis/initializeInAppMessaging.d.ts +12 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/apis/initializeInAppMessaging.js +57 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/apis/initializeInAppMessaging.js.map +1 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/apis/notifyMessageInteraction.d.ts +16 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/apis/notifyMessageInteraction.js +24 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/apis/notifyMessageInteraction.js.map +1 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageActionTaken.d.ts +18 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageActionTaken.js +24 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageActionTaken.js.map +1 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageDismissed.d.ts +18 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageDismissed.js +24 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageDismissed.js.map +1 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageDisplayed.d.ts +18 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageDisplayed.js +24 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageDisplayed.js.map +1 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageReceived.d.ts +18 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageReceived.js +24 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/apis/onMessageReceived.js.map +1 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/apis/setConflictHandler.d.ts +32 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/apis/setConflictHandler.js +63 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/apis/setConflictHandler.js.map +1 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/apis/syncMessages.d.ts +1 -3
- package/lib-esm/inAppMessaging/providers/pinpoint/apis/syncMessages.js +20 -18
- package/lib-esm/inAppMessaging/providers/pinpoint/apis/syncMessages.js.map +1 -1
- package/lib-esm/inAppMessaging/providers/pinpoint/index.d.ts +1 -1
- package/lib-esm/inAppMessaging/providers/pinpoint/index.js +1 -1
- package/lib-esm/inAppMessaging/providers/pinpoint/index.js.map +1 -1
- package/lib-esm/inAppMessaging/providers/pinpoint/types/index.d.ts +2 -1
- package/lib-esm/inAppMessaging/providers/pinpoint/types/index.js +1 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/types/index.js.map +1 -1
- package/lib-esm/inAppMessaging/providers/pinpoint/types/inputs.d.ts +33 -2
- package/lib-esm/inAppMessaging/providers/pinpoint/types/outputs.d.ts +17 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/types/outputs.js +3 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/types/outputs.js.map +1 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/types/types.d.ts +4 -1
- package/lib-esm/inAppMessaging/providers/pinpoint/types/types.js +6 -6
- package/lib-esm/inAppMessaging/providers/pinpoint/types/types.js.map +1 -1
- package/lib-esm/inAppMessaging/providers/pinpoint/utils/helpers.d.ts +17 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/utils/helpers.js +258 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/utils/helpers.js.map +1 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/utils/index.d.ts +1 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/utils/index.js +1 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/utils/index.js.map +1 -1
- package/lib-esm/inAppMessaging/providers/pinpoint/utils/messageProcessingHelpers.d.ts +6 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/utils/messageProcessingHelpers.js +242 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/utils/messageProcessingHelpers.js.map +1 -0
- package/lib-esm/inAppMessaging/providers/pinpoint/utils/resolveCredentials.d.ts +1 -6
- package/lib-esm/inAppMessaging/types/index.d.ts +1 -1
- package/lib-esm/inAppMessaging/utils/index.d.ts +1 -0
- package/lib-esm/inAppMessaging/utils/index.js +4 -0
- package/lib-esm/inAppMessaging/utils/index.js.map +1 -0
- package/lib-esm/inAppMessaging/utils/statusHelpers.d.ts +13 -0
- package/lib-esm/inAppMessaging/utils/statusHelpers.js +22 -0
- package/lib-esm/inAppMessaging/utils/statusHelpers.js.map +1 -0
- package/package.json +5 -6
- package/src/common/AWSPinpointProviderCommon/index.ts +9 -14
- package/src/common/eventListeners/index.ts +5 -3
- package/src/common/eventListeners/types.ts +4 -0
- package/src/common/index.ts +5 -1
- package/src/inAppMessaging/errors/validation.ts +6 -0
- package/src/inAppMessaging/index.ts +12 -1
- package/src/inAppMessaging/providers/index.ts +12 -1
- package/src/inAppMessaging/providers/pinpoint/apis/dispatchEvent.ts +63 -0
- package/src/inAppMessaging/providers/pinpoint/apis/identifyUser.ts +3 -4
- package/src/inAppMessaging/providers/pinpoint/apis/index.ts +8 -0
- package/src/inAppMessaging/providers/pinpoint/apis/initializeInAppMessaging.ts +64 -0
- package/src/inAppMessaging/providers/pinpoint/apis/notifyMessageInteraction.ts +28 -0
- package/src/inAppMessaging/providers/pinpoint/apis/onMessageActionTaken.ts +29 -0
- package/src/inAppMessaging/providers/pinpoint/apis/onMessageDismissed.ts +29 -0
- package/src/inAppMessaging/providers/pinpoint/apis/onMessageDisplayed.ts +29 -0
- package/src/inAppMessaging/providers/pinpoint/apis/onMessageReceived.ts +29 -0
- package/src/inAppMessaging/providers/pinpoint/apis/setConflictHandler.ts +68 -0
- package/src/inAppMessaging/providers/pinpoint/apis/syncMessages.ts +6 -5
- package/src/inAppMessaging/providers/pinpoint/index.ts +12 -1
- package/src/inAppMessaging/providers/pinpoint/types/index.ts +14 -1
- package/src/inAppMessaging/providers/pinpoint/types/inputs.ts +49 -2
- package/src/inAppMessaging/providers/pinpoint/types/outputs.ts +24 -0
- package/src/inAppMessaging/providers/pinpoint/types/types.ts +9 -1
- package/src/inAppMessaging/providers/pinpoint/utils/helpers.ts +325 -0
- package/src/inAppMessaging/providers/pinpoint/utils/index.ts +6 -0
- package/src/inAppMessaging/providers/pinpoint/utils/messageProcessingHelpers.ts +196 -0
- package/src/inAppMessaging/types/index.ts +7 -1
- package/src/inAppMessaging/utils/index.ts +8 -0
- package/src/inAppMessaging/utils/statusHelpers.ts +32 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aws-amplify/notifications",
|
|
3
|
-
"version": "2.0.1-console-preview.
|
|
3
|
+
"version": "2.0.1-console-preview.23dc225.0+23dc225",
|
|
4
4
|
"description": "Notifications category of aws-amplify",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"module": "./lib-esm/index.js",
|
|
@@ -92,19 +92,18 @@
|
|
|
92
92
|
"src"
|
|
93
93
|
],
|
|
94
94
|
"dependencies": {
|
|
95
|
-
"lodash": "^4.17.21"
|
|
96
|
-
"uuid": "^9.0.0"
|
|
95
|
+
"lodash": "^4.17.21"
|
|
97
96
|
},
|
|
98
97
|
"peerDependencies": {
|
|
99
|
-
"@aws-amplify/core": "6.0.1-console-preview.
|
|
98
|
+
"@aws-amplify/core": "6.0.1-console-preview.23dc225.0+23dc225"
|
|
100
99
|
},
|
|
101
100
|
"devDependencies": {
|
|
102
|
-
"@aws-amplify/core": "6.0.1-console-preview.
|
|
101
|
+
"@aws-amplify/core": "6.0.1-console-preview.23dc225.0+23dc225",
|
|
103
102
|
"@babel/core": "7.15.5",
|
|
104
103
|
"@babel/plugin-proposal-class-properties": "^7.0.0",
|
|
105
104
|
"@babel/plugin-proposal-private-methods": "^7.0.0",
|
|
106
105
|
"@babel/plugin-proposal-private-property-in-object": "^7.0.0",
|
|
107
106
|
"metro-react-native-babel-preset": "^0.66.2"
|
|
108
107
|
},
|
|
109
|
-
"gitHead": "
|
|
108
|
+
"gitHead": "23dc22531b963319f2b421f6f7c6cf867635b20e"
|
|
110
109
|
}
|
|
@@ -3,12 +3,12 @@
|
|
|
3
3
|
|
|
4
4
|
import {
|
|
5
5
|
Category,
|
|
6
|
-
ClientDevice,
|
|
7
6
|
ConsoleLogger,
|
|
8
7
|
CustomUserAgentDetails,
|
|
9
8
|
getAmplifyUserAgent,
|
|
10
9
|
InAppMessagingAction,
|
|
11
10
|
PushNotificationAction,
|
|
11
|
+
amplifyUuid,
|
|
12
12
|
} from '@aws-amplify/core/internals/utils';
|
|
13
13
|
import { Cache, fetchAuthSession } from '@aws-amplify/core';
|
|
14
14
|
|
|
@@ -19,7 +19,6 @@ import {
|
|
|
19
19
|
updateEndpoint,
|
|
20
20
|
UpdateEndpointInput,
|
|
21
21
|
} from '@aws-amplify/core/internals/aws-clients/pinpoint';
|
|
22
|
-
import { v4 as uuid } from 'uuid';
|
|
23
22
|
|
|
24
23
|
import {
|
|
25
24
|
NotificationsCategory,
|
|
@@ -35,15 +34,12 @@ export default abstract class AWSPinpointProviderCommon
|
|
|
35
34
|
static category: NotificationsCategory = 'Notifications';
|
|
36
35
|
static providerName = 'AWSPinpoint';
|
|
37
36
|
|
|
38
|
-
protected clientInfo;
|
|
39
37
|
protected config: Record<string, any> = {};
|
|
40
38
|
protected endpointInitialized = false;
|
|
41
39
|
protected initialized = false;
|
|
42
40
|
protected logger: ConsoleLogger;
|
|
43
41
|
|
|
44
42
|
constructor(logger) {
|
|
45
|
-
// this.config = { storage: new StorageHelper().getStorage() };
|
|
46
|
-
this.clientInfo = ClientDevice.clientInfo() ?? {};
|
|
47
43
|
this.logger = logger;
|
|
48
44
|
}
|
|
49
45
|
|
|
@@ -140,7 +136,7 @@ export default abstract class AWSPinpointProviderCommon
|
|
|
140
136
|
[endpointId]: {
|
|
141
137
|
Endpoint: {},
|
|
142
138
|
Events: {
|
|
143
|
-
[
|
|
139
|
+
[amplifyUuid()]: event,
|
|
144
140
|
},
|
|
145
141
|
},
|
|
146
142
|
},
|
|
@@ -185,14 +181,13 @@ export default abstract class AWSPinpointProviderCommon
|
|
|
185
181
|
try {
|
|
186
182
|
const { address, attributes, demographic, location, metrics, optOut } =
|
|
187
183
|
userInfo ?? {};
|
|
188
|
-
const { appVersion, make, model, platform, version } = this.clientInfo;
|
|
189
184
|
// Create the UpdateEndpoint input, prioritizing passed in user info and falling back to
|
|
190
185
|
// defaults (if any) obtained from the config
|
|
191
186
|
const input: UpdateEndpointInput = {
|
|
192
187
|
ApplicationId: appId,
|
|
193
188
|
EndpointId: endpointId,
|
|
194
189
|
EndpointRequest: {
|
|
195
|
-
RequestId:
|
|
190
|
+
RequestId: amplifyUuid(),
|
|
196
191
|
EffectiveDate: new Date().toISOString(),
|
|
197
192
|
ChannelType: endpointInfo.channelType,
|
|
198
193
|
Address: address ?? endpointInfo.address,
|
|
@@ -201,11 +196,11 @@ export default abstract class AWSPinpointProviderCommon
|
|
|
201
196
|
...attributes,
|
|
202
197
|
},
|
|
203
198
|
Demographic: {
|
|
204
|
-
AppVersion:
|
|
205
|
-
Make:
|
|
206
|
-
Model:
|
|
207
|
-
ModelVersion:
|
|
208
|
-
Platform:
|
|
199
|
+
AppVersion: null,
|
|
200
|
+
Make: null,
|
|
201
|
+
Model: null,
|
|
202
|
+
ModelVersion: null,
|
|
203
|
+
Platform: null,
|
|
209
204
|
...this.transferKeyToUpperCase({
|
|
210
205
|
...endpointInfo.demographic,
|
|
211
206
|
...demographic,
|
|
@@ -252,7 +247,7 @@ export default abstract class AWSPinpointProviderCommon
|
|
|
252
247
|
return cachedEndpointId;
|
|
253
248
|
}
|
|
254
249
|
// Otherwise, generate a new ID and store it in long-lived cache before returning it
|
|
255
|
-
const endpointId =
|
|
250
|
+
const endpointId = amplifyUuid();
|
|
256
251
|
// Set a longer TTL to avoid endpoint id being deleted after the default TTL (3 days)
|
|
257
252
|
// Also set its priority to the highest to reduce its chance of being deleted when cache is full
|
|
258
253
|
const ttl = 1000 * 60 * 60 * 24 * 365 * 100; // 100 years
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
|
-
import { EventListener, EventType } from './types';
|
|
4
|
+
import { EventListener, EventListenerRemover, EventType } from './types';
|
|
5
5
|
|
|
6
6
|
const eventListeners: Record<string, Set<EventListener<Function>>> = {};
|
|
7
7
|
|
|
@@ -28,7 +28,7 @@ export const notifyEventListenersAndAwaitHandlers = (
|
|
|
28
28
|
export const addEventListener = <EventHandler extends Function>(
|
|
29
29
|
type: EventType,
|
|
30
30
|
handler: EventHandler
|
|
31
|
-
):
|
|
31
|
+
): EventListenerRemover => {
|
|
32
32
|
// If there is no listener set for the event type, just create it
|
|
33
33
|
if (!eventListeners[type]) {
|
|
34
34
|
eventListeners[type] = new Set<EventListener<EventHandler>>();
|
|
@@ -40,5 +40,7 @@ export const addEventListener = <EventHandler extends Function>(
|
|
|
40
40
|
},
|
|
41
41
|
};
|
|
42
42
|
eventListeners[type].add(listener);
|
|
43
|
-
return
|
|
43
|
+
return {
|
|
44
|
+
remove: () => listener.remove(),
|
|
45
|
+
};
|
|
44
46
|
};
|
package/src/common/index.ts
CHANGED
|
@@ -7,4 +7,8 @@ export {
|
|
|
7
7
|
notifyEventListeners,
|
|
8
8
|
notifyEventListenersAndAwaitHandlers,
|
|
9
9
|
} from './eventListeners';
|
|
10
|
-
export {
|
|
10
|
+
export {
|
|
11
|
+
EventListener,
|
|
12
|
+
EventType,
|
|
13
|
+
EventListenerRemover,
|
|
14
|
+
} from './eventListeners/types';
|
|
@@ -8,6 +8,7 @@ export enum InAppMessagingValidationErrorCode {
|
|
|
8
8
|
NoCredentials = 'NoCredentials',
|
|
9
9
|
NoRegion = 'NoRegion',
|
|
10
10
|
NoEndpointId = 'NoEndpointId',
|
|
11
|
+
NotInitialized = 'NotInitialized',
|
|
11
12
|
}
|
|
12
13
|
|
|
13
14
|
export const validationErrorMap: AmplifyErrorMap<InAppMessagingValidationErrorCode> =
|
|
@@ -24,4 +25,9 @@ export const validationErrorMap: AmplifyErrorMap<InAppMessagingValidationErrorCo
|
|
|
24
25
|
[InAppMessagingValidationErrorCode.NoEndpointId]: {
|
|
25
26
|
message: 'Could not find or create EndpointId.',
|
|
26
27
|
},
|
|
28
|
+
[InAppMessagingValidationErrorCode.NotInitialized]: {
|
|
29
|
+
message: 'In-app messaging has not been initialized.',
|
|
30
|
+
recoverySuggestion:
|
|
31
|
+
'Please make sure to first call `initializePushNotifications`.',
|
|
32
|
+
},
|
|
27
33
|
};
|
|
@@ -1,4 +1,15 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
|
-
export {
|
|
4
|
+
export {
|
|
5
|
+
identifyUser,
|
|
6
|
+
syncMessages,
|
|
7
|
+
dispatchEvent,
|
|
8
|
+
setConflictHandler,
|
|
9
|
+
initializeInAppMessaging,
|
|
10
|
+
onMessageReceived,
|
|
11
|
+
onMessageDisplayed,
|
|
12
|
+
onMessageDismissed,
|
|
13
|
+
onMessageActionTaken,
|
|
14
|
+
notifyMessageInteraction,
|
|
15
|
+
} from './providers/pinpoint';
|
|
@@ -1,4 +1,15 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
|
-
export {
|
|
4
|
+
export {
|
|
5
|
+
identifyUser,
|
|
6
|
+
syncMessages,
|
|
7
|
+
dispatchEvent,
|
|
8
|
+
setConflictHandler,
|
|
9
|
+
initializeInAppMessaging,
|
|
10
|
+
onMessageReceived,
|
|
11
|
+
onMessageDisplayed,
|
|
12
|
+
onMessageDismissed,
|
|
13
|
+
onMessageActionTaken,
|
|
14
|
+
notifyMessageInteraction,
|
|
15
|
+
} from './pinpoint/apis';
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
PINPOINT_KEY_PREFIX,
|
|
6
|
+
STORAGE_KEY_SUFFIX,
|
|
7
|
+
processInAppMessages,
|
|
8
|
+
} from '../utils';
|
|
9
|
+
import { InAppMessage } from '../../../types';
|
|
10
|
+
import flatten from 'lodash/flatten';
|
|
11
|
+
import { defaultStorage } from '@aws-amplify/core';
|
|
12
|
+
import { notifyEventListeners } from '../../../../common';
|
|
13
|
+
import { assertServiceError } from '../../../errors';
|
|
14
|
+
import { DispatchEventInput } from '../types';
|
|
15
|
+
import { syncMessages } from './syncMessages';
|
|
16
|
+
import { conflictHandler, setConflictHandler } from './setConflictHandler';
|
|
17
|
+
import { assertIsInitialized } from '../../../utils';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Triggers an In-App message to be displayed. Use this after your campaigns have been synced to the device using
|
|
21
|
+
* {@link syncMessages}. Based on the messages synced and the event passed to this API, it triggers the display
|
|
22
|
+
* of the In-App message that meets the criteria.
|
|
23
|
+
*
|
|
24
|
+
* @remark
|
|
25
|
+
* If an event would trigger multiple messages, the message closest to expiry will be chosen by default.
|
|
26
|
+
* To change this behavior, you can use the {@link setConflictHandler} API to provide
|
|
27
|
+
* your own logic for resolving message conflicts.
|
|
28
|
+
*
|
|
29
|
+
* @param DispatchEventInput The input object that holds the event to be dispatched.
|
|
30
|
+
* @throws validation: {@link InAppMessagingValidationErrorCode} - Thrown when the provided parameters or library
|
|
31
|
+
* configuration is incorrect, or if In App messaging hasn't been initialized.
|
|
32
|
+
* @throws service exceptions - Thrown when the underlying Pinpoint service returns an error.
|
|
33
|
+
* @returns A promise that will resolve when the operation is complete.
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* // Sync message before disptaching an event
|
|
37
|
+
* await syncMessages();
|
|
38
|
+
*
|
|
39
|
+
* // Dispatch an event
|
|
40
|
+
* await dispatchEvent({ name: 'test_event' });
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export async function dispatchEvent(input: DispatchEventInput): Promise<void> {
|
|
44
|
+
assertIsInitialized();
|
|
45
|
+
try {
|
|
46
|
+
const key = `${PINPOINT_KEY_PREFIX}${STORAGE_KEY_SUFFIX}`;
|
|
47
|
+
const cachedMessages = await defaultStorage.getItem(key);
|
|
48
|
+
const messages: InAppMessage[] = await processInAppMessages(
|
|
49
|
+
cachedMessages ? JSON.parse(cachedMessages) : [],
|
|
50
|
+
input
|
|
51
|
+
);
|
|
52
|
+
const flattenedMessages = flatten(messages);
|
|
53
|
+
if (flattenedMessages.length > 0) {
|
|
54
|
+
notifyEventListeners(
|
|
55
|
+
'messageReceived',
|
|
56
|
+
conflictHandler(flattenedMessages)
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
} catch (error) {
|
|
60
|
+
assertServiceError(error);
|
|
61
|
+
throw error;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
resolveCredentials,
|
|
13
13
|
} from '../utils';
|
|
14
14
|
import { IdentifyUserInput } from '../types';
|
|
15
|
+
import { assertIsInitialized } from '../../../utils';
|
|
15
16
|
|
|
16
17
|
/**
|
|
17
18
|
* Sends information about a user to Pinpoint. Sending user information allows you to associate a user to their user
|
|
@@ -20,13 +21,10 @@ import { IdentifyUserInput } from '../types';
|
|
|
20
21
|
*
|
|
21
22
|
* @param {IdentifyUserParameters} params The input object used to construct requests sent to Pinpoint's UpdateEndpoint
|
|
22
23
|
* API.
|
|
23
|
-
*
|
|
24
24
|
* @throws service: {@link UpdateEndpointException} - Thrown when the underlying Pinpoint service returns an error.
|
|
25
25
|
* @throws validation: {@link InAppMessagingValidationErrorCode} - Thrown when the provided parameters or library
|
|
26
|
-
*
|
|
27
|
-
*
|
|
26
|
+
* configuration is incorrect, or if In App messaging hasn't been initialized.
|
|
28
27
|
* @returns A promise that will resolve when the operation is complete.
|
|
29
|
-
*
|
|
30
28
|
* @example
|
|
31
29
|
* ```ts
|
|
32
30
|
* // Identify a user with Pinpoint
|
|
@@ -72,6 +70,7 @@ export const identifyUser = async ({
|
|
|
72
70
|
userProfile,
|
|
73
71
|
options,
|
|
74
72
|
}: IdentifyUserInput): Promise<void> => {
|
|
73
|
+
assertIsInitialized();
|
|
75
74
|
const { credentials, identityId } = await resolveCredentials();
|
|
76
75
|
const { appId, region } = resolveConfig();
|
|
77
76
|
const { address, optOut, userAttributes } = options?.serviceOptions ?? {};
|
|
@@ -3,3 +3,11 @@
|
|
|
3
3
|
|
|
4
4
|
export { identifyUser } from './identifyUser';
|
|
5
5
|
export { syncMessages } from './syncMessages';
|
|
6
|
+
export { dispatchEvent } from './dispatchEvent';
|
|
7
|
+
export { setConflictHandler } from './setConflictHandler';
|
|
8
|
+
export { initializeInAppMessaging } from './initializeInAppMessaging';
|
|
9
|
+
export { onMessageReceived } from './onMessageReceived';
|
|
10
|
+
export { onMessageDismissed } from './onMessageDismissed';
|
|
11
|
+
export { onMessageDisplayed } from './onMessageDisplayed';
|
|
12
|
+
export { onMessageActionTaken } from './onMessageActionTaken';
|
|
13
|
+
export { notifyMessageInteraction } from './notifyMessageInteraction';
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import SessionTracker from '../../../sessionTracker';
|
|
5
|
+
import { InAppMessage, InAppMessagingEvent } from '../../../types';
|
|
6
|
+
import { addEventListener } from '../../../../common';
|
|
7
|
+
import { recordAnalyticsEvent } from '../utils/helpers';
|
|
8
|
+
import { PinpointMessageEvent } from '../types';
|
|
9
|
+
import { Hub, HubCapsule } from '@aws-amplify/core';
|
|
10
|
+
import { dispatchEvent } from './dispatchEvent';
|
|
11
|
+
import { incrementMessageCounts, sessionStateChangeHandler } from '../utils';
|
|
12
|
+
import { isInitialized, initialize } from '../../../utils';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Initialize and set up in-app messaging category. This API needs to be called to enable other InAppMessaging APIs.
|
|
16
|
+
*
|
|
17
|
+
* @remarks
|
|
18
|
+
* Make sure to call this early in your app at the root entry point after configuring Amplify.
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* Amplify.configure(config);
|
|
22
|
+
* initializeInAppMessaging();
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export function initializeInAppMessaging(): void {
|
|
26
|
+
if (isInitialized()) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
// set up the session tracker and start it
|
|
30
|
+
const sessionTracker = new SessionTracker(sessionStateChangeHandler);
|
|
31
|
+
sessionTracker.start();
|
|
32
|
+
|
|
33
|
+
// wire up default Pinpoint message event handling
|
|
34
|
+
addEventListener('messageDisplayed', (message: InAppMessage) => {
|
|
35
|
+
console.log('Recording message displayed event');
|
|
36
|
+
recordAnalyticsEvent(PinpointMessageEvent.MESSAGE_DISPLAYED, message);
|
|
37
|
+
incrementMessageCounts(message.id);
|
|
38
|
+
});
|
|
39
|
+
addEventListener('messageDismissed', (message: InAppMessage) => {
|
|
40
|
+
recordAnalyticsEvent(PinpointMessageEvent.MESSAGE_DISMISSED, message);
|
|
41
|
+
});
|
|
42
|
+
addEventListener('messageActionTaken', (message: InAppMessage) => {
|
|
43
|
+
recordAnalyticsEvent(PinpointMessageEvent.MESSAGE_ACTION_TAKEN, message);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// listen to analytics hub events
|
|
47
|
+
Hub.listen('analytics', analyticsListener);
|
|
48
|
+
|
|
49
|
+
initialize();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function analyticsListener({
|
|
53
|
+
payload,
|
|
54
|
+
}: HubCapsule<string, { event: string; data: InAppMessagingEvent }>) {
|
|
55
|
+
const { event, data } = payload;
|
|
56
|
+
switch (event) {
|
|
57
|
+
case 'record': {
|
|
58
|
+
dispatchEvent(data);
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
default:
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { notifyEventListeners } from '../../../../common';
|
|
5
|
+
import { assertIsInitialized } from '../../../utils';
|
|
6
|
+
import { NotifyMessageInteractionInput } from '../types/inputs';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Notifies the respective listener of the specified type with the message given.
|
|
10
|
+
*
|
|
11
|
+
* @param {NotifyMessageInteractionInput} input - The input object that holds the type and message.
|
|
12
|
+
* @throws validation: {@link InAppMessagingValidationErrorCode} - Thrown when the provided parameters or library
|
|
13
|
+
* configuration is incorrect, or if In App messaging hasn't been initialized.
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* onMessageRecieved((message) => {
|
|
17
|
+
* // Show end users the In-App message and notify event listeners
|
|
18
|
+
* notifyMessageInteraction({ type: 'messageDisplayed', message });
|
|
19
|
+
* });
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export function notifyMessageInteraction({
|
|
23
|
+
type,
|
|
24
|
+
message,
|
|
25
|
+
}: NotifyMessageInteractionInput): void {
|
|
26
|
+
assertIsInitialized();
|
|
27
|
+
notifyEventListeners(type, message);
|
|
28
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { addEventListener } from '../../../../common';
|
|
5
|
+
import { assertIsInitialized } from '../../../utils';
|
|
6
|
+
import { OnMessageActionTakenInput } from '../types/inputs';
|
|
7
|
+
import { OnMessageActionTakenOutput } from '../types/outputs';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Registers a callback that will be invoked on `messageActionTaken` events.
|
|
11
|
+
*
|
|
12
|
+
* @param {OnMessageActionTakenInput} input - The input object that holds the callback handler.
|
|
13
|
+
* @throws validation: {@link InAppMessagingValidationErrorCode} - Thrown when the provided parameters or library
|
|
14
|
+
* configuration is incorrect, or if In App messaging hasn't been initialized.
|
|
15
|
+
* @returns {OnMessageActionTakenOutput} - An object that holds a remove method to stop listening to events.
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* onMessageActionTaken((message) => {
|
|
19
|
+
* // use the message
|
|
20
|
+
* console.log(message.id);
|
|
21
|
+
* });
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export function onMessageActionTaken(
|
|
25
|
+
input: OnMessageActionTakenInput
|
|
26
|
+
): OnMessageActionTakenOutput {
|
|
27
|
+
assertIsInitialized();
|
|
28
|
+
return addEventListener('messageActionTaken', input);
|
|
29
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { addEventListener } from '../../../../common';
|
|
5
|
+
import { OnMessageDismissedOutput } from '../types/outputs';
|
|
6
|
+
import { OnMessageDismissedInput } from '../types/inputs';
|
|
7
|
+
import { assertIsInitialized } from '../../../utils';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Registers a callback that will be invoked on `messageDismissed` events.
|
|
11
|
+
*
|
|
12
|
+
* @param {OnMessageDismissedInput} input - The input object that holds the callback handler.
|
|
13
|
+
* @throws validation: {@link InAppMessagingValidationErrorCode} - Thrown when the provided parameters or library
|
|
14
|
+
* configuration is incorrect, or if In App messaging hasn't been initialized.
|
|
15
|
+
* @returns {OnMessageDismissedOutput} - An object that holds a remove method to stop listening to events.
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* onMessageDismissed((message) => {
|
|
19
|
+
* // use the message
|
|
20
|
+
* console.log(message.id);
|
|
21
|
+
* });
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export function onMessageDismissed(
|
|
25
|
+
input: OnMessageDismissedInput
|
|
26
|
+
): OnMessageDismissedOutput {
|
|
27
|
+
assertIsInitialized();
|
|
28
|
+
return addEventListener('messageDismissed', input);
|
|
29
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { addEventListener } from '../../../../common';
|
|
5
|
+
import { OnMessageDisplayedOutput } from '../types/outputs';
|
|
6
|
+
import { OnMessageDisplayedInput } from '../types/inputs';
|
|
7
|
+
import { assertIsInitialized } from '../../../utils';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Registers a callback that will be invoked on `messageDisplayed` events.
|
|
11
|
+
*
|
|
12
|
+
* @param {OnMessageDisplayedInput} input - The input object that holds the callback handler.
|
|
13
|
+
* @throws validation: {@link InAppMessagingValidationErrorCode} - Thrown when the provided parameters or library
|
|
14
|
+
* configuration is incorrect, or if In App messaging hasn't been initialized.
|
|
15
|
+
* @returns {OnMessageDismissedOutput} - An object that holds a remove method to stop listening to events.
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* onMessageDisplayed((message) => {
|
|
19
|
+
* // use the message
|
|
20
|
+
* console.log(message.id);
|
|
21
|
+
* });
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export function onMessageDisplayed(
|
|
25
|
+
input: OnMessageDisplayedInput
|
|
26
|
+
): OnMessageDisplayedOutput {
|
|
27
|
+
assertIsInitialized();
|
|
28
|
+
return addEventListener('messageDisplayed', input);
|
|
29
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { addEventListener } from '../../../../common';
|
|
5
|
+
import { assertIsInitialized } from '../../../utils';
|
|
6
|
+
import { OnMessageReceivedInput } from '../types/inputs';
|
|
7
|
+
import { OnMessageReceivedOutput } from '../types/outputs';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Registers a callback that will be invoked on `messageReceived` events.
|
|
11
|
+
*
|
|
12
|
+
* @param {OnMessageReceivedInput} input - The input object that holds the callback handler.
|
|
13
|
+
* @throws validation: {@link InAppMessagingValidationErrorCode} - Thrown when the provided parameters or library
|
|
14
|
+
* configuration is incorrect, or if In App messaging hasn't been initialized.
|
|
15
|
+
* @returns {OnMessageReceivedOutput} - An object that holds a remove method to stop listening to events.
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* onMessageReceived((message) => {
|
|
19
|
+
* // use the message
|
|
20
|
+
* console.log(message.id);
|
|
21
|
+
* });
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export function onMessageReceived(
|
|
25
|
+
input: OnMessageReceivedInput
|
|
26
|
+
): OnMessageReceivedOutput {
|
|
27
|
+
assertIsInitialized();
|
|
28
|
+
return addEventListener('messageReceived', input);
|
|
29
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { InAppMessage } from '../../../types';
|
|
5
|
+
import { assertIsInitialized } from '../../../utils';
|
|
6
|
+
import { InAppMessageConflictHandler, SetConflictHandlerInput } from '../types';
|
|
7
|
+
|
|
8
|
+
export let conflictHandler: InAppMessageConflictHandler =
|
|
9
|
+
defaultConflictHandler;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Set a conflict handler that will be used to resolve conflicts that may emerge
|
|
13
|
+
* when matching events with synced messages.
|
|
14
|
+
*
|
|
15
|
+
* @remark
|
|
16
|
+
* The conflict handler is not persisted across app restarts and so must be set again before dispatching an event for
|
|
17
|
+
* any custom handling to take effect.
|
|
18
|
+
* @throws validation: {@link InAppMessagingValidationErrorCode} - Thrown when the provided parameters or library
|
|
19
|
+
* configuration is incorrect, or if In App messaging hasn't been initialized.
|
|
20
|
+
* @param SetConflictHandlerInput: The input object that holds the conflict handler to be used.
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* // Sync messages before dispatching an event
|
|
24
|
+
* await syncMessages();
|
|
25
|
+
*
|
|
26
|
+
* // Example custom conflict handler
|
|
27
|
+
* const myConflictHandler = (messages) => {
|
|
28
|
+
* // Return a random message
|
|
29
|
+
* const randomIndex = Math.floor(Math.random() * messages.length);
|
|
30
|
+
* return messages[randomIndex];
|
|
31
|
+
* };
|
|
32
|
+
*
|
|
33
|
+
* // Set the conflict handler
|
|
34
|
+
* setConflictHandler(myConflictHandler);
|
|
35
|
+
*
|
|
36
|
+
* // Dispatch an event
|
|
37
|
+
* await dispatchEvent({ name: 'test_event' });
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export function setConflictHandler(input: SetConflictHandlerInput): void {
|
|
41
|
+
assertIsInitialized();
|
|
42
|
+
conflictHandler = input;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function defaultConflictHandler(messages: InAppMessage[]): InAppMessage {
|
|
46
|
+
// default behavior is to return the message closest to expiry
|
|
47
|
+
// this function assumes that messages processed by providers already filters out expired messages
|
|
48
|
+
const sorted = messages.sort((a, b) => {
|
|
49
|
+
const endDateA = a.metadata?.endDate;
|
|
50
|
+
const endDateB = b.metadata?.endDate;
|
|
51
|
+
// if both message end dates are falsy or have the same date string, treat them as equal
|
|
52
|
+
if (endDateA === endDateB) {
|
|
53
|
+
return 0;
|
|
54
|
+
}
|
|
55
|
+
// if only message A has an end date, treat it as closer to expiry
|
|
56
|
+
if (endDateA && !endDateB) {
|
|
57
|
+
return -1;
|
|
58
|
+
}
|
|
59
|
+
// if only message B has an end date, treat it as closer to expiry
|
|
60
|
+
if (!endDateA && endDateB) {
|
|
61
|
+
return 1;
|
|
62
|
+
}
|
|
63
|
+
// otherwise, compare them
|
|
64
|
+
return new Date(endDateA) < new Date(endDateB) ? -1 : 1;
|
|
65
|
+
});
|
|
66
|
+
// always return the top sorted
|
|
67
|
+
return sorted[0];
|
|
68
|
+
}
|
|
@@ -26,6 +26,7 @@ import {
|
|
|
26
26
|
assertServiceError,
|
|
27
27
|
assertValidationError,
|
|
28
28
|
} from '../../../errors';
|
|
29
|
+
import { assertIsInitialized } from '../../../utils';
|
|
29
30
|
|
|
30
31
|
/**
|
|
31
32
|
* Fetch and persist messages from Pinpoint campaigns.
|
|
@@ -33,10 +34,8 @@ import {
|
|
|
33
34
|
*
|
|
34
35
|
* @throws service exceptions - Thrown when the underlying Pinpoint service returns an error.
|
|
35
36
|
* @throws validation: {@link InAppMessagingValidationErrorCode} - Thrown when the provided parameters or library
|
|
36
|
-
*
|
|
37
|
-
*
|
|
37
|
+
* configuration is incorrect, or if In App messaging hasn't been initialized.
|
|
38
38
|
* @returns A promise that will resolve when the operation is complete.
|
|
39
|
-
*
|
|
40
39
|
* @example
|
|
41
40
|
* ```ts
|
|
42
41
|
* // Sync InApp messages with Pinpoint and device.
|
|
@@ -45,8 +44,9 @@ import {
|
|
|
45
44
|
* ```
|
|
46
45
|
*/
|
|
47
46
|
export async function syncMessages(): Promise<void> {
|
|
47
|
+
assertIsInitialized();
|
|
48
48
|
const messages = await fetchInAppMessages();
|
|
49
|
-
if (messages.length === 0) {
|
|
49
|
+
if (!messages || messages.length === 0) {
|
|
50
50
|
return;
|
|
51
51
|
}
|
|
52
52
|
try {
|
|
@@ -95,7 +95,8 @@ async function fetchInAppMessages() {
|
|
|
95
95
|
{ credentials, region },
|
|
96
96
|
input
|
|
97
97
|
);
|
|
98
|
-
const { InAppMessageCampaigns: messages } =
|
|
98
|
+
const { InAppMessageCampaigns: messages } =
|
|
99
|
+
response.InAppMessagesResponse ?? {};
|
|
99
100
|
return messages;
|
|
100
101
|
} catch (error) {
|
|
101
102
|
assertServiceError(error);
|
|
@@ -1,4 +1,15 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
|
-
export {
|
|
4
|
+
export {
|
|
5
|
+
identifyUser,
|
|
6
|
+
syncMessages,
|
|
7
|
+
dispatchEvent,
|
|
8
|
+
setConflictHandler,
|
|
9
|
+
initializeInAppMessaging,
|
|
10
|
+
onMessageReceived,
|
|
11
|
+
onMessageDisplayed,
|
|
12
|
+
onMessageDismissed,
|
|
13
|
+
onMessageActionTaken,
|
|
14
|
+
notifyMessageInteraction,
|
|
15
|
+
} from './apis';
|