Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions lib/components/Provider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type Props = (
webPushOptInMessage?: 'AUTOMATIC' | boolean;
customServiceWorkerPath?: string;
debug?: boolean;
onNewNotifications?: (notifications: InAppNotification[]) => void;
};

// Ensure that the code runs only in the browser
Expand Down Expand Up @@ -180,6 +181,25 @@ export const NotificationAPIProvider: React.FunctionComponent<
[debug]
);

const { onNewNotifications } = props;
const handleNewInAppNotifications = useCallback(
(notifications: InAppNotification[]) => {
debug.log('Received new in-app notifications via WebSocket', {
count: notifications?.length || 0,
notifications
});
playSound();
addNotificationsToState(notifications);
if (onNewNotifications) {
debug.log('Calling onNewNotifications callback', {
count: notifications?.length || 0
});
onNewNotifications(notifications);
}
},
[playSound, addNotificationsToState, onNewNotifications, debug]
);

const client = useMemo(() => {
debug.group('Initializing NotificationAPI client');

Expand All @@ -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
Expand All @@ -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,
Expand Down