@aws-amplify/notifications 1.0.25 → 1.0.26-unstable.1116fef.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (208) hide show
  1. package/lib/InAppMessaging/InAppMessaging.d.ts +10 -8
  2. package/lib/InAppMessaging/InAppMessaging.js +9 -11
  3. package/lib/InAppMessaging/InAppMessaging.js.map +1 -1
  4. package/lib/InAppMessaging/Providers/AWSPinpointProvider/index.d.ts +5 -25
  5. package/lib/InAppMessaging/Providers/AWSPinpointProvider/index.js +37 -252
  6. package/lib/InAppMessaging/Providers/AWSPinpointProvider/index.js.map +1 -1
  7. package/lib/InAppMessaging/Providers/AWSPinpointProvider/types.d.ts +0 -9
  8. package/lib/InAppMessaging/Providers/AWSPinpointProvider/types.js.map +1 -1
  9. package/lib/InAppMessaging/Providers/AWSPinpointProvider/utils.js +3 -9
  10. package/lib/InAppMessaging/Providers/AWSPinpointProvider/utils.js.map +1 -1
  11. package/lib/InAppMessaging/index.d.ts +1 -3
  12. package/lib/InAppMessaging/index.js +0 -2
  13. package/lib/InAppMessaging/index.js.map +1 -1
  14. package/lib/InAppMessaging/types.d.ts +30 -42
  15. package/lib/InAppMessaging/types.js +2 -0
  16. package/lib/InAppMessaging/types.js.map +1 -1
  17. package/lib/Notifications.d.ts +6 -2
  18. package/lib/Notifications.js +45 -0
  19. package/lib/Notifications.js.map +1 -1
  20. package/lib/PushNotification/NotEnabledError.d.ts +3 -0
  21. package/lib/PushNotification/NotEnabledError.js +16 -0
  22. package/lib/PushNotification/NotEnabledError.js.map +1 -0
  23. package/lib/PushNotification/Platform/index.d.ts +2 -0
  24. package/lib/PushNotification/Platform/index.js +26 -0
  25. package/lib/PushNotification/Platform/index.js.map +1 -0
  26. package/lib/PushNotification/Platform/index.native.d.ts +2 -0
  27. package/lib/PushNotification/Platform/index.native.js +8 -0
  28. package/lib/PushNotification/Platform/index.native.js.map +1 -0
  29. package/lib/PushNotification/Platform/types.d.ts +5 -0
  30. package/lib/PushNotification/Platform/types.js +5 -0
  31. package/lib/PushNotification/Platform/types.js.map +1 -0
  32. package/lib/PushNotification/PlatformNotSupportedError.d.ts +3 -0
  33. package/lib/PushNotification/PlatformNotSupportedError.js +16 -0
  34. package/lib/PushNotification/PlatformNotSupportedError.js.map +1 -0
  35. package/lib/PushNotification/Providers/AWSPinpointProvider/index.d.ts +15 -0
  36. package/lib/PushNotification/Providers/AWSPinpointProvider/index.js +115 -0
  37. package/lib/PushNotification/Providers/AWSPinpointProvider/index.js.map +1 -0
  38. package/lib/PushNotification/Providers/AWSPinpointProvider/types.d.ts +9 -0
  39. package/lib/PushNotification/Providers/AWSPinpointProvider/types.js +16 -0
  40. package/lib/PushNotification/Providers/AWSPinpointProvider/types.js.map +1 -0
  41. package/lib/PushNotification/Providers/AWSPinpointProvider/utils.d.ts +7 -0
  42. package/lib/PushNotification/Providers/AWSPinpointProvider/utils.js +58 -0
  43. package/lib/PushNotification/Providers/AWSPinpointProvider/utils.js.map +1 -0
  44. package/lib/PushNotification/Providers/index.d.ts +1 -0
  45. package/lib/PushNotification/Providers/index.js +7 -0
  46. package/lib/PushNotification/Providers/index.js.map +1 -0
  47. package/lib/PushNotification/PushNotification.d.ts +39 -0
  48. package/lib/PushNotification/PushNotification.js +91 -0
  49. package/lib/PushNotification/PushNotification.js.map +1 -0
  50. package/lib/PushNotification/PushNotification.native.d.ts +63 -0
  51. package/lib/PushNotification/PushNotification.native.js +327 -0
  52. package/lib/PushNotification/PushNotification.native.js.map +1 -0
  53. package/lib/PushNotification/index.d.ts +2 -0
  54. package/lib/PushNotification/index.js +9 -0
  55. package/lib/PushNotification/index.js.map +1 -0
  56. package/lib/PushNotification/types.d.ts +77 -0
  57. package/lib/PushNotification/types.js +12 -0
  58. package/lib/PushNotification/types.js.map +1 -0
  59. package/lib/PushNotification/utils.d.ts +3 -0
  60. package/lib/PushNotification/utils.js +81 -0
  61. package/lib/PushNotification/utils.js.map +1 -0
  62. package/lib/common/AWSPinpointProviderCommon/index.d.ts +34 -0
  63. package/lib/common/AWSPinpointProviderCommon/index.js +281 -0
  64. package/lib/common/AWSPinpointProviderCommon/index.js.map +1 -0
  65. package/lib/common/AWSPinpointProviderCommon/types.d.ts +9 -0
  66. package/lib/common/AWSPinpointProviderCommon/types.js +5 -0
  67. package/lib/common/AWSPinpointProviderCommon/types.js.map +1 -0
  68. package/lib/common/constants.d.ts +1 -0
  69. package/lib/common/constants.js +6 -0
  70. package/lib/common/constants.js.map +1 -0
  71. package/lib/common/eventListeners/index.d.ts +4 -0
  72. package/lib/common/eventListeners/index.js +55 -0
  73. package/lib/common/eventListeners/index.js.map +1 -0
  74. package/lib/common/eventListeners/types.d.ts +7 -0
  75. package/lib/common/eventListeners/types.js +5 -0
  76. package/lib/common/eventListeners/types.js.map +1 -0
  77. package/lib/common/index.d.ts +4 -0
  78. package/lib/common/index.js +13 -0
  79. package/lib/common/index.js.map +1 -0
  80. package/lib/index.d.ts +5 -2
  81. package/lib/index.js +4 -0
  82. package/lib/index.js.map +1 -1
  83. package/lib/types.d.ts +34 -2
  84. package/lib/types.js +2 -0
  85. package/lib/types.js.map +1 -1
  86. package/lib-esm/InAppMessaging/InAppMessaging.d.ts +10 -8
  87. package/lib-esm/InAppMessaging/InAppMessaging.js +9 -11
  88. package/lib-esm/InAppMessaging/InAppMessaging.js.map +1 -1
  89. package/lib-esm/InAppMessaging/Providers/AWSPinpointProvider/index.d.ts +5 -25
  90. package/lib-esm/InAppMessaging/Providers/AWSPinpointProvider/index.js +39 -254
  91. package/lib-esm/InAppMessaging/Providers/AWSPinpointProvider/index.js.map +1 -1
  92. package/lib-esm/InAppMessaging/Providers/AWSPinpointProvider/types.d.ts +0 -9
  93. package/lib-esm/InAppMessaging/Providers/AWSPinpointProvider/types.js.map +1 -1
  94. package/lib-esm/InAppMessaging/Providers/AWSPinpointProvider/utils.js +2 -8
  95. package/lib-esm/InAppMessaging/Providers/AWSPinpointProvider/utils.js.map +1 -1
  96. package/lib-esm/InAppMessaging/index.d.ts +1 -3
  97. package/lib-esm/InAppMessaging/index.js +0 -1
  98. package/lib-esm/InAppMessaging/index.js.map +1 -1
  99. package/lib-esm/InAppMessaging/types.d.ts +30 -42
  100. package/lib-esm/InAppMessaging/types.js +2 -0
  101. package/lib-esm/InAppMessaging/types.js.map +1 -1
  102. package/lib-esm/Notifications.d.ts +6 -2
  103. package/lib-esm/Notifications.js +48 -3
  104. package/lib-esm/Notifications.js.map +1 -1
  105. package/lib-esm/PushNotification/NotEnabledError.d.ts +3 -0
  106. package/lib-esm/PushNotification/NotEnabledError.js +14 -0
  107. package/lib-esm/PushNotification/NotEnabledError.js.map +1 -0
  108. package/lib-esm/PushNotification/Platform/index.d.ts +2 -0
  109. package/lib-esm/PushNotification/Platform/index.js +24 -0
  110. package/lib-esm/PushNotification/Platform/index.js.map +1 -0
  111. package/lib-esm/PushNotification/Platform/index.native.d.ts +2 -0
  112. package/lib-esm/PushNotification/Platform/index.native.js +6 -0
  113. package/lib-esm/PushNotification/Platform/index.native.js.map +1 -0
  114. package/lib-esm/PushNotification/Platform/types.d.ts +5 -0
  115. package/lib-esm/PushNotification/Platform/types.js +3 -0
  116. package/lib-esm/PushNotification/Platform/types.js.map +1 -0
  117. package/lib-esm/PushNotification/PlatformNotSupportedError.d.ts +3 -0
  118. package/lib-esm/PushNotification/PlatformNotSupportedError.js +14 -0
  119. package/lib-esm/PushNotification/PlatformNotSupportedError.js.map +1 -0
  120. package/lib-esm/PushNotification/Providers/AWSPinpointProvider/index.d.ts +15 -0
  121. package/lib-esm/PushNotification/Providers/AWSPinpointProvider/index.js +113 -0
  122. package/lib-esm/PushNotification/Providers/AWSPinpointProvider/index.js.map +1 -0
  123. package/lib-esm/PushNotification/Providers/AWSPinpointProvider/types.d.ts +9 -0
  124. package/lib-esm/PushNotification/Providers/AWSPinpointProvider/types.js +14 -0
  125. package/lib-esm/PushNotification/Providers/AWSPinpointProvider/types.js.map +1 -0
  126. package/lib-esm/PushNotification/Providers/AWSPinpointProvider/utils.d.ts +7 -0
  127. package/lib-esm/PushNotification/Providers/AWSPinpointProvider/utils.js +56 -0
  128. package/lib-esm/PushNotification/Providers/AWSPinpointProvider/utils.js.map +1 -0
  129. package/lib-esm/PushNotification/Providers/index.d.ts +1 -0
  130. package/lib-esm/PushNotification/Providers/index.js +4 -0
  131. package/lib-esm/PushNotification/Providers/index.js.map +1 -0
  132. package/lib-esm/PushNotification/PushNotification.d.ts +39 -0
  133. package/lib-esm/PushNotification/PushNotification.js +89 -0
  134. package/lib-esm/PushNotification/PushNotification.js.map +1 -0
  135. package/lib-esm/PushNotification/PushNotification.native.d.ts +63 -0
  136. package/lib-esm/PushNotification/PushNotification.native.js +325 -0
  137. package/lib-esm/PushNotification/PushNotification.native.js.map +1 -0
  138. package/lib-esm/PushNotification/index.d.ts +2 -0
  139. package/lib-esm/PushNotification/index.js +5 -0
  140. package/lib-esm/PushNotification/index.js.map +1 -0
  141. package/lib-esm/PushNotification/types.d.ts +77 -0
  142. package/lib-esm/PushNotification/types.js +10 -0
  143. package/lib-esm/PushNotification/types.js.map +1 -0
  144. package/lib-esm/PushNotification/utils.d.ts +3 -0
  145. package/lib-esm/PushNotification/utils.js +79 -0
  146. package/lib-esm/PushNotification/utils.js.map +1 -0
  147. package/lib-esm/common/AWSPinpointProviderCommon/index.d.ts +34 -0
  148. package/lib-esm/common/AWSPinpointProviderCommon/index.js +279 -0
  149. package/lib-esm/common/AWSPinpointProviderCommon/index.js.map +1 -0
  150. package/lib-esm/common/AWSPinpointProviderCommon/types.d.ts +9 -0
  151. package/lib-esm/common/AWSPinpointProviderCommon/types.js +3 -0
  152. package/lib-esm/common/AWSPinpointProviderCommon/types.js.map +1 -0
  153. package/lib-esm/common/constants.d.ts +1 -0
  154. package/lib-esm/common/constants.js +4 -0
  155. package/lib-esm/common/constants.js.map +1 -0
  156. package/lib-esm/common/eventListeners/index.d.ts +4 -0
  157. package/lib-esm/common/eventListeners/index.js +53 -0
  158. package/lib-esm/common/eventListeners/index.js.map +1 -0
  159. package/lib-esm/common/eventListeners/types.d.ts +7 -0
  160. package/lib-esm/common/eventListeners/types.js +3 -0
  161. package/lib-esm/common/eventListeners/types.js.map +1 -0
  162. package/lib-esm/common/index.d.ts +4 -0
  163. package/lib-esm/common/index.js +6 -0
  164. package/lib-esm/common/index.js.map +1 -0
  165. package/lib-esm/index.d.ts +5 -2
  166. package/lib-esm/index.js +2 -0
  167. package/lib-esm/index.js.map +1 -1
  168. package/lib-esm/types.d.ts +34 -2
  169. package/lib-esm/types.js +2 -0
  170. package/lib-esm/types.js.map +1 -1
  171. package/package.json +11 -50
  172. package/src/InAppMessaging/InAppMessaging.ts +27 -36
  173. package/src/InAppMessaging/Providers/AWSPinpointProvider/index.ts +45 -245
  174. package/src/InAppMessaging/Providers/AWSPinpointProvider/types.ts +0 -12
  175. package/src/InAppMessaging/Providers/AWSPinpointProvider/utils.ts +3 -12
  176. package/src/InAppMessaging/index.ts +0 -3
  177. package/src/InAppMessaging/types.ts +52 -56
  178. package/src/Notifications.ts +42 -4
  179. package/src/PushNotification/NotEnabledError.ts +11 -0
  180. package/src/PushNotification/Platform/index.native.ts +9 -0
  181. package/src/PushNotification/Platform/index.ts +22 -0
  182. package/src/PushNotification/Platform/types.ts +15 -0
  183. package/src/PushNotification/PlatformNotSupportedError.ts +9 -0
  184. package/src/PushNotification/Providers/AWSPinpointProvider/index.ts +137 -0
  185. package/src/PushNotification/Providers/AWSPinpointProvider/types.ts +13 -0
  186. package/src/PushNotification/Providers/AWSPinpointProvider/utils.ts +79 -0
  187. package/src/PushNotification/Providers/index.ts +4 -0
  188. package/src/PushNotification/PushNotification.native.ts +381 -0
  189. package/src/PushNotification/PushNotification.ts +107 -0
  190. package/src/PushNotification/index.ts +9 -0
  191. package/src/PushNotification/types.ts +114 -0
  192. package/src/PushNotification/utils.ts +112 -0
  193. package/src/common/AWSPinpointProviderCommon/index.ts +295 -0
  194. package/src/common/AWSPinpointProviderCommon/types.ts +14 -0
  195. package/src/common/constants.ts +5 -0
  196. package/src/common/eventListeners/index.ts +44 -0
  197. package/src/common/eventListeners/types.ts +12 -0
  198. package/src/common/index.ts +11 -0
  199. package/src/index.ts +9 -3
  200. package/src/types.ts +47 -2
  201. package/CHANGELOG.md +0 -215
  202. package/lib/InAppMessaging/eventListeners.d.ts +0 -3
  203. package/lib/InAppMessaging/eventListeners.js +0 -28
  204. package/lib/InAppMessaging/eventListeners.js.map +0 -1
  205. package/lib-esm/InAppMessaging/eventListeners.d.ts +0 -3
  206. package/lib-esm/InAppMessaging/eventListeners.js +0 -26
  207. package/lib-esm/InAppMessaging/eventListeners.js.map +0 -1
  208. package/src/InAppMessaging/eventListeners.ts +0 -42
@@ -1,58 +1,51 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- import { NotificationsCategory } from '../types';
4
- import { AWSPinpointProviderConfig } from './Providers/AWSPinpointProvider/types';
5
-
6
- export type NotificationsSubcategory = 'InAppMessaging';
7
-
8
- export type UserInfo = {
9
- attributes?: Record<string, string[]>;
10
- demographic?: {
11
- appVersion?: string;
12
- locale?: string;
13
- make?: string;
14
- model?: string;
15
- modelVersion?: string;
16
- platform?: string;
17
- platformVersion?: string;
18
- timezone?: string;
19
- };
20
- location?: {
21
- city?: string;
22
- country?: string;
23
- latitude?: number;
24
- longitude?: number;
25
- postalCode?: string;
26
- region?: string;
27
- };
28
- metrics?: Record<string, number>;
29
- };
30
-
31
- export type InAppMessagingEvent = {
32
- name: string;
33
- attributes?: Record<string, string>;
34
- metrics?: Record<string, number>;
35
- };
36
3
 
37
- export interface InAppMessagingConfig {
38
- listenForAnalyticsEvents?: boolean;
39
- AWSPinpoint?: AWSPinpointProviderConfig;
4
+ import { EventListener } from '../common';
5
+ import { AWSPinpointProviderConfig } from '../common/AWSPinpointProviderCommon/types';
6
+ import {
7
+ NotificationsProvider,
8
+ NotificationsSubCategory as NotificationsSubCategories,
9
+ UserInfo,
10
+ } from '../types';
11
+
12
+ export type NotificationsSubCategory = Extract<
13
+ NotificationsSubCategories,
14
+ 'InAppMessaging'
15
+ >;
16
+
17
+ export interface InAppMessagingInterface {
18
+ configure: (config: InAppMessagingConfig) => InAppMessagingConfig;
19
+ getModuleName: () => NotificationsSubCategory;
20
+ getPluggable: (providerName: string) => InAppMessagingProvider;
21
+ addPluggable: (pluggable: InAppMessagingProvider) => void;
22
+ removePluggable: (providerName: string) => void;
23
+ syncMessages: () => Promise<void[]>;
24
+ clearMessages: () => Promise<void[]>;
25
+ dispatchEvent: (event: InAppMessagingEvent) => Promise<void>;
26
+ identifyUser: (userId: string, userInfo: UserInfo) => Promise<void[]>;
27
+ onMessageReceived: (
28
+ handler: OnMessageInteractionEventHandler
29
+ ) => EventListener<OnMessageInteractionEventHandler>;
30
+ onMessageDisplayed: (
31
+ handler: OnMessageInteractionEventHandler
32
+ ) => EventListener<OnMessageInteractionEventHandler>;
33
+ onMessageDismissed: (
34
+ handler: OnMessageInteractionEventHandler
35
+ ) => EventListener<OnMessageInteractionEventHandler>;
36
+ onMessageActionTaken: (
37
+ handler: OnMessageInteractionEventHandler
38
+ ) => EventListener<OnMessageInteractionEventHandler>;
39
+ notifyMessageInteraction: (
40
+ message: InAppMessage,
41
+ type: InAppMessageInteractionEvent
42
+ ) => void;
43
+ setConflictHandler: (handler: InAppMessageConflictHandler) => void;
40
44
  }
41
45
 
42
- export interface InAppMessagingProvider {
43
- // you need to implement these methods
44
-
45
- // configure your provider
46
- configure(config: object): object;
47
-
48
- // return category ('Notifications')
49
- getCategory(): NotificationsCategory;
50
-
46
+ export interface InAppMessagingProvider extends NotificationsProvider {
51
47
  // return sub-category ('InAppMessaging')
52
- getSubCategory(): NotificationsSubcategory;
53
-
54
- // return the name of you provider
55
- getProviderName(): string;
48
+ getSubCategory(): NotificationsSubCategory;
56
49
 
57
50
  // get in-app messages from provider
58
51
  getInAppMessages(): Promise<any>;
@@ -62,11 +55,19 @@ export interface InAppMessagingProvider {
62
55
  messages: InAppMessage[],
63
56
  event: InAppMessagingEvent
64
57
  ): Promise<InAppMessage[]>;
58
+ }
65
59
 
66
- // identify the current user with the provider
67
- identifyUser(userId: string, userInfo: UserInfo): Promise<void>;
60
+ export interface InAppMessagingConfig {
61
+ listenForAnalyticsEvents?: boolean;
62
+ AWSPinpoint?: AWSPinpointProviderConfig;
68
63
  }
69
64
 
65
+ export type InAppMessagingEvent = {
66
+ name: string;
67
+ attributes?: Record<string, string>;
68
+ metrics?: Record<string, number>;
69
+ };
70
+
70
71
  export type InAppMessageLayout =
71
72
  | 'BOTTOM_BANNER'
72
73
  | 'CAROUSEL'
@@ -129,11 +130,6 @@ export interface InAppMessage {
129
130
 
130
131
  export type OnMessageInteractionEventHandler = (message: InAppMessage) => any;
131
132
 
132
- export interface OnMessageInteractionEventListener {
133
- handleEvent: OnMessageInteractionEventHandler;
134
- remove: () => void;
135
- }
136
-
137
133
  export enum InAppMessageInteractionEvent {
138
134
  MESSAGE_RECEIVED = 'MESSAGE_RECEIVED_EVENT',
139
135
  MESSAGE_DISPLAYED = 'MESSAGE_DISPLAYED_EVENT',
@@ -2,17 +2,21 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
4
  import { Amplify, ConsoleLogger as Logger } from '@aws-amplify/core';
5
- import InAppMessaging from './InAppMessaging';
6
- import { NotificationsCategory, NotificationsConfig } from './types';
5
+
6
+ import InAppMessagingClass from './InAppMessaging';
7
+ import { InAppMessagingInterface as InAppMessaging } from './InAppMessaging/types';
8
+ import { PushNotificationInterface as PushNotification } from './PushNotification/types';
9
+ import { NotificationsCategory, NotificationsConfig, UserInfo } from './types';
7
10
 
8
11
  const logger = new Logger('Notifications');
9
12
 
10
13
  class NotificationsClass {
11
14
  private config: Record<string, any> = {};
12
15
  private inAppMessaging: InAppMessaging;
16
+ private pushNotification?: PushNotification;
13
17
 
14
18
  constructor() {
15
- this.inAppMessaging = new InAppMessaging();
19
+ this.inAppMessaging = new InAppMessagingClass();
16
20
  }
17
21
 
18
22
  /**
@@ -35,12 +39,46 @@ class NotificationsClass {
35
39
  // Configure sub-categories
36
40
  this.inAppMessaging.configure(this.config.InAppMessaging);
37
41
 
42
+ if (this.config.Push) {
43
+ try {
44
+ const PushNotification = require('./PushNotification').default;
45
+ this.pushNotification = new PushNotification();
46
+ this.pushNotification.configure(this.config.Push);
47
+ } catch (err) {
48
+ logger.error(err);
49
+ }
50
+ }
51
+
38
52
  return this.config;
39
53
  };
40
54
 
41
- get InAppMessaging() {
55
+ get InAppMessaging(): InAppMessaging {
42
56
  return this.inAppMessaging;
43
57
  }
58
+
59
+ get Push(): PushNotification {
60
+ return this.pushNotification ?? ({} as PushNotification);
61
+ }
62
+
63
+ identifyUser = (userId: string, userInfo: UserInfo): Promise<void[]> => {
64
+ const identifyFunctions: Function[] = [];
65
+ if (this.inAppMessaging) {
66
+ identifyFunctions.push(this.inAppMessaging.identifyUser);
67
+ }
68
+ if (this.pushNotification) {
69
+ identifyFunctions.push(this.pushNotification.identifyUser);
70
+ }
71
+ return Promise.all<void>(
72
+ identifyFunctions.map(async identifyFunction => {
73
+ try {
74
+ await identifyFunction(userId, userInfo);
75
+ } catch (err) {
76
+ logger.error('Failed to identify user', err);
77
+ throw err;
78
+ }
79
+ })
80
+ );
81
+ };
44
82
  }
45
83
 
46
84
  const Notifications = new NotificationsClass();
@@ -0,0 +1,11 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export default class NotEnabledError extends Error {
5
+ constructor() {
6
+ super(
7
+ 'Function is unavailable as Push has not been enabled. Please call `Push.enable` before calling this function.'
8
+ );
9
+ this.name = 'NotEnabledError';
10
+ }
11
+ }
@@ -0,0 +1,9 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { Platform as NativePlatform } from 'react-native';
5
+ import { Platform as PlatformInterface } from './types';
6
+
7
+ const { OS } = NativePlatform;
8
+
9
+ export const Platform: PlatformInterface = { OS };
@@ -0,0 +1,22 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { Platform as PlatformInterface } from './types';
5
+
6
+ const userAgentIncludes = (text: string): boolean => {
7
+ return navigator?.userAgent?.includes(text);
8
+ };
9
+
10
+ const getOS = (): PlatformInterface['OS'] => {
11
+ if (userAgentIncludes('Android')) return 'android';
12
+ if (userAgentIncludes('like Mac')) return 'ios';
13
+ if (userAgentIncludes('Win')) return 'windows';
14
+ if (userAgentIncludes('Mac')) return 'macos';
15
+ if (userAgentIncludes('Linux')) return 'linux';
16
+ if (userAgentIncludes('X11')) return 'unix';
17
+ return 'unknown';
18
+ };
19
+
20
+ const OS = getOS();
21
+
22
+ export const Platform: PlatformInterface = { OS };
@@ -0,0 +1,15 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export interface Platform {
5
+ OS: KnownOS | 'unknown';
6
+ }
7
+
8
+ type KnownOS =
9
+ | 'windows'
10
+ | 'macos'
11
+ | 'unix'
12
+ | 'linux'
13
+ | 'ios'
14
+ | 'android'
15
+ | 'web';
@@ -0,0 +1,9 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export default class PlatformNotSupportedError extends Error {
5
+ constructor() {
6
+ super('Function not supported on current platform');
7
+ this.name = 'PlatformNotSupportedError';
8
+ }
9
+ }
@@ -0,0 +1,137 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { ChannelType } from '@aws-sdk/client-pinpoint';
5
+ import { addEventListener, AWSPinpointProviderCommon } from '../../../common';
6
+ import PlatformNotSupportedError from '../../PlatformNotSupportedError';
7
+ import { Platform } from '../../Platform';
8
+ import {
9
+ PushNotificationEvent,
10
+ PushNotificationMessage,
11
+ PushNotificationProvider,
12
+ NotificationsSubCategory,
13
+ } from '../../types';
14
+ import { AWSPinpointMessageEvent } from './types';
15
+ import {
16
+ dispatchPushNotificationEvent,
17
+ getAnalyticsEvent,
18
+ logger,
19
+ } from './utils';
20
+
21
+ export default class AWSPinpointProvider
22
+ extends AWSPinpointProviderCommon
23
+ implements PushNotificationProvider
24
+ {
25
+ static subCategory: NotificationsSubCategory = 'PushNotification';
26
+
27
+ private configured = false;
28
+
29
+ constructor() {
30
+ super(logger);
31
+ }
32
+
33
+ /**
34
+ * get the sub-category of the plugin
35
+ */
36
+ getSubCategory() {
37
+ return AWSPinpointProvider.subCategory;
38
+ }
39
+
40
+ configure = (config = {}): Record<string, any> => {
41
+ this.config = {
42
+ ...super.configure(config),
43
+ endpointInfo: { channelType: this.getChannelType() },
44
+ };
45
+
46
+ // some configuration steps should not be re-run even if provider is re-configured for some reason
47
+ if (!this.configured) {
48
+ // wire up default Pinpoint message event handling
49
+ addEventListener(
50
+ PushNotificationEvent.BACKGROUND_MESSAGE_RECEIVED,
51
+ message =>
52
+ this.recordMessageEvent(
53
+ message,
54
+ AWSPinpointMessageEvent.BACKGROUND_MESSAGE_RECEIVED
55
+ )
56
+ );
57
+ addEventListener(
58
+ PushNotificationEvent.FOREGROUND_MESSAGE_RECEIVED,
59
+ message =>
60
+ this.recordMessageEvent(
61
+ message,
62
+ AWSPinpointMessageEvent.FOREGROUND_MESSAGE_RECEIVED
63
+ )
64
+ );
65
+ const launchNotificationOpenedListener = addEventListener(
66
+ PushNotificationEvent.LAUNCH_NOTIFICATION_OPENED,
67
+ message => {
68
+ this.recordMessageEvent(
69
+ message,
70
+ AWSPinpointMessageEvent.NOTIFICATION_OPENED
71
+ );
72
+ // once we are done with it we can remove the listener
73
+ launchNotificationOpenedListener?.remove();
74
+ }
75
+ );
76
+ addEventListener(PushNotificationEvent.NOTIFICATION_OPENED, message => {
77
+ this.recordMessageEvent(
78
+ message,
79
+ AWSPinpointMessageEvent.NOTIFICATION_OPENED
80
+ );
81
+ // if we are in this state, we no longer need the listener as the app was launched via some other means
82
+ launchNotificationOpenedListener?.remove();
83
+ });
84
+ }
85
+
86
+ this.configured = true;
87
+ dispatchPushNotificationEvent('pinpointProvider_configured', null);
88
+ return this.config;
89
+ };
90
+
91
+ registerDevice = async (address: string): Promise<void> => {
92
+ if (!this.initialized) {
93
+ await this.init();
94
+ }
95
+ try {
96
+ this.config.endpointInfo = {
97
+ ...this.config.endpointInfo,
98
+ address,
99
+ };
100
+ await this.updateEndpoint();
101
+ } catch (err) {
102
+ this.logger.error('Error registering device', err);
103
+ throw err;
104
+ }
105
+ };
106
+
107
+ private getChannelType = (): ChannelType => {
108
+ switch (Platform.OS) {
109
+ case 'android': {
110
+ // FCM was previously known as GCM and continues to be the channel type in Pinpoint
111
+ return ChannelType.GCM;
112
+ }
113
+ case 'ios': {
114
+ // If building in debug mode, use the APNs sandbox
115
+ return global['__DEV__'] ? ChannelType.APNS_SANDBOX : ChannelType.APNS;
116
+ }
117
+ default: {
118
+ throw new PlatformNotSupportedError();
119
+ }
120
+ }
121
+ };
122
+
123
+ private recordMessageEvent = async (
124
+ message: PushNotificationMessage,
125
+ event: AWSPinpointMessageEvent
126
+ ): Promise<void> => {
127
+ const analyticsEvent = getAnalyticsEvent(message, event);
128
+ if (!analyticsEvent) {
129
+ logger.debug('A notification missing event information was not recorded');
130
+ return;
131
+ }
132
+ if (!this.initialized) {
133
+ await this.init();
134
+ }
135
+ return this.recordAnalyticsEvent(analyticsEvent);
136
+ };
137
+ }
@@ -0,0 +1,13 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export enum AWSPinpointMessageEventSource {
5
+ CAMPAIGN = '_campaign',
6
+ JOURNEY = '_journey',
7
+ }
8
+
9
+ export enum AWSPinpointMessageEvent {
10
+ NOTIFICATION_OPENED = 'opened_notification',
11
+ BACKGROUND_MESSAGE_RECEIVED = 'received_background',
12
+ FOREGROUND_MESSAGE_RECEIVED = 'received_foreground',
13
+ }
@@ -0,0 +1,79 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { Event as AWSPinpointAnalyticsEvent } from '@aws-sdk/client-pinpoint';
5
+ import { ConsoleLogger, Hub } from '@aws-amplify/core';
6
+ import { AMPLIFY_SYMBOL } from '../../../common';
7
+ import { PushNotificationMessage } from '../../types';
8
+ import {
9
+ AWSPinpointMessageEventSource,
10
+ AWSPinpointMessageEvent,
11
+ } from './types';
12
+
13
+ const ANDROID_CAMPAIGN_ACTIVITY_ID_KEY =
14
+ 'pinpoint.campaign.campaign_activity_id';
15
+ const ANDROID_CAMPAIGN_ID_KEY = 'pinpoint.campaign.campaign_id';
16
+ const ANDROID_CAMPAIGN_TREATMENT_ID_KEY = 'pinpoint.campaign.treatment_id';
17
+
18
+ export const logger = new ConsoleLogger('PushNotification.AWSPinpointProvider');
19
+
20
+ export const dispatchPushNotificationEvent = (
21
+ event: string,
22
+ data: any,
23
+ message?: string
24
+ ) => {
25
+ Hub.dispatch(
26
+ 'pushNotification',
27
+ { event, data, message },
28
+ 'PushNotification',
29
+ AMPLIFY_SYMBOL
30
+ );
31
+ };
32
+
33
+ export const getAnalyticsEvent = (
34
+ { data }: PushNotificationMessage,
35
+ event: AWSPinpointMessageEvent
36
+ ): AWSPinpointAnalyticsEvent | null => {
37
+ if (!data) {
38
+ return null;
39
+ }
40
+ const eventAttributes = getAnalyticsEventAttributes(data);
41
+ if (!eventAttributes) {
42
+ return null;
43
+ }
44
+ const { source, attributes } = eventAttributes;
45
+ return {
46
+ Attributes: attributes,
47
+ EventType: `${source}.${event}`,
48
+ Timestamp: new Date().toISOString(),
49
+ };
50
+ };
51
+
52
+ const getAnalyticsEventAttributes = (data: PushNotificationMessage['data']) => {
53
+ if (data.hasOwnProperty(ANDROID_CAMPAIGN_ID_KEY)) {
54
+ return {
55
+ source: AWSPinpointMessageEventSource.CAMPAIGN,
56
+ attributes: {
57
+ campaign_activity_id: data[ANDROID_CAMPAIGN_ACTIVITY_ID_KEY],
58
+ campaign_id: data[ANDROID_CAMPAIGN_ID_KEY],
59
+ treatment_id: data[ANDROID_CAMPAIGN_TREATMENT_ID_KEY],
60
+ },
61
+ };
62
+ }
63
+ const pinpoint =
64
+ typeof data.pinpoint === 'string'
65
+ ? JSON.parse(data.pinpoint)
66
+ : data.pinpoint;
67
+ if (pinpoint?.campaign) {
68
+ return {
69
+ source: AWSPinpointMessageEventSource.CAMPAIGN,
70
+ attributes: pinpoint.campaign,
71
+ };
72
+ }
73
+ if (pinpoint?.journey) {
74
+ return {
75
+ source: AWSPinpointMessageEventSource.JOURNEY,
76
+ attributes: pinpoint.journey,
77
+ };
78
+ }
79
+ };
@@ -0,0 +1,4 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export { default as AWSPinpointProvider } from './AWSPinpointProvider';