From c2a5202a3b16df7741ce1b35dcc177aaffff5224 Mon Sep 17 00:00:00 2001 From: Gonzalo Lopez Date: Wed, 12 Jun 2024 11:41:00 -0300 Subject: [PATCH 1/3] added additionalInfo and updateSuscription method --- CHANGELOG.md | 10 ++- README.md | 10 ++- lib/NotificationContext.js | 1 + lib/NotificationProvider/index.js | 10 ++- lib/usePushNotification.js | 35 ++++++++++- lib/utils/api/SubscribeNotifications/index.js | 10 ++- test/usePushNotification.test.js | 61 +++++++++++++++++++ .../api/subscribeNotifications/index.test.js | 7 ++- 8 files changed, 135 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 789eed6..b639974 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,14 @@ # Changelog -[Unreleased] +## [Unreleased] -## [0.0.1] - 2024-05-03 +## [0.0.3] - 2024-06-13 + +### Added + +- additional info to send in subscription + +## [0.0.2] - 2024-05-03 ### Added diff --git a/README.md b/README.md index 368f599..fe491f6 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ For more information about this, read https://rnfirebase.io/reference/messaging/ ## Functions
-
NotificationProvider(children, appName, events, environment)null | React.element
+
NotificationProvider(children, appName, events, environment, additionalInfo)null | React.element

It is the main component of the package, it is a HOC that is responsible for handling the logic of subscribing to notifications and receiving messages from the Firebase console. The HOC contains listeners to listen to notifications in the foreground and background, so (unless we cancel the subscription), we will receive notifications from the app even when it is closed.

setupBackgroundMessageHandler(callback)
@@ -70,6 +70,10 @@ For more information about this, read https://rnfirebase.io/reference/messaging/ 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. +updateSuscription +This function is responsible for updating the subscription to the notification service + + addNewEvent This function allows you to add a new event to receive notifications. @@ -87,7 +91,7 @@ For more information about this, read https://rnfirebase.io/reference/messaging/ -## NotificationProvider(children, appName, events, environment) ⇒ null \| React.element +## NotificationProvider(children, appName, events, environment, additionalInfo) ⇒ null \| React.element It is the main component of the package, it is a HOC that is responsible for handling the logic of subscribing to notifications and receiving messages from the Firebase console. The HOC contains listeners to listen to notifications in the foreground and background, so (unless we cancel the subscription), we will receive notifications from the app even when it is closed. **Kind**: global function @@ -102,6 +106,7 @@ It is the main component of the package, it is a HOC that is responsible for han | appName | string | name of the aplication | | events | Array.<string> | is an array that will contain the events to which the user wants to subscribe | | environment | string | The environment is necessary for the API that we are going to use to subscribe the device to notifications. | +| additionalInfo | object | fields to be sent as part of the body of the subscription request | **Example** ```js @@ -139,6 +144,7 @@ is a hook, which returns the elements contained within the notifications context | backgroundNotification | An object containing all data received when a background push notification is triggered. | | 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. | + | updateSuscription | This function is responsible for updating the subscription to the notification service | | 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. | diff --git a/lib/NotificationContext.js b/lib/NotificationContext.js index 79cdd17..a5a005f 100644 --- a/lib/NotificationContext.js +++ b/lib/NotificationContext.js @@ -18,6 +18,7 @@ export const NotificationContext = React.createContext(null); * | backgroundNotification | An object containing all data received when a background push notification is triggered. | * | 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. | + * | updateSuscription | This function is responsible for updating the subscription to the notification service | * | 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. | diff --git a/lib/NotificationProvider/index.js b/lib/NotificationProvider/index.js index 6c370d0..291e4be 100644 --- a/lib/NotificationProvider/index.js +++ b/lib/NotificationProvider/index.js @@ -17,6 +17,7 @@ import usePushNotification from '../usePushNotification'; * @param {string} appName name of the aplication * @param {Array} events is an array that will contain the events to which the user wants to subscribe * @param {string} environment The environment is necessary for the API that we are going to use to subscribe the device to notifications. + * @param {object} additionalInfo fields to be sent as part of the body of the subscription request * @throws null when not receive a children argument * @returns {null | React.element} * @example @@ -34,7 +35,13 @@ import usePushNotification from '../usePushNotification'; * ) */ -const NotificationProvider = ({children, appName, events, environment}) => { +const NotificationProvider = ({ + children, + appName, + events, + environment, + additionalInfo, +}) => { if (!children) return null; const validAppName = !!appName && isString(appName) ? appName : ''; @@ -53,6 +60,7 @@ const NotificationProvider = ({children, appName, events, environment}) => { validEvents, validAppName, isRegistered, + additionalInfo, ); // @function handlerForegroundData diff --git a/lib/usePushNotification.js b/lib/usePushNotification.js index 32d96ad..6c16088 100644 --- a/lib/usePushNotification.js +++ b/lib/usePushNotification.js @@ -4,7 +4,13 @@ import {getFCMToken, isArray, isString} from './utils'; import SubscribeNotifications from './utils/api/SubscribeNotifications'; import UnSubscribeNotifications from './utils/api/UnSubscribeNotifications'; -const usePushNotification = (environment, events, appName, isRegistered) => { +const usePushNotification = ( + environment, + events, + appName, + isRegistered, + additionalInfo, +) => { const [notificationState, setNotificationState] = useState({ deviceToken: null, foregroundNotification: {}, @@ -54,6 +60,7 @@ const usePushNotification = (environment, events, appName, isRegistered) => { events: pushEvents, deviceToken: token, request: Request, + additionalInfo, }); isRegistered.current = true; @@ -63,6 +70,31 @@ const usePushNotification = (environment, events, appName, isRegistered) => { } }, [pushEvents]); + /** + * @function updateSuscription + * @description This function is responsible for updating the subscription to the notification service + * @param {object} props all properties that will be sent as additional data in the subscription + * @returns {Promise} + */ + + const updateSuscription = async (props = {}) => { + try { + const token = await getDeviceToken(); + + const response = await SubscribeNotifications({ + appName, + events: pushEvents, + deviceToken: token, + request: Request, + additionalInfo: props, + }); + + return response; + } catch (error) { + return Promise.reject(error); + } + }; + /** * @function cancelNotifications * @description 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. @@ -161,6 +193,7 @@ const usePushNotification = (environment, events, appName, isRegistered) => { updateNotificationState, getSubscribedEvents, deleteReceivedNotification, + updateSuscription, }; }; diff --git a/lib/utils/api/SubscribeNotifications/index.js b/lib/utils/api/SubscribeNotifications/index.js index d695308..ae0fe66 100644 --- a/lib/utils/api/SubscribeNotifications/index.js +++ b/lib/utils/api/SubscribeNotifications/index.js @@ -1,11 +1,11 @@ -import {isString, isArray} from '../../index'; +import {isString, isArray, isObject} from '../../index'; const SubscribeNotifications = async (params = {}) => { try { if (!params || !Object.keys(params).length) throw new Error('params is not a valid object'); - const {deviceToken, events, appName, request} = params; + const {deviceToken, events, appName, request, additionalInfo} = params; if (!deviceToken || !isString(deviceToken)) throw new Error('device token is invalid or null'); @@ -19,10 +19,16 @@ const SubscribeNotifications = async (params = {}) => { if (!parsedEvents.length) throw new Error('events to be suscribed are invalids'); + const validAdditionalInfo = + isObject(additionalInfo) && !!Object.keys(additionalInfo).length; + const body = { token: deviceToken, events: parsedEvents, platformApplicationName: appName, + ...(validAdditionalInfo && { + additionalInfo, + }), }; const response = await request.post({ diff --git a/test/usePushNotification.test.js b/test/usePushNotification.test.js index 95e71f5..1038a06 100644 --- a/test/usePushNotification.test.js +++ b/test/usePushNotification.test.js @@ -2,6 +2,7 @@ import {useState} from 'react'; import {renderHook} from '@testing-library/react-native'; import usePushNotification from '../lib/usePushNotification'; import * as UnSubscribeNotifications from '../lib/utils/api/UnSubscribeNotifications'; +import * as SubscribeNotifications from '../lib/utils/api/SubscribeNotifications'; import {promiseWrapper} from '../lib/utils'; describe('usePushNotification hook', () => { @@ -10,6 +11,11 @@ describe('usePushNotification hook', () => { 'default', ); + const spySubscribeNotifications = jest.spyOn( + SubscribeNotifications, + 'default', + ); + const initialState = { deviceToken: null, foregroundNotification: {}, @@ -37,6 +43,61 @@ describe('usePushNotification hook', () => { }); describe('the object contains:', () => { + describe('updateSuscription util', () => { + it('this util call the api to suscribe to notifications service', async () => { + spySubscribeNotifications.mockResolvedValueOnce({result: {}}); + useState.mockReturnValueOnce([ + { + ...initialState, + deviceToken: 'fcmToken', + pushEvents: ['picking:session:created'], + }, + mockSetState, + ]); + + const {result} = renderHook(() => + usePushNotification( + 'local', + ['picking:session:created'], + 'PickingApp', + {current: false}, + ), + ); + const {updateSuscription} = result.current; + + const [response] = await promiseWrapper( + updateSuscription({language: 'en-US'}), + ); + + await expect(response).toStrictEqual({result: {}}); + }); + + it('this util returns an error when the suscription has an error', async () => { + spySubscribeNotifications.mockRejectedValueOnce({message: 'error'}); + useState.mockReturnValueOnce([ + { + ...initialState, + deviceToken: 'fcmToken', + pushEvents: ['picking:session:created'], + }, + mockSetState, + ]); + + const {result} = renderHook(() => + usePushNotification( + 'local', + ['picking:session:created'], + 'PickingApp', + {current: false}, + ), + ); + const {updateSuscription} = result.current; + + const [, response] = await promiseWrapper(updateSuscription()); + + await expect(response).toStrictEqual({message: 'error'}); + }); + }); describe('cancelNotifications util', () => { it('this utils call the api and cancel the subscription to event passed an arguments or all events', async () => { spyUnSubscribeNotifications.mockResolvedValueOnce({result: {}}); diff --git a/test/utils/api/subscribeNotifications/index.test.js b/test/utils/api/subscribeNotifications/index.test.js index 578f5c1..005ac34 100644 --- a/test/utils/api/subscribeNotifications/index.test.js +++ b/test/utils/api/subscribeNotifications/index.test.js @@ -70,7 +70,12 @@ describe('SubscribeNotifications', () => { nock(server).post('/subscribe/push').reply(200, {}); - const response = await SubscribeNotifications(validParams); + const response = await SubscribeNotifications({ + ...validParams, + additionalInfo: { + language: 'en-US', + }, + }); expect(response).toStrictEqual({result: {}}); }); From c9951ebb16d8b5ae5b2fa4c198e1195353246d1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gonzalo=20L=C3=B3pez?= Date: Thu, 13 Jun 2024 11:33:45 -0300 Subject: [PATCH 2/3] added additionalInfo and updateSuscription method (#5) Co-authored-by: Gonzalo Lopez From d3d8571bdbe2b7c2e74cc9a51fbe7749ba13f8e8 Mon Sep 17 00:00:00 2001 From: Fernando Colom Date: Thu, 13 Jun 2024 11:34:03 -0300 Subject: [PATCH 3/3] 0.0.3 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8786aec..1f9a426 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@janiscommerce/app-push-notification", - "version": "0.0.2", + "version": "0.0.3", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@janiscommerce/app-push-notification", - "version": "0.0.2", + "version": "0.0.3", "license": "ISC", "dependencies": { "@janiscommerce/app-request": "^2.2.0", diff --git a/package.json b/package.json index 4f88af4..8a8dc0d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@janiscommerce/app-push-notification", - "version": "0.0.2", + "version": "0.0.3", "type": "commonjs", "description": "This package will take care of performing the main actions for registration to receive notifications in the foreground and background.", "main": "lib/index.js",