@janiscommerce/app-push-notification 0.0.3 → 0.0.4
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 +6 -0
- package/README.md +3 -2
- package/lib/NotificationProvider/index.js +25 -0
- package/lib/utils/channel/index.js +71 -0
- package/package.json +2 -1
package/CHANGELOG.md
CHANGED
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, additionalInfo)</a> ⇒ <code>null</code> | <code>React.element</code></dt>
|
|
37
|
+
<dt><a href="#NotificationProvider">NotificationProvider(children, appName, events, environment, additionalInfo, channelConfigs)</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>
|
|
@@ -91,7 +91,7 @@ For more information about this, read https://rnfirebase.io/reference/messaging/
|
|
|
91
91
|
|
|
92
92
|
<a name="NotificationProvider"></a>
|
|
93
93
|
|
|
94
|
-
## NotificationProvider(children, appName, events, environment, additionalInfo) ⇒ <code>null</code> \| <code>React.element</code>
|
|
94
|
+
## NotificationProvider(children, appName, events, environment, additionalInfo, channelConfigs) ⇒ <code>null</code> \| <code>React.element</code>
|
|
95
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.
|
|
96
96
|
|
|
97
97
|
**Kind**: global function
|
|
@@ -107,6 +107,7 @@ It is the main component of the package, it is a HOC that is responsible for han
|
|
|
107
107
|
| events | <code>Array.<string></code> | is an array that will contain the events to which the user wants to subscribe |
|
|
108
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
109
|
| additionalInfo | <code>object</code> | fields to be sent as part of the body of the subscription request |
|
|
110
|
+
| channelConfigs | <code>Array.<(string\|object)></code> | is the configuration that will be used to create new notification channels |
|
|
110
111
|
|
|
111
112
|
**Example**
|
|
112
113
|
```js
|
|
@@ -9,6 +9,11 @@ import {
|
|
|
9
9
|
isObject,
|
|
10
10
|
} from '../utils';
|
|
11
11
|
import usePushNotification from '../usePushNotification';
|
|
12
|
+
import {
|
|
13
|
+
makeDefaultChannel,
|
|
14
|
+
makeNotificationChannels,
|
|
15
|
+
parseNotificationChannel,
|
|
16
|
+
} from '../utils/channel';
|
|
12
17
|
|
|
13
18
|
/**
|
|
14
19
|
* @function NotificationProvider
|
|
@@ -18,6 +23,7 @@ import usePushNotification from '../usePushNotification';
|
|
|
18
23
|
* @param {Array<string>} events is an array that will contain the events to which the user wants to subscribe
|
|
19
24
|
* @param {string} environment The environment is necessary for the API that we are going to use to subscribe the device to notifications.
|
|
20
25
|
* @param {object} additionalInfo fields to be sent as part of the body of the subscription request
|
|
26
|
+
* @param {Array<string | object>} channelConfigs is the configuration that will be used to create new notification channels
|
|
21
27
|
* @throws null when not receive a children argument
|
|
22
28
|
* @returns {null | React.element}
|
|
23
29
|
* @example
|
|
@@ -41,6 +47,7 @@ const NotificationProvider = ({
|
|
|
41
47
|
events,
|
|
42
48
|
environment,
|
|
43
49
|
additionalInfo,
|
|
50
|
+
channelConfigs = [],
|
|
44
51
|
}) => {
|
|
45
52
|
if (!children) return null;
|
|
46
53
|
|
|
@@ -48,6 +55,8 @@ const NotificationProvider = ({
|
|
|
48
55
|
const validEnvironment =
|
|
49
56
|
!!environment && isString(environment) ? environment : '';
|
|
50
57
|
const validEvents = !!events && isArray(events) ? events : [];
|
|
58
|
+
const validChannelConfigs =
|
|
59
|
+
!!channelConfigs && isArray(channelConfigs) ? channelConfigs : [];
|
|
51
60
|
|
|
52
61
|
const isRegistered = useRef(false);
|
|
53
62
|
const {
|
|
@@ -95,6 +104,18 @@ const NotificationProvider = ({
|
|
|
95
104
|
return updateNotificationState({backgroundNotification: data});
|
|
96
105
|
};
|
|
97
106
|
|
|
107
|
+
const createNotificationChannels = async () => {
|
|
108
|
+
/* istanbul ignore else */
|
|
109
|
+
if (validChannelConfigs) {
|
|
110
|
+
const parsedChannelConfigs = validChannelConfigs
|
|
111
|
+
?.map((config) => parseNotificationChannel(config))
|
|
112
|
+
.filter(Boolean);
|
|
113
|
+
|
|
114
|
+
await makeNotificationChannels(parsedChannelConfigs);
|
|
115
|
+
}
|
|
116
|
+
await makeDefaultChannel();
|
|
117
|
+
};
|
|
118
|
+
|
|
98
119
|
useEffect(() => {
|
|
99
120
|
const alreadySuscribed = isRegistered.current;
|
|
100
121
|
if (environment && appName && !!pushEvents.length && !alreadySuscribed) {
|
|
@@ -102,6 +123,10 @@ const NotificationProvider = ({
|
|
|
102
123
|
}
|
|
103
124
|
}, [pushEvents]);
|
|
104
125
|
|
|
126
|
+
useEffect(() => {
|
|
127
|
+
createNotificationChannels();
|
|
128
|
+
}, []);
|
|
129
|
+
|
|
105
130
|
/* istanbul ignore next */
|
|
106
131
|
useEffect(() => {
|
|
107
132
|
const foregroundMessageHandler = setupForegroundMessageHandler(
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import notifee, {AndroidImportance} from '@notifee/react-native';
|
|
2
|
+
import {isObject, isString} from '..';
|
|
3
|
+
|
|
4
|
+
export const parseChannelConfiguration = (params = {}) => {
|
|
5
|
+
if (!params || !isObject(params)) return null;
|
|
6
|
+
|
|
7
|
+
const {name, id = '', description = ''} = params;
|
|
8
|
+
|
|
9
|
+
if (!name || !isString(name)) return null;
|
|
10
|
+
|
|
11
|
+
const isValidDescription = !!description && isString(description);
|
|
12
|
+
const hasValidId = !!id && isString(id);
|
|
13
|
+
|
|
14
|
+
return {
|
|
15
|
+
name,
|
|
16
|
+
id: hasValidId ? id : name,
|
|
17
|
+
importance: AndroidImportance.HIGH,
|
|
18
|
+
...(isValidDescription && {
|
|
19
|
+
description,
|
|
20
|
+
}),
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const parseNotificationChannel = (channel) => {
|
|
25
|
+
const channelType = typeof channel;
|
|
26
|
+
const allowedConfigs = ['string', 'object'];
|
|
27
|
+
|
|
28
|
+
if (!allowedConfigs.includes(channelType)) return null;
|
|
29
|
+
const channelData = channelType === 'string' ? {name: channel} : channel;
|
|
30
|
+
|
|
31
|
+
const parsedChannel = parseChannelConfiguration(channelData);
|
|
32
|
+
|
|
33
|
+
return parsedChannel;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/* eslint-disable consistent-return */
|
|
37
|
+
export const makeNotificationChannel = async (channelConfig = {}) => {
|
|
38
|
+
const {id, name} = channelConfig;
|
|
39
|
+
|
|
40
|
+
if (!id || !name || !isString(id) || !isString(name)) return null;
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
await notifee.createChannel(channelConfig);
|
|
44
|
+
} catch (error) {
|
|
45
|
+
return Promise.reject(error);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/* eslint-disable consistent-return */
|
|
50
|
+
export const makeNotificationChannels = async (channelConfigs) => {
|
|
51
|
+
try {
|
|
52
|
+
await notifee.createChannels(channelConfigs);
|
|
53
|
+
} catch (error) {
|
|
54
|
+
return Promise.reject(error);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/* eslint-disable consistent-return */
|
|
59
|
+
export const makeDefaultChannel = async () => {
|
|
60
|
+
try {
|
|
61
|
+
const parsedChannel = parseChannelConfiguration({
|
|
62
|
+
id: 'default_channel',
|
|
63
|
+
name: 'Common notifications',
|
|
64
|
+
description: 'Default channel to receive notifications',
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
await makeNotificationChannel(parsedChannel);
|
|
68
|
+
} catch (error) {
|
|
69
|
+
return Promise.reject(error);
|
|
70
|
+
}
|
|
71
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@janiscommerce/app-push-notification",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
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",
|
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
"license": "ISC",
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"@janiscommerce/app-request": "^2.2.0",
|
|
31
|
+
"@notifee/react-native": "^7.8.2",
|
|
31
32
|
"@react-native-async-storage/async-storage": "^1.18.1",
|
|
32
33
|
"@react-native-firebase/app": "^18.3.1",
|
|
33
34
|
"@react-native-firebase/messaging": "^18.3.1",
|