expo-notifications 0.28.3 → 0.28.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.
- package/CHANGELOG.md +5 -1
- package/android/build.gradle +2 -2
- package/build/NotificationsEmitter.d.ts.map +1 -1
- package/build/NotificationsEmitter.js +2 -12
- package/build/NotificationsEmitter.js.map +1 -1
- package/build/useLastNotificationResponse.d.ts.map +1 -1
- package/build/useLastNotificationResponse.js +5 -2
- package/build/useLastNotificationResponse.js.map +1 -1
- package/build/utils/mapNotificationResponse.d.ts +21 -0
- package/build/utils/mapNotificationResponse.d.ts.map +1 -0
- package/build/utils/mapNotificationResponse.js +26 -0
- package/build/utils/mapNotificationResponse.js.map +1 -0
- package/package.json +2 -2
- package/src/NotificationsEmitter.ts +2 -13
- package/src/useLastNotificationResponse.ts +5 -2
- package/src/utils/mapNotificationResponse.ts +28 -0
package/CHANGELOG.md
CHANGED
|
@@ -10,7 +10,11 @@
|
|
|
10
10
|
|
|
11
11
|
### 💡 Others
|
|
12
12
|
|
|
13
|
-
## 0.28.
|
|
13
|
+
## 0.28.4 — 2024-05-29
|
|
14
|
+
|
|
15
|
+
### 🐛 Bug fixes
|
|
16
|
+
|
|
17
|
+
- [Android] Correctly map response in useLastNotificationResponse hook. ([#28938](https://github.com/expo/expo/pull/28938) by [@douglowder](https://github.com/douglowder))
|
|
14
18
|
|
|
15
19
|
### 💡 Others
|
|
16
20
|
|
package/android/build.gradle
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
apply plugin: 'com.android.library'
|
|
2
2
|
|
|
3
3
|
group = 'host.exp.exponent'
|
|
4
|
-
version = '0.28.
|
|
4
|
+
version = '0.28.4'
|
|
5
5
|
|
|
6
6
|
def expoModulesCorePlugin = new File(project(":expo-modules-core").projectDir.absolutePath, "ExpoModulesCorePlugin.gradle")
|
|
7
7
|
apply from: expoModulesCorePlugin
|
|
@@ -14,7 +14,7 @@ android {
|
|
|
14
14
|
namespace "expo.modules.notifications"
|
|
15
15
|
defaultConfig {
|
|
16
16
|
versionCode 21
|
|
17
|
-
versionName '0.28.
|
|
17
|
+
versionName '0.28.4'
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
buildFeatures {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NotificationsEmitter.d.ts","sourceRoot":"","sources":["../src/NotificationsEmitter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,YAAY,EAAuB,MAAM,mBAAmB,CAAC;AAEpF,OAAO,EAAE,YAAY,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"NotificationsEmitter.d.ts","sourceRoot":"","sources":["../src/NotificationsEmitter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,YAAY,EAAuB,MAAM,mBAAmB,CAAC;AAEpF,OAAO,EAAE,YAAY,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAY3E,eAAO,MAAM,yBAAyB,+CAA+C,CAAC;AAEtF;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,+BAA+B,CAC7C,QAAQ,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI,GACtC,YAAY,CAEd;AAED;;;;;;;GAOG;AACH,wBAAgB,+BAA+B,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,YAAY,CAElF;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,uCAAuC,CACrD,QAAQ,EAAE,CAAC,KAAK,EAAE,oBAAoB,KAAK,IAAI,GAC9C,YAAY,CAQd;AAED;;;;GAIG;AACH,wBAAgB,8BAA8B,CAAC,YAAY,EAAE,YAAY,QAExE;AAGD;;GAEG;AACH,wBAAsB,gCAAgC,IAAI,OAAO,CAAC,oBAAoB,GAAG,IAAI,CAAC,CAK7F"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { EventEmitter, UnavailabilityError } from 'expo-modules-core';
|
|
2
2
|
import NotificationsEmitterModule from './NotificationsEmitterModule';
|
|
3
|
+
import { mapNotificationResponse } from './utils/mapNotificationResponse';
|
|
3
4
|
// Web uses SyntheticEventEmitter
|
|
4
5
|
const emitter = new EventEmitter(NotificationsEmitterModule);
|
|
5
6
|
const didReceiveNotificationEventName = 'onDidReceiveNotification';
|
|
@@ -73,18 +74,7 @@ export function addNotificationsDroppedListener(listener) {
|
|
|
73
74
|
*/
|
|
74
75
|
export function addNotificationResponseReceivedListener(listener) {
|
|
75
76
|
return emitter.addListener(didReceiveNotificationResponseEventName, (response) => {
|
|
76
|
-
const mappedResponse =
|
|
77
|
-
try {
|
|
78
|
-
const dataString = mappedResponse?.notification?.request?.content['dataString'];
|
|
79
|
-
if (typeof dataString === 'string') {
|
|
80
|
-
mappedResponse.notification.request.content.data = JSON.parse(dataString);
|
|
81
|
-
delete mappedResponse.notification.request.content.dataString;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
catch (e) {
|
|
85
|
-
console.log(`Error in response: ${e}`);
|
|
86
|
-
}
|
|
87
|
-
console.log(`response received: ${JSON.stringify(mappedResponse, null, 2)}`);
|
|
77
|
+
const mappedResponse = mapNotificationResponse(response);
|
|
88
78
|
listener(mappedResponse);
|
|
89
79
|
});
|
|
90
80
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NotificationsEmitter.js","sourceRoot":"","sources":["../src/NotificationsEmitter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAgB,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAGpF,OAAO,0BAA0B,MAAM,8BAA8B,CAAC;
|
|
1
|
+
{"version":3,"file":"NotificationsEmitter.js","sourceRoot":"","sources":["../src/NotificationsEmitter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAgB,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAGpF,OAAO,0BAA0B,MAAM,8BAA8B,CAAC;AACtE,OAA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CAAgC,EAAE,CAAC;AAC7E,CAAC","sourcesContent":["import { EventEmitter, Subscription, UnavailabilityError } from 'expo-modules-core';\n\nimport { Notification, NotificationResponse } from './Notifications.types';\nimport NotificationsEmitterModule from './NotificationsEmitterModule';\nimport { mapNotificationResponse } from './utils/mapNotificationResponse';\n\n// Web uses SyntheticEventEmitter\nconst emitter = new EventEmitter(NotificationsEmitterModule);\n\nconst didReceiveNotificationEventName = 'onDidReceiveNotification';\nconst didDropNotificationsEventName = 'onNotificationsDeleted';\nconst didReceiveNotificationResponseEventName = 'onDidReceiveNotificationResponse';\n\n// @docsMissing\nexport const DEFAULT_ACTION_IDENTIFIER = 'expo.modules.notifications.actions.DEFAULT';\n\n/**\n * Listeners registered by this method will be called whenever a notification is received while the app is running.\n * @param listener A function accepting a notification ([`Notification`](#notification)) as an argument.\n * @return A [`Subscription`](#subscription) object represents the subscription of the provided listener.\n * @example Registering a notification listener using a React hook:\n * ```jsx\n * import React from 'react';\n * import * as Notifications from 'expo-notifications';\n *\n * export default function App() {\n * React.useEffect(() => {\n * const subscription = Notifications.addNotificationReceivedListener(notification => {\n * console.log(notification);\n * });\n * return () => subscription.remove();\n * }, []);\n *\n * return (\n * // Your app content\n * );\n * }\n * ```\n * @header listen\n */\nexport function addNotificationReceivedListener(\n listener: (event: Notification) => void\n): Subscription {\n return emitter.addListener<Notification>(didReceiveNotificationEventName, listener);\n}\n\n/**\n * Listeners registered by this method will be called whenever some notifications have been dropped by the server.\n * Applicable only to Firebase Cloud Messaging which we use as a notifications service on Android. It corresponds to `onDeletedMessages()` callback.\n * More information can be found in [Firebase docs](https://firebase.google.com/docs/cloud-messaging/android/receive#override-ondeletedmessages).\n * @param listener A callback function.\n * @return A [`Subscription`](#subscription) object represents the subscription of the provided listener.\n * @header listen\n */\nexport function addNotificationsDroppedListener(listener: () => void): Subscription {\n return emitter.addListener<void>(didDropNotificationsEventName, listener);\n}\n\n/**\n * Listeners registered by this method will be called whenever a user interacts with a notification (for example, taps on it).\n * @param listener A function accepting notification response ([`NotificationResponse`](#notificationresponse)) as an argument.\n * @return A [`Subscription`](#subscription) object represents the subscription of the provided listener.\n * @example Register a notification responder listener:\n * ```jsx\n * import React from 'react';\n * import { Linking } from 'react-native';\n * import * as Notifications from 'expo-notifications';\n *\n * export default function Container() {\n * React.useEffect(() => {\n * const subscription = Notifications.addNotificationResponseReceivedListener(response => {\n * const url = response.notification.request.content.data.url;\n * Linking.openURL(url);\n * });\n * return () => subscription.remove();\n * }, []);\n *\n * return (\n * // Your app content\n * );\n * }\n * ```\n * @header listen\n */\nexport function addNotificationResponseReceivedListener(\n listener: (event: NotificationResponse) => void\n): Subscription {\n return emitter.addListener<NotificationResponse>(\n didReceiveNotificationResponseEventName,\n (response: NotificationResponse) => {\n const mappedResponse = mapNotificationResponse(response);\n listener(mappedResponse);\n }\n );\n}\n\n/**\n * Removes a notification subscription returned by an `addNotificationListener` call.\n * @param subscription A subscription returned by `addNotificationListener` method.\n * @header listen\n */\nexport function removeNotificationSubscription(subscription: Subscription) {\n emitter.removeSubscription(subscription);\n}\n\n// @docsMissing\n/**\n * @header listen\n */\nexport async function getLastNotificationResponseAsync(): Promise<NotificationResponse | null> {\n if (!NotificationsEmitterModule.getLastNotificationResponseAsync) {\n throw new UnavailabilityError('ExpoNotifications', 'getLastNotificationResponseAsync');\n }\n return await NotificationsEmitterModule.getLastNotificationResponseAsync();\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useLastNotificationResponse.d.ts","sourceRoot":"","sources":["../src/useLastNotificationResponse.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"useLastNotificationResponse.d.ts","sourceRoot":"","sources":["../src/useLastNotificationResponse.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAK7D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,MAAM,CAAC,OAAO,UAAU,2BAA2B,4CA8BlD"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useEffect, useLayoutEffect, useState } from 'react';
|
|
2
2
|
import { addNotificationResponseReceivedListener } from './NotificationsEmitter';
|
|
3
3
|
import NotificationsEmitterModule from './NotificationsEmitterModule';
|
|
4
|
+
import { mapNotificationResponse } from './utils/mapNotificationResponse';
|
|
4
5
|
/**
|
|
5
6
|
* A React hook always returns the notification response that was received most recently
|
|
6
7
|
* (a notification response designates an interaction with a notification, such as tapping on it).
|
|
@@ -42,7 +43,8 @@ export default function useLastNotificationResponse() {
|
|
|
42
43
|
// useLayoutEffect ensures the listener is registered as soon as possible
|
|
43
44
|
useLayoutEffect(() => {
|
|
44
45
|
const subscription = addNotificationResponseReceivedListener((response) => {
|
|
45
|
-
|
|
46
|
+
const mappedResponse = mapNotificationResponse(response);
|
|
47
|
+
setLastNotificationResponse(mappedResponse);
|
|
46
48
|
});
|
|
47
49
|
return () => {
|
|
48
50
|
subscription.remove();
|
|
@@ -56,7 +58,8 @@ export default function useLastNotificationResponse() {
|
|
|
56
58
|
// We only update the state with the resolved value if it's empty,
|
|
57
59
|
// because if it's not empty it must have been populated by the `useLayoutEffect`
|
|
58
60
|
// listener which returns "live" values.
|
|
59
|
-
|
|
61
|
+
const mappedResponse = response ? mapNotificationResponse(response) : response;
|
|
62
|
+
setLastNotificationResponse((currentResponse) => currentResponse ?? mappedResponse);
|
|
60
63
|
});
|
|
61
64
|
}, []);
|
|
62
65
|
return lastNotificationResponse;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useLastNotificationResponse.js","sourceRoot":"","sources":["../src/useLastNotificationResponse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAG7D,OAAO,EAAE,uCAAuC,EAAE,MAAM,wBAAwB,CAAC;AACjF,OAAO,0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I,cAAc,CAAC,CAAC;QACtF,CAAC,CAAC,CAAC;IACL,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO,wBAAwB,CAAC;AAClC,CAAC","sourcesContent":["import { useEffect, useLayoutEffect, useState } from 'react';\n\nimport { NotificationResponse } from './Notifications.types';\nimport { addNotificationResponseReceivedListener } from './NotificationsEmitter';\nimport NotificationsEmitterModule from './NotificationsEmitterModule';\nimport { mapNotificationResponse } from './utils/mapNotificationResponse';\n\n/**\n * A React hook always returns the notification response that was received most recently\n * (a notification response designates an interaction with a notification, such as tapping on it).\n *\n * > If you don't want to use a hook, you can use `Notifications.getLastNotificationResponseAsync()` instead.\n *\n * @return The hook may return one of these three types/values:\n * - `undefined` - until we're sure of what to return,\n * - `null` - if no notification response has been received yet,\n * - a [`NotificationResponse`](#notificationresponse) object - if a notification response was received.\n *\n * @example\n * Responding to a notification tap by opening a URL that could be put into the notification's `data`\n * (opening the URL is your responsibility and is not a part of the `expo-notifications` API):\n * ```jsx\n * import * as Notifications from 'expo-notifications';\n * import { Linking } from 'react-native';\n *\n * export default function App() {\n * const lastNotificationResponse = Notifications.useLastNotificationResponse();\n * React.useEffect(() => {\n * if (\n * lastNotificationResponse &&\n * lastNotificationResponse.notification.request.content.data.url &&\n * lastNotificationResponse.actionIdentifier === Notifications.DEFAULT_ACTION_IDENTIFIER\n * ) {\n * Linking.openURL(lastNotificationResponse.notification.request.content.data.url);\n * }\n * }, [lastNotificationResponse]);\n * return (\n * // Your app content\n * );\n * }\n * ```\n * @header listen\n */\nexport default function useLastNotificationResponse() {\n const [lastNotificationResponse, setLastNotificationResponse] = useState<\n NotificationResponse | null | undefined\n >(undefined);\n\n // useLayoutEffect ensures the listener is registered as soon as possible\n useLayoutEffect(() => {\n const subscription = addNotificationResponseReceivedListener((response) => {\n const mappedResponse = mapNotificationResponse(response);\n setLastNotificationResponse(mappedResponse);\n });\n return () => {\n subscription.remove();\n };\n }, []);\n\n // On each mount of this hook we fetch last notification response\n // from the native module which is an \"always active listener\"\n // and always returns the most recent response.\n useEffect(() => {\n NotificationsEmitterModule.getLastNotificationResponseAsync?.().then((response) => {\n // We only update the state with the resolved value if it's empty,\n // because if it's not empty it must have been populated by the `useLayoutEffect`\n // listener which returns \"live\" values.\n const mappedResponse = response ? mapNotificationResponse(response) : response;\n setLastNotificationResponse((currentResponse) => currentResponse ?? mappedResponse);\n });\n }, []);\n\n return lastNotificationResponse;\n}\n"]}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { NotificationResponse } from '../Notifications.types';
|
|
2
|
+
/**
|
|
3
|
+
* @hidden
|
|
4
|
+
*
|
|
5
|
+
* Does any required processing of a notification response from native code
|
|
6
|
+
* before it is passed to a notification response listener, or to the
|
|
7
|
+
* last notification response hook.
|
|
8
|
+
*
|
|
9
|
+
* @param response The raw response passed in from native code
|
|
10
|
+
* @returns the mapped response.
|
|
11
|
+
*/
|
|
12
|
+
export declare const mapNotificationResponse: (response: NotificationResponse) => NotificationResponse & {
|
|
13
|
+
notification: {
|
|
14
|
+
request: {
|
|
15
|
+
content: {
|
|
16
|
+
dataString?: string;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
//# sourceMappingURL=mapNotificationResponse.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mapNotificationResponse.d.ts","sourceRoot":"","sources":["../../src/utils/mapNotificationResponse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAE9D;;;;;;;;;GASG;AACH,eAAO,MAAM,uBAAuB,aAAc,oBAAoB;kBAEpD;QAAE,OAAO,EAAE;YAAE,OAAO,EAAE;gBAAE,UAAU,CAAC,EAAE,MAAM,CAAA;aAAE,CAAA;SAAE,CAAA;KAAE;CAalE,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hidden
|
|
3
|
+
*
|
|
4
|
+
* Does any required processing of a notification response from native code
|
|
5
|
+
* before it is passed to a notification response listener, or to the
|
|
6
|
+
* last notification response hook.
|
|
7
|
+
*
|
|
8
|
+
* @param response The raw response passed in from native code
|
|
9
|
+
* @returns the mapped response.
|
|
10
|
+
*/
|
|
11
|
+
export const mapNotificationResponse = (response) => {
|
|
12
|
+
const mappedResponse = { ...response };
|
|
13
|
+
try {
|
|
14
|
+
const dataString = mappedResponse?.notification?.request?.content['dataString'];
|
|
15
|
+
if (typeof dataString === 'string') {
|
|
16
|
+
mappedResponse.notification.request.content.data = JSON.parse(dataString);
|
|
17
|
+
delete mappedResponse.notification.request.content.dataString;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
catch (e) {
|
|
21
|
+
console.log(`Error in response: ${e}`);
|
|
22
|
+
}
|
|
23
|
+
console.log(`response received: ${JSON.stringify(mappedResponse, null, 2)}`);
|
|
24
|
+
return mappedResponse;
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=mapNotificationResponse.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mapNotificationResponse.js","sourceRoot":"","sources":["../../src/utils/mapNotificationResponse.ts"],"names":[],"mappings":"AAEA;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,QAA8B,EAAE,EAAE;IACxE,MAAM,cAAc,GAEhB,EAAE,GAAG,QAAQ,EAAE,CAAC;IACpB,IAAI;QACF,MAAM,UAAU,GAAG,cAAc,EAAE,YAAY,EAAE,OAAO,EAAE,OAAO,CAAC,YAAY,CAAC,CAAC;QAChF,IAAI,OAAO,UAAU,KAAK,QAAQ,EAAE;YAClC,cAAc,CAAC,YAAY,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;YAC1E,OAAO,cAAc,CAAC,YAAY,CAAC,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC;SAC/D;KACF;IAAC,OAAO,CAAM,EAAE;QACf,OAAO,CAAC,GAAG,CAAC,sBAAsB,CAAC,EAAE,CAAC,CAAC;KACxC;IACD,OAAO,CAAC,GAAG,CAAC,sBAAsB,IAAI,CAAC,SAAS,CAAC,cAAc,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC;IAC7E,OAAO,cAAc,CAAC;AACxB,CAAC,CAAC","sourcesContent":["import { NotificationResponse } from '../Notifications.types';\n\n/**\n * @hidden\n *\n * Does any required processing of a notification response from native code\n * before it is passed to a notification response listener, or to the\n * last notification response hook.\n *\n * @param response The raw response passed in from native code\n * @returns the mapped response.\n */\nexport const mapNotificationResponse = (response: NotificationResponse) => {\n const mappedResponse: NotificationResponse & {\n notification: { request: { content: { dataString?: string } } };\n } = { ...response };\n try {\n const dataString = mappedResponse?.notification?.request?.content['dataString'];\n if (typeof dataString === 'string') {\n mappedResponse.notification.request.content.data = JSON.parse(dataString);\n delete mappedResponse.notification.request.content.dataString;\n }\n } catch (e: any) {\n console.log(`Error in response: ${e}`);\n }\n console.log(`response received: ${JSON.stringify(mappedResponse, null, 2)}`);\n return mappedResponse;\n};\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "expo-notifications",
|
|
3
|
-
"version": "0.28.
|
|
3
|
+
"version": "0.28.4",
|
|
4
4
|
"description": "Notifications module",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"types": "build/index.d.ts",
|
|
@@ -55,5 +55,5 @@
|
|
|
55
55
|
"peerDependencies": {
|
|
56
56
|
"expo": "*"
|
|
57
57
|
},
|
|
58
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "979e9f1fc3cfa9c827700dcf99992e671cfdf63e"
|
|
59
59
|
}
|
|
@@ -2,6 +2,7 @@ import { EventEmitter, Subscription, UnavailabilityError } from 'expo-modules-co
|
|
|
2
2
|
|
|
3
3
|
import { Notification, NotificationResponse } from './Notifications.types';
|
|
4
4
|
import NotificationsEmitterModule from './NotificationsEmitterModule';
|
|
5
|
+
import { mapNotificationResponse } from './utils/mapNotificationResponse';
|
|
5
6
|
|
|
6
7
|
// Web uses SyntheticEventEmitter
|
|
7
8
|
const emitter = new EventEmitter(NotificationsEmitterModule);
|
|
@@ -87,19 +88,7 @@ export function addNotificationResponseReceivedListener(
|
|
|
87
88
|
return emitter.addListener<NotificationResponse>(
|
|
88
89
|
didReceiveNotificationResponseEventName,
|
|
89
90
|
(response: NotificationResponse) => {
|
|
90
|
-
const mappedResponse
|
|
91
|
-
notification: { request: { content: { dataString?: string } } };
|
|
92
|
-
} = { ...response };
|
|
93
|
-
try {
|
|
94
|
-
const dataString = mappedResponse?.notification?.request?.content['dataString'];
|
|
95
|
-
if (typeof dataString === 'string') {
|
|
96
|
-
mappedResponse.notification.request.content.data = JSON.parse(dataString);
|
|
97
|
-
delete mappedResponse.notification.request.content.dataString;
|
|
98
|
-
}
|
|
99
|
-
} catch (e: any) {
|
|
100
|
-
console.log(`Error in response: ${e}`);
|
|
101
|
-
}
|
|
102
|
-
console.log(`response received: ${JSON.stringify(mappedResponse, null, 2)}`);
|
|
91
|
+
const mappedResponse = mapNotificationResponse(response);
|
|
103
92
|
listener(mappedResponse);
|
|
104
93
|
}
|
|
105
94
|
);
|
|
@@ -3,6 +3,7 @@ import { useEffect, useLayoutEffect, useState } from 'react';
|
|
|
3
3
|
import { NotificationResponse } from './Notifications.types';
|
|
4
4
|
import { addNotificationResponseReceivedListener } from './NotificationsEmitter';
|
|
5
5
|
import NotificationsEmitterModule from './NotificationsEmitterModule';
|
|
6
|
+
import { mapNotificationResponse } from './utils/mapNotificationResponse';
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* A React hook always returns the notification response that was received most recently
|
|
@@ -48,7 +49,8 @@ export default function useLastNotificationResponse() {
|
|
|
48
49
|
// useLayoutEffect ensures the listener is registered as soon as possible
|
|
49
50
|
useLayoutEffect(() => {
|
|
50
51
|
const subscription = addNotificationResponseReceivedListener((response) => {
|
|
51
|
-
|
|
52
|
+
const mappedResponse = mapNotificationResponse(response);
|
|
53
|
+
setLastNotificationResponse(mappedResponse);
|
|
52
54
|
});
|
|
53
55
|
return () => {
|
|
54
56
|
subscription.remove();
|
|
@@ -63,7 +65,8 @@ export default function useLastNotificationResponse() {
|
|
|
63
65
|
// We only update the state with the resolved value if it's empty,
|
|
64
66
|
// because if it's not empty it must have been populated by the `useLayoutEffect`
|
|
65
67
|
// listener which returns "live" values.
|
|
66
|
-
|
|
68
|
+
const mappedResponse = response ? mapNotificationResponse(response) : response;
|
|
69
|
+
setLastNotificationResponse((currentResponse) => currentResponse ?? mappedResponse);
|
|
67
70
|
});
|
|
68
71
|
}, []);
|
|
69
72
|
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { NotificationResponse } from '../Notifications.types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @hidden
|
|
5
|
+
*
|
|
6
|
+
* Does any required processing of a notification response from native code
|
|
7
|
+
* before it is passed to a notification response listener, or to the
|
|
8
|
+
* last notification response hook.
|
|
9
|
+
*
|
|
10
|
+
* @param response The raw response passed in from native code
|
|
11
|
+
* @returns the mapped response.
|
|
12
|
+
*/
|
|
13
|
+
export const mapNotificationResponse = (response: NotificationResponse) => {
|
|
14
|
+
const mappedResponse: NotificationResponse & {
|
|
15
|
+
notification: { request: { content: { dataString?: string } } };
|
|
16
|
+
} = { ...response };
|
|
17
|
+
try {
|
|
18
|
+
const dataString = mappedResponse?.notification?.request?.content['dataString'];
|
|
19
|
+
if (typeof dataString === 'string') {
|
|
20
|
+
mappedResponse.notification.request.content.data = JSON.parse(dataString);
|
|
21
|
+
delete mappedResponse.notification.request.content.dataString;
|
|
22
|
+
}
|
|
23
|
+
} catch (e: any) {
|
|
24
|
+
console.log(`Error in response: ${e}`);
|
|
25
|
+
}
|
|
26
|
+
console.log(`response received: ${JSON.stringify(mappedResponse, null, 2)}`);
|
|
27
|
+
return mappedResponse;
|
|
28
|
+
};
|