Skip to content

Commit

Permalink
util to clean notification states was added
Browse files Browse the repository at this point in the history
  • Loading branch information
Gonzalo Lopez committed Apr 30, 2024
1 parent cbcc23b commit eb3646f
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ For more information about this, read https://rnfirebase.io/reference/messaging/
<td>This function allows you to add a new event to receive notifications.</td>
</tr>
<tr>
<td>deleteReceivedNotification</td>
<td>An util that clears the foreground or background notification state to the depending on the type it receives by parameter</td>
</tr>
<tr>
<td>getSubscribedEvents</td>
<td>This function returns an array with the events to which the user is subscribed.</td>
</tr>
Expand Down Expand Up @@ -136,6 +140,7 @@ is a hook, which returns the elements contained within the notifications context
| subscribeError | An object containing all data received from a notification service subscription failure. |
| cancelNotifications | This util is responsible for making the request to unsubscribe from all notification events. If no arguments are received, the request will be made with the previously registered events. |
| addNewEvent | This function allows you to add a new event to receive notifications. |
| deleteReceivedNotification | An util that clears the foreground or background notification state to the depending on the type it receives by parameter
| getSubscribedEvents | This function returns an array with the events to which the user is subscribed. |

**Kind**: global function
Expand Down
1 change: 1 addition & 0 deletions lib/NotificationContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const NotificationContext = React.createContext(null);
* | subscribeError | An object containing all data received from a notification service subscription failure. |
* | cancelNotifications | This util is responsible for making the request to unsubscribe from all notification events. If no arguments are received, the request will be made with the previously registered events. |
* | addNewEvent | This function allows you to add a new event to receive notifications. |
* | deleteReceivedNotification | An util that clears the foreground or background notification state to the depending on the type it receives by parameter
* | getSubscribedEvents | This function returns an array with the events to which the user is subscribed. |
* @returns {object}
* @example
Expand Down
32 changes: 32 additions & 0 deletions lib/usePushNotification.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,37 @@ const usePushNotification = (environment, events, appName, isRegistered) => {
}
};

/**
* @function deleteReceivedNotification
* @description This utility allows you to reset the state corresponding to the type of notification received as an argument.
* @param {string} notificationType the type of notification you want to delete, it can be a foreground or background notification
* @returns {null}
*
* @example
* import {usePushNotification} from '@janiscommerce/app-push-notification
*
* const {deleteReceivedNotification} = usePushNotification()
*
* const resetForegroundNotification = () => {
* deleteReceivedNotification('foreground')
* }
*/

const deleteReceivedNotification = (params = {}) => {
const {type = ''} = params
const allowTypes = ['foreground','background']
const deleteNotification = {
foreground: () => updateNotificationState({foregroundNotification:{}}),
background: () => updateNotificationState({backgroundNotification:{}})
}

if(!type || !allowTypes.includes(type)) return null;

const restartNotification = deleteNotification[type]

return restartNotification()
}

/**
* @function addNewEvent
* @description This function allows you to add a new event to receive notifications
Expand Down Expand Up @@ -129,6 +160,7 @@ const usePushNotification = (environment, events, appName, isRegistered) => {
registerDeviceToNotifications,
updateNotificationState,
getSubscribedEvents,
deleteReceivedNotification
};
};

Expand Down
2 changes: 1 addition & 1 deletion lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export const setupForegroundMessageHandler = (callback) =>
* @param {Function} callback is the function that will receive the payload and render it as appropriate
*/

export const setupBackgroundMessageHandler = (callback) =>
export const setupBackgroundMessageHandler = (callback = () => {}) =>
messaging().setBackgroundMessageHandler(async (remoteMessage) => {
callback(remoteMessage);
});
Expand Down
64 changes: 64 additions & 0 deletions test/usePushNotification.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,5 +230,69 @@ describe('usePushNotification hook', () => {
expect(response).toStrictEqual(['picking:session:created']);
});
});

describe('deleteReceivedNotification util', () => {
describe('should remove the information of the selected notification type from the notification status', () => {
const mockState = {
foregroundNotification:{data:{},notification:{}},
backgroundNotification:{data:{},notification:{}},
deviceToken: 'fcmToken',
pushEvents: ['picking:session:created']
}

it('if the selected type is foreground then should reset the foreground notification information', () => {
useState.mockReturnValueOnce([mockState,mockSetState]);

const {result} = renderHook(() =>
usePushNotification(
'local',
['picking:session:created'],
'PickingApp',
{current: false},
),
);
const {deleteReceivedNotification} = result.current;
deleteReceivedNotification({type:'foreground'})

expect(mockSetState).toHaveBeenCalledWith({...mockState,foregroundNotification:{}})
})

it('if the selected type is background then should reset the background notification information', () => {
useState.mockReturnValueOnce([mockState,mockSetState]);

const {result} = renderHook(() =>
usePushNotification(
'local',
['picking:session:created'],
'PickingApp',
{current: false},
),
);
const {deleteReceivedNotification} = result.current;

deleteReceivedNotification({type:'background'})

expect(mockSetState).toHaveBeenCalledWith({...mockState,backgroundNotification:{}})
})

it('if type is not pass return null', () => {
useState.mockReturnValueOnce([mockState,mockSetState]);

const {result} = renderHook(() =>
usePushNotification(
'local',
['picking:session:created'],
'PickingApp',
{current: false},
),
);
const {deleteReceivedNotification} = result.current;

deleteReceivedNotification()

expect(mockSetState).not.toHaveBeenCalled()
})
})
})
});
});
7 changes: 7 additions & 0 deletions test/utils/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ describe('utils', () => {
expect(mockBackgroundMessageHandler).toHaveBeenCalledTimes(1);
expect(mockCallback).toHaveBeenCalledWith(fakeRemoteMessage);
});
it('calls setBackgroundMessageHandler with remoteMessage', () => {
setupBackgroundMessageHandler();

mockBackgroundMessageHandler.mock.calls[0][0](fakeRemoteMessage);

expect(mockBackgroundMessageHandler).toHaveBeenCalledTimes(1);
});
});

describe('setupNotificationOpenedHandler provides the listener that listens for the opening of the app from the background through a notification', () => {
Expand Down

0 comments on commit eb3646f

Please sign in to comment.