@janiscommerce/app-push-notification 0.0.4 → 0.0.6
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 +12 -0
- package/README.md +26 -1
- package/lib/NotificationProvider/index.js +8 -1
- package/lib/constants/defaultChannelConfigs.js +13 -0
- package/lib/index.js +6 -1
- package/lib/usePushNotification.js +5 -0
- package/lib/utils/api/cancelNotificationsSubscription/index.js +39 -0
- package/lib/utils/channel/index.js +9 -5
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -29,6 +29,31 @@ Inside remoteMessage you get the notifications object that contains the informat
|
|
|
29
29
|
|
|
30
30
|
For more information about this, read https://rnfirebase.io/reference/messaging/remotemessage
|
|
31
31
|
|
|
32
|
+
## Customize background notification sound:
|
|
33
|
+
|
|
34
|
+
To customize the background notification sound, you can pass the `backgroundNotificationSound` parameter to the `NotificationProvider` component. By default, it uses the 'default' sound.
|
|
35
|
+
|
|
36
|
+
```javascript
|
|
37
|
+
import NotificationProvider from '@janiscommerce/app-push-notification'
|
|
38
|
+
|
|
39
|
+
return (
|
|
40
|
+
<NotificationProvider
|
|
41
|
+
appName='pickingApp'
|
|
42
|
+
events={["picking:session:created","picking:session:assigned"]}
|
|
43
|
+
environment='beta'
|
|
44
|
+
backgroundNotificationSound='custom_sound' // Custom sound file name
|
|
45
|
+
>
|
|
46
|
+
<MyComponent/>
|
|
47
|
+
</NotificationProvider>
|
|
48
|
+
)
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
To use a custom sound on Android, you must:
|
|
52
|
+
1. Place the sound file in the `android/app/src/main/res/raw/` folder
|
|
53
|
+
2. The file name must be lowercase and without special characters
|
|
54
|
+
3. The sound file must be in .mp3, .wav, or .aac format
|
|
55
|
+
4. Pass the exactly file name (without extension) as the value of `backgroundNotificationSound`
|
|
56
|
+
|
|
32
57
|
# This library provides the following components and methods:
|
|
33
58
|
|
|
34
59
|
## Functions
|
|
@@ -67,7 +92,7 @@ For more information about this, read https://rnfirebase.io/reference/messaging/
|
|
|
67
92
|
</tr>
|
|
68
93
|
<tr>
|
|
69
94
|
<td>cancelNotifications</td>
|
|
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>
|
|
95
|
+
<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. <em>⚠️ Deprecated: Use cancelNotificationsSubscription instead.</em></td>
|
|
71
96
|
</tr>
|
|
72
97
|
<tr>
|
|
73
98
|
<td>updateSuscription</td>
|
|
@@ -24,6 +24,7 @@ import {
|
|
|
24
24
|
* @param {string} environment The environment is necessary for the API that we are going to use to subscribe the device to notifications.
|
|
25
25
|
* @param {object} additionalInfo fields to be sent as part of the body of the subscription request
|
|
26
26
|
* @param {Array<string | object>} channelConfigs is the configuration that will be used to create new notification channels
|
|
27
|
+
* @param {string} backgroundNotificationSound is the sound that will be played when the app is in the background
|
|
27
28
|
* @throws null when not receive a children argument
|
|
28
29
|
* @returns {null | React.element}
|
|
29
30
|
* @example
|
|
@@ -48,6 +49,7 @@ const NotificationProvider = ({
|
|
|
48
49
|
environment,
|
|
49
50
|
additionalInfo,
|
|
50
51
|
channelConfigs = [],
|
|
52
|
+
backgroundNotificationSound,
|
|
51
53
|
}) => {
|
|
52
54
|
if (!children) return null;
|
|
53
55
|
|
|
@@ -58,6 +60,11 @@ const NotificationProvider = ({
|
|
|
58
60
|
const validChannelConfigs =
|
|
59
61
|
!!channelConfigs && isArray(channelConfigs) ? channelConfigs : [];
|
|
60
62
|
|
|
63
|
+
const safeBackgroundSound =
|
|
64
|
+
isString(backgroundNotificationSound) && backgroundNotificationSound?.trim()
|
|
65
|
+
? backgroundNotificationSound.trim()
|
|
66
|
+
: 'default';
|
|
67
|
+
|
|
61
68
|
const isRegistered = useRef(false);
|
|
62
69
|
const {
|
|
63
70
|
registerDeviceToNotifications,
|
|
@@ -113,7 +120,7 @@ const NotificationProvider = ({
|
|
|
113
120
|
|
|
114
121
|
await makeNotificationChannels(parsedChannelConfigs);
|
|
115
122
|
}
|
|
116
|
-
await makeDefaultChannel();
|
|
123
|
+
await makeDefaultChannel({sound: safeBackgroundSound});
|
|
117
124
|
};
|
|
118
125
|
|
|
119
126
|
useEffect(() => {
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import {AndroidImportance, AndroidVisibility} from '@notifee/react-native';
|
|
2
|
+
|
|
3
|
+
const DEFAULT_CHANNEL_CONFIGS = {
|
|
4
|
+
badge: true,
|
|
5
|
+
importance: AndroidImportance.HIGH,
|
|
6
|
+
lights: true,
|
|
7
|
+
sound: 'default',
|
|
8
|
+
vibration: true,
|
|
9
|
+
vibrationPattern: [500, 1000, 500, 1000],
|
|
10
|
+
visibility: AndroidVisibility.PUBLIC,
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export default DEFAULT_CHANNEL_CONFIGS;
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import NotificationProvider from './NotificationProvider';
|
|
2
2
|
import {usePushNotification} from './NotificationContext';
|
|
3
3
|
import {setupBackgroundMessageHandler} from './utils';
|
|
4
|
+
import cancelNotificationsSubscription from './utils/api/cancelNotificationsSubscription';
|
|
4
5
|
|
|
5
|
-
export {
|
|
6
|
+
export {
|
|
7
|
+
usePushNotification,
|
|
8
|
+
setupBackgroundMessageHandler,
|
|
9
|
+
cancelNotificationsSubscription,
|
|
10
|
+
};
|
|
6
11
|
export default NotificationProvider;
|
|
@@ -100,6 +100,7 @@ const usePushNotification = (
|
|
|
100
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.
|
|
101
101
|
* @param {Array<string>} events is the list of events to which I want to unsubscribe the device
|
|
102
102
|
* @returns {Promise}
|
|
103
|
+
* @deprecated Use cancelNotificationsSubscription from the main export instead
|
|
103
104
|
*
|
|
104
105
|
* @example
|
|
105
106
|
*
|
|
@@ -109,6 +110,10 @@ const usePushNotification = (
|
|
|
109
110
|
*/
|
|
110
111
|
|
|
111
112
|
const cancelNotifications = async (cancelEvents) => {
|
|
113
|
+
console.warn(
|
|
114
|
+
'⚠️ cancelNotifications is deprecated. Use cancelNotificationsSubscription instead.',
|
|
115
|
+
);
|
|
116
|
+
|
|
112
117
|
const eventsAreValid = cancelEvents && isArray(cancelEvents);
|
|
113
118
|
const eventsToCancel = eventsAreValid ? cancelEvents : pushEvents;
|
|
114
119
|
try {
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import RequestInstance from '@janiscommerce/app-request';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Cancels push notification subscriptions for specified events
|
|
5
|
+
* @param {Object} params - The parameters object
|
|
6
|
+
* @param {string[]} [params.events=[]] - Array of event names to unsubscribe from
|
|
7
|
+
* @param {string} params.env - Environment identifier (required)
|
|
8
|
+
* @returns {Promise<Object>} Promise that resolves with the API response
|
|
9
|
+
* @throws {Error} When environment is not provided or API request fails
|
|
10
|
+
* @example
|
|
11
|
+
* // Cancel subscription for specific events
|
|
12
|
+
* await cancelNotificationsSubscription({
|
|
13
|
+
* events: ['user.login', 'order.created'],
|
|
14
|
+
* env: 'janisdev'
|
|
15
|
+
* });
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const cancelNotificationsSubscription = async (params = {}) => {
|
|
19
|
+
try {
|
|
20
|
+
const {events = [], env = ''} = params;
|
|
21
|
+
|
|
22
|
+
if (!env) throw new Error('invalid environment');
|
|
23
|
+
|
|
24
|
+
const Request = new RequestInstance({JANIS_ENV: String(env)});
|
|
25
|
+
|
|
26
|
+
return await Request.post({
|
|
27
|
+
service: 'notification',
|
|
28
|
+
namespace: 'unsubscribe',
|
|
29
|
+
pathParams: ['push'],
|
|
30
|
+
body: {
|
|
31
|
+
events,
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
} catch (error) {
|
|
35
|
+
return Promise.reject(error);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export default cancelNotificationsSubscription;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import notifee, {AndroidImportance} from '@notifee/react-native';
|
|
2
|
+
import DEFAULT_CHANNEL_CONFIGS from '../../constants/defaultChannelConfigs';
|
|
2
3
|
import {isObject, isString} from '..';
|
|
3
4
|
|
|
4
5
|
export const parseChannelConfiguration = (params = {}) => {
|
|
5
6
|
if (!params || !isObject(params)) return null;
|
|
6
7
|
|
|
7
|
-
const {name, id = '', description = ''} = params;
|
|
8
|
+
const {name, id = '', description = '', ...restConfigs} = params;
|
|
8
9
|
|
|
9
10
|
if (!name || !isString(name)) return null;
|
|
10
11
|
|
|
@@ -12,6 +13,8 @@ export const parseChannelConfiguration = (params = {}) => {
|
|
|
12
13
|
const hasValidId = !!id && isString(id);
|
|
13
14
|
|
|
14
15
|
return {
|
|
16
|
+
...DEFAULT_CHANNEL_CONFIGS,
|
|
17
|
+
...restConfigs,
|
|
15
18
|
name,
|
|
16
19
|
id: hasValidId ? id : name,
|
|
17
20
|
importance: AndroidImportance.HIGH,
|
|
@@ -56,12 +59,13 @@ export const makeNotificationChannels = async (channelConfigs) => {
|
|
|
56
59
|
};
|
|
57
60
|
|
|
58
61
|
/* eslint-disable consistent-return */
|
|
59
|
-
export const makeDefaultChannel = async () => {
|
|
62
|
+
export const makeDefaultChannel = async (configs = {}) => {
|
|
60
63
|
try {
|
|
61
64
|
const parsedChannel = parseChannelConfiguration({
|
|
62
|
-
id: '
|
|
63
|
-
name: '
|
|
64
|
-
description: 'Default channel to receive notifications',
|
|
65
|
+
id: 'channel_default',
|
|
66
|
+
name: 'Operational notifications',
|
|
67
|
+
description: 'Default channel to receive operational notifications',
|
|
68
|
+
...configs,
|
|
65
69
|
});
|
|
66
70
|
|
|
67
71
|
await makeNotificationChannel(parsedChannel);
|
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.6",
|
|
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",
|