@aws-amplify/rtn-push-notification 1.1.8-api-v6-models.b3abc9b.0 → 1.2.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.
Files changed (116) hide show
  1. package/android/build.gradle +16 -1
  2. package/android/gradle/wrapper/gradle-wrapper.properties +1 -1
  3. package/android/src/hasPackageName/AndroidManifest.xml +22 -0
  4. package/android/src/main/AndroidManifest.xml +1 -2
  5. package/lib/apis/addMessageEventListener.d.ts +3 -0
  6. package/lib/apis/addMessageEventListener.js +11 -0
  7. package/lib/apis/addTokenEventListener.d.ts +2 -0
  8. package/lib/apis/addTokenEventListener.js +10 -0
  9. package/lib/apis/completeNotification.d.ts +1 -0
  10. package/lib/apis/completeNotification.js +8 -0
  11. package/lib/apis/getBadgeCount.d.ts +1 -0
  12. package/lib/apis/getBadgeCount.js +8 -0
  13. package/lib/apis/getConstants.d.ts +10 -0
  14. package/lib/apis/getConstants.js +8 -0
  15. package/lib/apis/getLaunchNotification.d.ts +2 -0
  16. package/lib/apis/getLaunchNotification.js +9 -0
  17. package/lib/apis/getPermissionStatus.d.ts +2 -0
  18. package/lib/apis/getPermissionStatus.js +9 -0
  19. package/lib/apis/index.d.ts +10 -0
  20. package/lib/apis/index.js +25 -0
  21. package/lib/apis/registerHeadlessTask.d.ts +2 -0
  22. package/lib/apis/registerHeadlessTask.js +17 -0
  23. package/lib/apis/requestPermissions.d.ts +2 -0
  24. package/lib/apis/requestPermissions.js +16 -0
  25. package/lib/apis/setBadgeCount.d.ts +1 -0
  26. package/lib/apis/setBadgeCount.js +8 -0
  27. package/lib/constants.d.ts +2 -0
  28. package/lib/constants.js +12 -0
  29. package/lib/index.d.ts +24 -3
  30. package/lib/index.js +15 -3
  31. package/lib/nativeModule.d.ts +4 -0
  32. package/lib/nativeModule.js +15 -0
  33. package/lib/tsconfig.tsbuildinfo +1 -0
  34. package/lib/types/index.d.ts +2 -0
  35. package/lib/types/index.js +20 -0
  36. package/lib/types/module.d.ts +31 -0
  37. package/lib/{types.js → types/module.js} +0 -1
  38. package/lib/types/native.d.ts +69 -0
  39. package/lib/types/native.js +4 -0
  40. package/lib/utils/index.d.ts +2 -0
  41. package/lib/utils/index.js +9 -0
  42. package/lib/utils/normalizeNativeMessage.d.ts +5 -0
  43. package/lib/utils/normalizeNativeMessage.js +74 -0
  44. package/lib/utils/normalizeNativePermissionStatus.d.ts +5 -0
  45. package/lib/utils/normalizeNativePermissionStatus.js +23 -0
  46. package/lib-esm/apis/addMessageEventListener.d.ts +3 -0
  47. package/lib-esm/apis/addMessageEventListener.js +7 -0
  48. package/lib-esm/apis/addTokenEventListener.d.ts +2 -0
  49. package/lib-esm/apis/addTokenEventListener.js +6 -0
  50. package/lib-esm/apis/completeNotification.d.ts +1 -0
  51. package/lib-esm/apis/completeNotification.js +4 -0
  52. package/lib-esm/apis/getBadgeCount.d.ts +1 -0
  53. package/lib-esm/apis/getBadgeCount.js +4 -0
  54. package/lib-esm/apis/getConstants.d.ts +10 -0
  55. package/lib-esm/apis/getConstants.js +4 -0
  56. package/lib-esm/apis/getLaunchNotification.d.ts +2 -0
  57. package/lib-esm/apis/getLaunchNotification.js +5 -0
  58. package/lib-esm/apis/getPermissionStatus.d.ts +2 -0
  59. package/lib-esm/apis/getPermissionStatus.js +5 -0
  60. package/lib-esm/apis/index.d.ts +10 -0
  61. package/lib-esm/apis/index.js +12 -0
  62. package/lib-esm/apis/registerHeadlessTask.d.ts +2 -0
  63. package/lib-esm/apis/registerHeadlessTask.js +13 -0
  64. package/lib-esm/apis/requestPermissions.d.ts +2 -0
  65. package/lib-esm/apis/requestPermissions.js +12 -0
  66. package/lib-esm/apis/setBadgeCount.d.ts +1 -0
  67. package/lib-esm/apis/setBadgeCount.js +4 -0
  68. package/lib-esm/constants.d.ts +2 -0
  69. package/lib-esm/constants.js +9 -0
  70. package/lib-esm/index.d.ts +24 -3
  71. package/lib-esm/index.js +14 -3
  72. package/lib-esm/nativeModule.d.ts +4 -0
  73. package/lib-esm/nativeModule.js +12 -0
  74. package/lib-esm/tsconfig.tsbuildinfo +1 -0
  75. package/lib-esm/types/index.d.ts +2 -0
  76. package/lib-esm/types/index.js +4 -0
  77. package/lib-esm/types/module.d.ts +31 -0
  78. package/lib-esm/{types.js → types/module.js} +1 -1
  79. package/lib-esm/types/native.d.ts +69 -0
  80. package/lib-esm/types/native.js +3 -0
  81. package/lib-esm/utils/index.d.ts +2 -0
  82. package/lib-esm/utils/index.js +4 -0
  83. package/lib-esm/utils/normalizeNativeMessage.d.ts +5 -0
  84. package/lib-esm/utils/normalizeNativeMessage.js +70 -0
  85. package/lib-esm/utils/normalizeNativePermissionStatus.d.ts +5 -0
  86. package/lib-esm/utils/normalizeNativePermissionStatus.js +19 -0
  87. package/package.json +53 -8
  88. package/src/apis/addMessageEventListener.ts +21 -0
  89. package/src/apis/addTokenEventListener.ts +14 -0
  90. package/src/apis/completeNotification.ts +7 -0
  91. package/src/apis/getBadgeCount.ts +7 -0
  92. package/src/apis/getConstants.ts +6 -0
  93. package/src/apis/getLaunchNotification.ts +12 -0
  94. package/src/apis/getPermissionStatus.ts +10 -0
  95. package/src/apis/index.ts +13 -0
  96. package/src/apis/registerHeadlessTask.ts +21 -0
  97. package/src/apis/requestPermissions.ts +18 -0
  98. package/src/apis/setBadgeCount.ts +7 -0
  99. package/src/constants.ts +11 -0
  100. package/src/index.ts +33 -7
  101. package/src/nativeModule.ts +20 -0
  102. package/src/types/index.ts +5 -0
  103. package/src/types/module.ts +43 -0
  104. package/src/types/native.ts +92 -0
  105. package/src/utils/index.ts +5 -0
  106. package/src/utils/normalizeNativeMessage.ts +103 -0
  107. package/src/utils/normalizeNativePermissionStatus.ts +27 -0
  108. package/lib/.tsbuildinfo +0 -3
  109. package/lib/index.js.map +0 -1
  110. package/lib/types.d.ts +0 -18
  111. package/lib/types.js.map +0 -1
  112. package/lib-esm/.tsbuildinfo +0 -3
  113. package/lib-esm/index.js.map +0 -1
  114. package/lib-esm/types.d.ts +0 -18
  115. package/lib-esm/types.js.map +0 -1
  116. package/src/types.ts +0 -23
@@ -0,0 +1,19 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ /**
4
+ * @internal
5
+ */
6
+ export const normalizeNativePermissionStatus = (nativeStatus) => {
7
+ switch (nativeStatus) {
8
+ case 'ShouldRequest':
9
+ return 'shouldRequest';
10
+ case 'NotDetermined':
11
+ case 'ShouldExplainThenRequest':
12
+ return 'shouldExplainThenRequest';
13
+ case 'Authorized':
14
+ case 'Granted':
15
+ return 'granted';
16
+ case 'Denied':
17
+ return 'denied';
18
+ }
19
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws-amplify/rtn-push-notification",
3
- "version": "1.1.8-api-v6-models.b3abc9b.0+b3abc9b",
3
+ "version": "1.2.0",
4
4
  "description": "React Native module for aws-amplify push notifications",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib-esm/index.js",
@@ -10,18 +10,22 @@
10
10
  "access": "public"
11
11
  },
12
12
  "scripts": {
13
- "test": "tslint 'src/**/*.ts'",
13
+ "test": "npm run lint && jest -w 1 --coverage",
14
14
  "test:android": "./android/gradlew test -p ./android",
15
15
  "build-with-test": "npm run clean && npm test && tsc && webpack",
16
- "build:cjs": "node ./build es5 && webpack && webpack --config ./webpack.config.dev.js",
17
- "build:esm": "node ./build es6",
18
- "build:cjs:watch": "node ./build es5 --watch",
19
- "build:esm:watch": "node ./build es6 --watch",
16
+ "build:cjs": "rimraf lib && tsc -m commonjs --outDir lib && webpack && webpack --config ./webpack.config.dev.js",
17
+ "build:esm": "rimraf lib-esm && tsc -m esnext --outDir lib-esm",
18
+ "build:cjs:watch": "rimraf lib && tsc -m commonjs --outDir lib --watch",
19
+ "build:esm:watch": "rimraf lib-esm && tsc -m esnext --outDir lib-esm --watch",
20
20
  "build": "npm run clean && npm run build:esm && npm run build:cjs",
21
21
  "clean": "rimraf lib-esm lib dist",
22
22
  "format": "echo \"Not implemented\"",
23
23
  "lint": "tslint 'src/**/*.ts' && npm run ts-coverage",
24
- "ts-coverage": "typescript-coverage-report -p ./tsconfig.build.json -t 88.21"
24
+ "ts-coverage": "typescript-coverage-report -p ./tsconfig.json -t 99"
25
+ },
26
+ "devDependencies": {
27
+ "@types/react-native": "0.70.0",
28
+ "typescript": "5.0.2"
25
29
  },
26
30
  "react-native": {
27
31
  "./lib/index": "./lib-esm/index.js"
@@ -44,5 +48,46 @@
44
48
  "lib-esm",
45
49
  "src"
46
50
  ],
47
- "gitHead": "b3abc9b75f6f1353f4c2fbc10cd3369be498e9fc"
51
+ "jest": {
52
+ "globals": {
53
+ "ts-jest": {
54
+ "diagnostics": false,
55
+ "tsConfig": {
56
+ "allowJs": true,
57
+ "noEmitOnError": false
58
+ }
59
+ }
60
+ },
61
+ "transform": {
62
+ "^.+\\.(js|jsx|ts|tsx)$": "ts-jest"
63
+ },
64
+ "testPathIgnorePatterns": [
65
+ "/testUtils/"
66
+ ],
67
+ "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(tsx?|jsx?)$",
68
+ "moduleFileExtensions": [
69
+ "ts",
70
+ "tsx",
71
+ "js",
72
+ "json",
73
+ "jsx"
74
+ ],
75
+ "testEnvironment": "jsdom",
76
+ "testURL": "http://localhost/",
77
+ "coverageThreshold": {
78
+ "global": {
79
+ "branches": 0,
80
+ "functions": 0,
81
+ "lines": 0,
82
+ "statements": 0
83
+ }
84
+ },
85
+ "coveragePathIgnorePatterns": [
86
+ "node_modules",
87
+ "dist",
88
+ "lib",
89
+ "lib-esm"
90
+ ]
91
+ },
92
+ "gitHead": "d505105326d7f6214f6bd1e06eb20be3a3651377"
48
93
  }
@@ -0,0 +1,21 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { EmitterSubscription } from 'react-native';
5
+ import { nativeEventEmitter } from '../nativeModule';
6
+ import { NativeMessage, PushNotificationMessage } from '../types';
7
+ import { normalizeNativeMessage } from '../utils';
8
+
9
+ export const addMessageEventListener = (
10
+ event: string,
11
+ listener: (
12
+ message: PushNotificationMessage | null,
13
+ completionHandlerId?: string
14
+ ) => void
15
+ ): EmitterSubscription =>
16
+ nativeEventEmitter.addListener(event, (nativeMessage: NativeMessage) => {
17
+ listener(
18
+ normalizeNativeMessage(nativeMessage),
19
+ nativeMessage.completionHandlerId
20
+ );
21
+ });
@@ -0,0 +1,14 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { EmitterSubscription } from 'react-native';
5
+ import { nativeEventEmitter } from '../nativeModule';
6
+ import { TokenPayload } from '../types';
7
+
8
+ export const addTokenEventListener = (
9
+ event: string,
10
+ listener: (token: string) => void
11
+ ): EmitterSubscription =>
12
+ nativeEventEmitter.addListener(event, ({ token }: TokenPayload) => {
13
+ listener(token);
14
+ });
@@ -0,0 +1,7 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { nativeModule } from '../nativeModule';
5
+
6
+ export const completeNotification = (completionHandlerId: string): void =>
7
+ nativeModule.completeNotification?.(completionHandlerId);
@@ -0,0 +1,7 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { nativeModule } from '../nativeModule';
5
+
6
+ export const getBadgeCount = (): void | Promise<number | null> =>
7
+ nativeModule.getBadgeCount?.();
@@ -0,0 +1,6 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { nativeModule } from '../nativeModule';
5
+
6
+ export const getConstants = () => nativeModule.getConstants();
@@ -0,0 +1,12 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { nativeModule } from '../nativeModule';
5
+ import { PushNotificationMessage } from '../types';
6
+ import { normalizeNativeMessage } from '../utils';
7
+
8
+ export const getLaunchNotification =
9
+ async (): Promise<PushNotificationMessage | null> =>
10
+ normalizeNativeMessage(
11
+ (await nativeModule.getLaunchNotification()) ?? undefined
12
+ );
@@ -0,0 +1,10 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { nativeModule } from '../nativeModule';
5
+ import { PushNotificationPermissionStatus } from '../types';
6
+ import { normalizeNativePermissionStatus } from '../utils';
7
+
8
+ export const getPermissionStatus =
9
+ async (): Promise<PushNotificationPermissionStatus> =>
10
+ normalizeNativePermissionStatus(await nativeModule.getPermissionStatus());
@@ -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 { addMessageEventListener } from './addMessageEventListener';
5
+ export { addTokenEventListener } from './addTokenEventListener';
6
+ export { completeNotification } from './completeNotification';
7
+ export { getBadgeCount } from './getBadgeCount';
8
+ export { getConstants } from './getConstants';
9
+ export { getLaunchNotification } from './getLaunchNotification';
10
+ export { getPermissionStatus } from './getPermissionStatus';
11
+ export { registerHeadlessTask } from './registerHeadlessTask';
12
+ export { requestPermissions } from './requestPermissions';
13
+ export { setBadgeCount } from './setBadgeCount';
@@ -0,0 +1,21 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { AppRegistry } from 'react-native';
5
+ import { getConstants } from './getConstants';
6
+ import { NativeMessage, PushNotificationMessage } from '../types';
7
+ import { normalizeNativeMessage } from '../utils';
8
+
9
+ export const registerHeadlessTask = (
10
+ task: (message: PushNotificationMessage | null) => Promise<void>
11
+ ): void => {
12
+ const { NativeHeadlessTaskKey } = getConstants();
13
+ if (NativeHeadlessTaskKey) {
14
+ AppRegistry.registerHeadlessTask(
15
+ NativeHeadlessTaskKey,
16
+ () => async (nativeMessage: NativeMessage) => {
17
+ await task(normalizeNativeMessage(nativeMessage));
18
+ }
19
+ );
20
+ }
21
+ };
@@ -0,0 +1,18 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { nativeModule } from '../nativeModule';
5
+ import { PushNotificationPermissions } from '../types';
6
+
7
+ export const requestPermissions = async (
8
+ { alert = true, badge = true, sound = true }: PushNotificationPermissions = {
9
+ alert: true,
10
+ badge: true,
11
+ sound: true,
12
+ }
13
+ ): Promise<boolean> =>
14
+ nativeModule.requestPermissions({
15
+ alert,
16
+ badge,
17
+ sound,
18
+ });
@@ -0,0 +1,7 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { nativeModule } from '../nativeModule';
5
+
6
+ export const setBadgeCount = (count: number): void =>
7
+ nativeModule.setBadgeCount?.(count);
@@ -0,0 +1,11 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { Platform } from 'react-native';
4
+
5
+ // General
6
+ export const PACKAGE_NAME = '@aws-amplify/rtn-push-notification';
7
+ export const LINKING_ERROR =
8
+ `The ${PACKAGE_NAME} package doesn't seem to be linked. Make sure: \n\n` +
9
+ Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
10
+ '- You rebuilt the app after installing the package\n' +
11
+ '- You are not using Expo Go\n';
package/src/index.ts CHANGED
@@ -1,10 +1,36 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import { NativeModules } from 'react-native';
5
- import { PushNotificationNativeModule } from './types';
6
- export { PushNotificationNativeModule } from './types';
7
- export const {
8
- AmplifyRTNPushNotification,
9
- }: { AmplifyRTNPushNotification?: PushNotificationNativeModule } =
10
- NativeModules;
4
+ import {
5
+ addMessageEventListener,
6
+ addTokenEventListener,
7
+ completeNotification,
8
+ getBadgeCount,
9
+ getConstants,
10
+ getLaunchNotification,
11
+ getPermissionStatus,
12
+ registerHeadlessTask,
13
+ requestPermissions,
14
+ setBadgeCount,
15
+ } from './apis';
16
+ export {
17
+ PushNotificationMessage,
18
+ PushNotificationPermissionStatus,
19
+ PushNotificationPermissions,
20
+ } from './types';
21
+
22
+ const module = {
23
+ addMessageEventListener,
24
+ addTokenEventListener,
25
+ completeNotification,
26
+ getBadgeCount,
27
+ getConstants,
28
+ getLaunchNotification,
29
+ getPermissionStatus,
30
+ registerHeadlessTask,
31
+ requestPermissions,
32
+ setBadgeCount,
33
+ };
34
+
35
+ export type PushNotificationModule = typeof module;
36
+ export { module };
@@ -0,0 +1,20 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { NativeModules, NativeEventEmitter } from 'react-native';
5
+ import { LINKING_ERROR } from './constants';
6
+ import { PushNotificationNativeModule } from './types';
7
+
8
+ export const nativeModule: PushNotificationNativeModule =
9
+ NativeModules.AmplifyRTNPushNotification
10
+ ? NativeModules.AmplifyRTNPushNotification
11
+ : new Proxy(
12
+ {},
13
+ {
14
+ get() {
15
+ throw new Error(LINKING_ERROR);
16
+ },
17
+ }
18
+ );
19
+
20
+ export const nativeEventEmitter = new NativeEventEmitter(nativeModule);
@@ -0,0 +1,5 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export * from './module';
5
+ export * from './native';
@@ -0,0 +1,43 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /**
5
+ * Types here are duplicated to the push notificaiton category within Amplify as it is not possible to share a
6
+ * source of truth with a package that may conditionally not exist for developers not using push notifications.
7
+ * Modifications made to these types should be reflected in the Amplify library and vice-versa!
8
+ */
9
+
10
+ export interface PushNotificationPermissions
11
+ extends Partial<Record<string, boolean>> {
12
+ alert?: boolean;
13
+ badge?: boolean;
14
+ sound?: boolean;
15
+ }
16
+
17
+ export interface PushNotificationMessage {
18
+ title?: string;
19
+ body?: string;
20
+ imageUrl?: string;
21
+ deeplinkUrl?: string;
22
+ goToUrl?: string;
23
+ fcmOptions?: FcmPlatformOptions;
24
+ apnsOptions?: ApnsPlatformOptions;
25
+ data?: Record<string, unknown>;
26
+ }
27
+
28
+ export type PushNotificationPermissionStatus =
29
+ | 'denied'
30
+ | 'granted'
31
+ | 'shouldRequest'
32
+ | 'shouldExplainThenRequest';
33
+
34
+ interface ApnsPlatformOptions {
35
+ subtitle?: string;
36
+ }
37
+
38
+ interface FcmPlatformOptions {
39
+ channelId: string;
40
+ messageId: string;
41
+ senderId: string;
42
+ sendTime: Date;
43
+ }
@@ -0,0 +1,92 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { NativeModule } from 'react-native';
5
+ import { PushNotificationMessage, PushNotificationPermissions } from './module';
6
+
7
+ export interface PushNotificationNativeModule extends NativeModule {
8
+ completeNotification?: (completionHandlerId: string) => void;
9
+ getConstants: () => {
10
+ NativeEvent: {
11
+ BACKGROUND_MESSAGE_RECEIVED?: string;
12
+ FOREGROUND_MESSAGE_RECEIVED: string;
13
+ LAUNCH_NOTIFICATION_OPENED: string;
14
+ NOTIFICATION_OPENED: string;
15
+ TOKEN_RECEIVED: string;
16
+ };
17
+ NativeHeadlessTaskKey?: string;
18
+ };
19
+ getLaunchNotification: () => Promise<NativeMessage | null>;
20
+ getBadgeCount?: () => Promise<number>;
21
+ setBadgeCount?: (count: number) => void;
22
+ getPermissionStatus: () => Promise<NativePermissionStatus>;
23
+ requestPermissions: (
24
+ permissions: PushNotificationPermissions
25
+ ) => Promise<boolean>;
26
+ }
27
+
28
+ export type NativeAction = {
29
+ deeplink?: string;
30
+ url?: string;
31
+ };
32
+
33
+ export type NativeMessage = (ApnsMessage | FcmMessage) & {
34
+ token?: never;
35
+ };
36
+
37
+ export type NativePermissionStatus =
38
+ | AndroidPermissionStatus
39
+ | IosPermissionStatus;
40
+
41
+ export interface NormalizedValues {
42
+ body?: string;
43
+ imageUrl?: string;
44
+ title?: string;
45
+ action?: Pick<PushNotificationMessage, 'goToUrl' | 'deeplinkUrl'>;
46
+ options?: Pick<PushNotificationMessage, 'apnsOptions' | 'fcmOptions'>;
47
+ data?: Record<string, unknown>;
48
+ }
49
+
50
+ // iOS
51
+ export type ApnsMessage = {
52
+ aps: {
53
+ alert?: {
54
+ body?: string;
55
+ title?: string;
56
+ subtitle?: string;
57
+ };
58
+ };
59
+ data?: {
60
+ pinpoint?: NativeAction;
61
+ 'media-url'?: string;
62
+ };
63
+ rawData?: never;
64
+ completionHandlerId?: string;
65
+ };
66
+
67
+ export type IosPermissionStatus = 'NotDetermined' | 'Authorized' | 'Denied';
68
+
69
+ // Android
70
+ export type FcmMessage = {
71
+ action?: NativeAction;
72
+ aps?: never;
73
+ body?: string;
74
+ imageUrl?: string;
75
+ rawData?: Record<string, unknown>;
76
+ title?: string;
77
+ channelId?: string;
78
+ messageId?: string;
79
+ senderId?: string;
80
+ sendTime?: number;
81
+ completionHandlerId?: never;
82
+ };
83
+
84
+ export type AndroidPermissionStatus =
85
+ | 'ShouldRequest'
86
+ | 'ShouldExplainThenRequest'
87
+ | 'Granted'
88
+ | 'Denied';
89
+
90
+ export type TokenPayload = {
91
+ token: string;
92
+ };
@@ -0,0 +1,5 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export { normalizeNativeMessage } from './normalizeNativeMessage';
5
+ export { normalizeNativePermissionStatus } from './normalizeNativePermissionStatus';
@@ -0,0 +1,103 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { isEmpty } from 'lodash';
5
+ import {
6
+ ApnsMessage,
7
+ FcmMessage,
8
+ NativeAction,
9
+ NativeMessage,
10
+ NormalizedValues,
11
+ PushNotificationMessage,
12
+ } from '../types';
13
+
14
+ /**
15
+ * @internal
16
+ */
17
+ export const normalizeNativeMessage = (
18
+ nativeMessage?: NativeMessage
19
+ ): PushNotificationMessage | null => {
20
+ let normalized: NormalizedValues;
21
+ if (isApnsMessage(nativeMessage)) {
22
+ normalized = normalizeApnsMessage(nativeMessage);
23
+ } else if (isFcmMessage(nativeMessage)) {
24
+ normalized = normalizeFcmMessage(nativeMessage);
25
+ } else {
26
+ return null;
27
+ }
28
+ const { body, imageUrl, title, action, options, data } = normalized;
29
+ return {
30
+ body,
31
+ data,
32
+ imageUrl,
33
+ title,
34
+ ...action,
35
+ ...options,
36
+ };
37
+ };
38
+
39
+ const normalizeApnsMessage = (apnsMessage: ApnsMessage): NormalizedValues => {
40
+ const { aps, data } = apnsMessage;
41
+ const { body, title } = aps.alert ?? {};
42
+ const action = getApnsAction(data?.pinpoint) ?? {};
43
+ const imageUrl = data?.['media-url'];
44
+ const options = getApnsOptions(apnsMessage);
45
+ return { body, imageUrl, title, action, options, data };
46
+ };
47
+
48
+ const normalizeFcmMessage = (fcmMessage: FcmMessage): NormalizedValues => {
49
+ const { body, imageUrl, rawData: data, title } = fcmMessage;
50
+ const action = getFcmAction(fcmMessage.action) ?? {};
51
+ const options = getFcmOptions(fcmMessage);
52
+ return { body, imageUrl, title, action, options, data };
53
+ };
54
+
55
+ const getApnsAction = (
56
+ action?: NativeAction
57
+ ): Pick<PushNotificationMessage, 'deeplinkUrl'> | undefined => {
58
+ if (action?.deeplink) {
59
+ return { deeplinkUrl: action.deeplink };
60
+ }
61
+ };
62
+
63
+ const getFcmAction = (
64
+ action?: NativeAction
65
+ ): Pick<PushNotificationMessage, 'goToUrl' | 'deeplinkUrl'> | undefined => {
66
+ if (action?.url) {
67
+ return { goToUrl: action.url };
68
+ }
69
+ if (action?.deeplink) {
70
+ return { deeplinkUrl: action.deeplink };
71
+ }
72
+ };
73
+
74
+ const getApnsOptions = ({
75
+ aps,
76
+ }: ApnsMessage): Pick<PushNotificationMessage, 'apnsOptions'> => {
77
+ const { subtitle } = aps.alert ?? {};
78
+ const apnsOptions = { ...(subtitle && { subtitle }) };
79
+ return { ...(!isEmpty(apnsOptions) && { apnsOptions }) };
80
+ };
81
+
82
+ const getFcmOptions = ({
83
+ channelId = '',
84
+ messageId = '',
85
+ senderId = '',
86
+ sendTime = new Date().getTime(),
87
+ }: FcmMessage): Pick<PushNotificationMessage, 'fcmOptions'> => {
88
+ const fcmOptions = {
89
+ channelId,
90
+ messageId,
91
+ senderId,
92
+ sendTime: new Date(sendTime),
93
+ };
94
+ return { ...(!isEmpty(fcmOptions) && { fcmOptions }) };
95
+ };
96
+
97
+ const isApnsMessage = (
98
+ nativeMessage?: NativeMessage
99
+ ): nativeMessage is ApnsMessage => !!nativeMessage?.aps;
100
+
101
+ const isFcmMessage = (
102
+ nativeMessage?: NativeMessage
103
+ ): nativeMessage is FcmMessage => !!nativeMessage?.rawData;
@@ -0,0 +1,27 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import {
5
+ NativePermissionStatus,
6
+ PushNotificationPermissionStatus,
7
+ } from '../types';
8
+
9
+ /**
10
+ * @internal
11
+ */
12
+ export const normalizeNativePermissionStatus = (
13
+ nativeStatus: NativePermissionStatus
14
+ ): PushNotificationPermissionStatus => {
15
+ switch (nativeStatus) {
16
+ case 'ShouldRequest':
17
+ return 'shouldRequest';
18
+ case 'NotDetermined':
19
+ case 'ShouldExplainThenRequest':
20
+ return 'shouldExplainThenRequest';
21
+ case 'Authorized':
22
+ case 'Granted':
23
+ return 'granted';
24
+ case 'Denied':
25
+ return 'denied';
26
+ }
27
+ };
package/lib/.tsbuildinfo DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "version": "3.8.3"
3
- }
package/lib/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,qEAAqE;AACrE,sCAAsC;;AAEtC,6CAA6C;AAI5C,QAAA,0BAA0B,GAE1B,4BAAa,4BAAC"}
package/lib/types.d.ts DELETED
@@ -1,18 +0,0 @@
1
- export interface PushNotificationNativeModule {
2
- completeNotification?: (completionHandlerId: string) => void;
3
- getConstants: () => {
4
- NativeEvent: {
5
- BACKGROUND_MESSAGE_RECEIVED?: string;
6
- FOREGROUND_MESSAGE_RECEIVED: string;
7
- LAUNCH_NOTIFICATION_OPENED: string;
8
- NOTIFICATION_OPENED: string;
9
- TOKEN_RECEIVED: string;
10
- };
11
- NativeHeadlessTaskKey?: string;
12
- };
13
- getLaunchNotification: () => Promise<Record<string, string>>;
14
- getBadgeCount: () => Promise<number>;
15
- setBadgeCount: (count: number) => void;
16
- getPermissionStatus: () => Promise<string>;
17
- requestPermissions: (permissions: Record<string, boolean>) => Promise<boolean>;
18
- }
package/lib/types.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";AAAA,qEAAqE;AACrE,sCAAsC"}
@@ -1,3 +0,0 @@
1
- {
2
- "version": "3.8.3"
3
- }
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,sCAAsC;AAEtC,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAG7C,MAAM,CACL,IAAA,qEAA0B,CAEZ"}