@janiscommerce/app-push-notification 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,8 +1,14 @@
1
1
  # Changelog
2
2
 
3
- [Unreleased]
3
+ ## [Unreleased]
4
4
 
5
- ## [0.0.1] - 2024-05-03
5
+ ## [0.0.3] - 2024-06-13
6
+
7
+ ### Added
8
+
9
+ - additional info to send in subscription
10
+
11
+ ## [0.0.2] - 2024-05-03
6
12
 
7
13
  ### Added
8
14
 
package/README.md CHANGED
@@ -34,7 +34,7 @@ For more information about this, read https://rnfirebase.io/reference/messaging/
34
34
  ## Functions
35
35
 
36
36
  <dl>
37
- <dt><a href="#NotificationProvider">NotificationProvider(children, appName, events, environment)</a> ⇒ <code>null</code> | <code>React.element</code></dt>
37
+ <dt><a href="#NotificationProvider">NotificationProvider(children, appName, events, environment, additionalInfo)</a> ⇒ <code>null</code> | <code>React.element</code></dt>
38
38
  <dd><p>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.</p>
39
39
  </dd>
40
40
  <dt><a href="#setupBackgroundMessageHandler">setupBackgroundMessageHandler(callback)</a></dt>
@@ -70,6 +70,10 @@ For more information about this, read https://rnfirebase.io/reference/messaging/
70
70
  <td>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.</td>
71
71
  </tr>
72
72
  <tr>
73
+ <td>updateSuscription</td>
74
+ <td>This function is responsible for updating the subscription to the notification service</td>
75
+ </tr>
76
+ <tr>
73
77
  <td>addNewEvent</td>
74
78
  <td>This function allows you to add a new event to receive notifications.</td>
75
79
  </tr>
@@ -87,7 +91,7 @@ For more information about this, read https://rnfirebase.io/reference/messaging/
87
91
 
88
92
  <a name="NotificationProvider"></a>
89
93
 
90
- ## NotificationProvider(children, appName, events, environment) ⇒ <code>null</code> \| <code>React.element</code>
94
+ ## NotificationProvider(children, appName, events, environment, additionalInfo) ⇒ <code>null</code> \| <code>React.element</code>
91
95
  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.
92
96
 
93
97
  **Kind**: global function
@@ -102,6 +106,7 @@ It is the main component of the package, it is a HOC that is responsible for han
102
106
  | appName | <code>string</code> | name of the aplication |
103
107
  | events | <code>Array.&lt;string&gt;</code> | is an array that will contain the events to which the user wants to subscribe |
104
108
  | environment | <code>string</code> | The environment is necessary for the API that we are going to use to subscribe the device to notifications. |
109
+ | additionalInfo | <code>object</code> | fields to be sent as part of the body of the subscription request |
105
110
 
106
111
  **Example**
107
112
  ```js
@@ -139,6 +144,7 @@ is a hook, which returns the elements contained within the notifications context
139
144
  | backgroundNotification | An object containing all data received when a background push notification is triggered. |
140
145
  | subscribeError | An object containing all data received from a notification service subscription failure. |
141
146
  | 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. |
147
+ | updateSuscription | This function is responsible for updating the subscription to the notification service |
142
148
  | addNewEvent | This function allows you to add a new event to receive notifications. |
143
149
  | deleteReceivedNotification | An util that clears the foreground or background notification state to the depending on the type it receives by parameter
144
150
  | getSubscribedEvents | This function returns an array with the events to which the user is subscribed. |
@@ -18,6 +18,7 @@ export const NotificationContext = React.createContext(null);
18
18
  * | backgroundNotification | An object containing all data received when a background push notification is triggered. |
19
19
  * | subscribeError | An object containing all data received from a notification service subscription failure. |
20
20
  * | 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. |
21
+ * | updateSuscription | This function is responsible for updating the subscription to the notification service |
21
22
  * | addNewEvent | This function allows you to add a new event to receive notifications. |
22
23
  * | deleteReceivedNotification | An util that clears the foreground or background notification state to the depending on the type it receives by parameter
23
24
  * | getSubscribedEvents | This function returns an array with the events to which the user is subscribed. |
@@ -17,6 +17,7 @@ import usePushNotification from '../usePushNotification';
17
17
  * @param {string} appName name of the aplication
18
18
  * @param {Array<string>} events is an array that will contain the events to which the user wants to subscribe
19
19
  * @param {string} environment The environment is necessary for the API that we are going to use to subscribe the device to notifications.
20
+ * @param {object} additionalInfo fields to be sent as part of the body of the subscription request
20
21
  * @throws null when not receive a children argument
21
22
  * @returns {null | React.element}
22
23
  * @example
@@ -34,7 +35,13 @@ import usePushNotification from '../usePushNotification';
34
35
  * )
35
36
  */
36
37
 
37
- const NotificationProvider = ({children, appName, events, environment}) => {
38
+ const NotificationProvider = ({
39
+ children,
40
+ appName,
41
+ events,
42
+ environment,
43
+ additionalInfo,
44
+ }) => {
38
45
  if (!children) return null;
39
46
 
40
47
  const validAppName = !!appName && isString(appName) ? appName : '';
@@ -53,6 +60,7 @@ const NotificationProvider = ({children, appName, events, environment}) => {
53
60
  validEvents,
54
61
  validAppName,
55
62
  isRegistered,
63
+ additionalInfo,
56
64
  );
57
65
 
58
66
  // @function handlerForegroundData
@@ -4,7 +4,13 @@ import {getFCMToken, isArray, isString} from './utils';
4
4
  import SubscribeNotifications from './utils/api/SubscribeNotifications';
5
5
  import UnSubscribeNotifications from './utils/api/UnSubscribeNotifications';
6
6
 
7
- const usePushNotification = (environment, events, appName, isRegistered) => {
7
+ const usePushNotification = (
8
+ environment,
9
+ events,
10
+ appName,
11
+ isRegistered,
12
+ additionalInfo,
13
+ ) => {
8
14
  const [notificationState, setNotificationState] = useState({
9
15
  deviceToken: null,
10
16
  foregroundNotification: {},
@@ -54,6 +60,7 @@ const usePushNotification = (environment, events, appName, isRegistered) => {
54
60
  events: pushEvents,
55
61
  deviceToken: token,
56
62
  request: Request,
63
+ additionalInfo,
57
64
  });
58
65
 
59
66
  isRegistered.current = true;
@@ -63,6 +70,31 @@ const usePushNotification = (environment, events, appName, isRegistered) => {
63
70
  }
64
71
  }, [pushEvents]);
65
72
 
73
+ /**
74
+ * @function updateSuscription
75
+ * @description This function is responsible for updating the subscription to the notification service
76
+ * @param {object} props all properties that will be sent as additional data in the subscription
77
+ * @returns {Promise}
78
+ */
79
+
80
+ const updateSuscription = async (props = {}) => {
81
+ try {
82
+ const token = await getDeviceToken();
83
+
84
+ const response = await SubscribeNotifications({
85
+ appName,
86
+ events: pushEvents,
87
+ deviceToken: token,
88
+ request: Request,
89
+ additionalInfo: props,
90
+ });
91
+
92
+ return response;
93
+ } catch (error) {
94
+ return Promise.reject(error);
95
+ }
96
+ };
97
+
66
98
  /**
67
99
  * @function cancelNotifications
68
100
  * @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) => {
161
193
  updateNotificationState,
162
194
  getSubscribedEvents,
163
195
  deleteReceivedNotification,
196
+ updateSuscription,
164
197
  };
165
198
  };
166
199
 
@@ -1,11 +1,11 @@
1
- import {isString, isArray} from '../../index';
1
+ import {isString, isArray, isObject} from '../../index';
2
2
 
3
3
  const SubscribeNotifications = async (params = {}) => {
4
4
  try {
5
5
  if (!params || !Object.keys(params).length)
6
6
  throw new Error('params is not a valid object');
7
7
 
8
- const {deviceToken, events, appName, request} = params;
8
+ const {deviceToken, events, appName, request, additionalInfo} = params;
9
9
 
10
10
  if (!deviceToken || !isString(deviceToken))
11
11
  throw new Error('device token is invalid or null');
@@ -19,10 +19,16 @@ const SubscribeNotifications = async (params = {}) => {
19
19
  if (!parsedEvents.length)
20
20
  throw new Error('events to be suscribed are invalids');
21
21
 
22
+ const validAdditionalInfo =
23
+ isObject(additionalInfo) && !!Object.keys(additionalInfo).length;
24
+
22
25
  const body = {
23
26
  token: deviceToken,
24
27
  events: parsedEvents,
25
28
  platformApplicationName: appName,
29
+ ...(validAdditionalInfo && {
30
+ additionalInfo,
31
+ }),
26
32
  };
27
33
 
28
34
  const response = await request.post({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@janiscommerce/app-push-notification",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "type": "commonjs",
5
5
  "description": "This package will take care of performing the main actions for registration to receive notifications in the foreground and background.",
6
6
  "main": "lib/index.js",