diff --git a/CHANGELOG.md b/CHANGELOG.md index 789eed6..b54db80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,18 @@ # Changelog -[Unreleased] +## [Unreleased] -## [0.0.1] - 2024-05-03 +### Added + +- Added notification channels + +## [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 5289134..4ab7184 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, channelConfigs)null | React.element
+
NotificationProvider(children, appName, events, environment, additionalInfo, channelConfigs)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, channelConfigs) ⇒ null \| React.element +## NotificationProvider(children, appName, events, environment, additionalInfo, channelConfigs) ⇒ 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 | | channelConfigs | Array.<(string\|object)> | is the configuration that will be used to create new notification channels | **Example** @@ -140,6 +145,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 91a3686..b56c9bf 100644 --- a/lib/NotificationProvider/index.js +++ b/lib/NotificationProvider/index.js @@ -22,6 +22,7 @@ import { * @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 * @param {Array} channelConfigs is the configuration that will be used to create new notification channels * @throws null when not receive a children argument * @returns {null | React.element} @@ -45,6 +46,7 @@ const NotificationProvider = ({ appName, events, environment, + additionalInfo, channelConfigs = [], }) => { if (!children) return null; @@ -67,6 +69,7 @@ const NotificationProvider = ({ 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/package-lock.json b/package-lock.json index 32e716c..56cf257 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 5246c24..2707af6 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", 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: {}}); });