From d9255ab362547b664917cb9fc505eb64d169a60e Mon Sep 17 00:00:00 2001 From: Sahand Seifi Date: Sat, 20 Dec 2025 13:12:10 -0500 Subject: [PATCH 1/2] onNewNotifications handler --- lib/components/Provider/index.tsx | 32 +++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/components/Provider/index.tsx b/lib/components/Provider/index.tsx index 5d1e72c..8a1a35f 100644 --- a/lib/components/Provider/index.tsx +++ b/lib/components/Provider/index.tsx @@ -46,6 +46,7 @@ type Props = ( webPushOptInMessage?: 'AUTOMATIC' | boolean; customServiceWorkerPath?: string; debug?: boolean; + onNewNotification?: (notifications: InAppNotification[]) => void; }; // Ensure that the code runs only in the browser @@ -180,6 +181,25 @@ export const NotificationAPIProvider: React.FunctionComponent< [debug] ); + const { onNewNotification } = props; + const handleNewInAppNotifications = useCallback( + (notifications: InAppNotification[]) => { + debug.log('Received new in-app notifications via WebSocket', { + count: notifications?.length || 0, + notifications + }); + playSound(); + addNotificationsToState(notifications); + if (onNewNotification) { + debug.log('Calling onNewNotification callback', { + count: notifications?.length || 0 + }); + onNewNotification(notifications); + } + }, + [playSound, addNotificationsToState, onNewNotification, debug] + ); + const client = useMemo(() => { debug.group('Initializing NotificationAPI client'); @@ -198,14 +218,7 @@ export const NotificationAPIProvider: React.FunctionComponent< ? props.client : NotificationAPIClientSDK.init({ ...clientConfig, - onNewInAppNotifications: (notifications) => { - debug.log('Received new in-app notifications via WebSocket', { - count: notifications?.length || 0, - notifications - }); - playSound(); - addNotificationsToState(notifications); - } + onNewInAppNotifications: handleNewInAppNotifications }); // identify user @@ -226,8 +239,7 @@ export const NotificationAPIProvider: React.FunctionComponent< config.user.number, config.hashedUserId, config.debug, - addNotificationsToState, - playSound, + handleNewInAppNotifications, props.client, config.apiURL, config.wsURL, From 34ea97241b5bbcf2f500d9a654cbce9467d4288a Mon Sep 17 00:00:00 2001 From: Sahand Seifi Date: Sat, 20 Dec 2025 13:12:11 -0500 Subject: [PATCH 2/2] Add onNewNotifications callback to React SDK --- lib/components/Provider/index.tsx | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/components/Provider/index.tsx b/lib/components/Provider/index.tsx index 8a1a35f..e20d8af 100644 --- a/lib/components/Provider/index.tsx +++ b/lib/components/Provider/index.tsx @@ -46,7 +46,7 @@ type Props = ( webPushOptInMessage?: 'AUTOMATIC' | boolean; customServiceWorkerPath?: string; debug?: boolean; - onNewNotification?: (notifications: InAppNotification[]) => void; + onNewNotifications?: (notifications: InAppNotification[]) => void; }; // Ensure that the code runs only in the browser @@ -181,7 +181,7 @@ export const NotificationAPIProvider: React.FunctionComponent< [debug] ); - const { onNewNotification } = props; + const { onNewNotifications } = props; const handleNewInAppNotifications = useCallback( (notifications: InAppNotification[]) => { debug.log('Received new in-app notifications via WebSocket', { @@ -190,14 +190,14 @@ export const NotificationAPIProvider: React.FunctionComponent< }); playSound(); addNotificationsToState(notifications); - if (onNewNotification) { - debug.log('Calling onNewNotification callback', { + if (onNewNotifications) { + debug.log('Calling onNewNotifications callback', { count: notifications?.length || 0 }); - onNewNotification(notifications); + onNewNotifications(notifications); } }, - [playSound, addNotificationsToState, onNewNotification, debug] + [playSound, addNotificationsToState, onNewNotifications, debug] ); const client = useMemo(() => {