diff --git a/README.md b/README.md index a93d5a0..368f599 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,10 @@ For more information about this, read https://rnfirebase.io/reference/messaging/ 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. @@ -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 diff --git a/lib/NotificationContext.js b/lib/NotificationContext.js index 35c2dbc..79cdd17 100644 --- a/lib/NotificationContext.js +++ b/lib/NotificationContext.js @@ -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 diff --git a/lib/usePushNotification.js b/lib/usePushNotification.js index 74e1cea..9962d2d 100644 --- a/lib/usePushNotification.js +++ b/lib/usePushNotification.js @@ -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 @@ -129,6 +160,7 @@ const usePushNotification = (environment, events, appName, isRegistered) => { registerDeviceToNotifications, updateNotificationState, getSubscribedEvents, + deleteReceivedNotification }; }; diff --git a/lib/utils/index.js b/lib/utils/index.js index 77731f7..78894cc 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -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); }); diff --git a/test/usePushNotification.test.js b/test/usePushNotification.test.js index aaa66f5..d03e0b4 100644 --- a/test/usePushNotification.test.js +++ b/test/usePushNotification.test.js @@ -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() + }) + }) + }) }); }); diff --git a/test/utils/index.test.js b/test/utils/index.test.js index 456bf87..19cb966 100644 --- a/test/utils/index.test.js +++ b/test/utils/index.test.js @@ -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', () => {