@janiscommerce/app-push-notification 0.2.0 → 1.1.0-beta.1
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.
|
@@ -18,7 +18,7 @@ import {
|
|
|
18
18
|
makeNotificationChannels,
|
|
19
19
|
parseNotificationChannel,
|
|
20
20
|
} from '../utils/channel';
|
|
21
|
-
import
|
|
21
|
+
import Crashlytics from '../utils/crashlytics';
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
24
|
* @function NotificationProvider
|
|
@@ -120,14 +120,14 @@ const NotificationProvider = ({
|
|
|
120
120
|
|
|
121
121
|
const SubscribeToNotifications = async () => {
|
|
122
122
|
try {
|
|
123
|
-
|
|
124
|
-
const fcmToken = await getFCMToken();
|
|
125
|
-
const isSameToken = storedToken === fcmToken;
|
|
123
|
+
Crashlytics.log('[SubscribeToNotifications] Starting subscription');
|
|
126
124
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
await registerDeviceToNotifications({token: fcmToken, additionalInfo});
|
|
125
|
+
await registerDeviceToNotifications({additionalInfo});
|
|
130
126
|
} catch (error) {
|
|
127
|
+
Crashlytics.recordError(
|
|
128
|
+
error,
|
|
129
|
+
'[SubscribeToNotifications] Error registering device to notifications',
|
|
130
|
+
);
|
|
131
131
|
if (onSubscriptionError && isFunction(onSubscriptionError))
|
|
132
132
|
onSubscriptionError(error);
|
|
133
133
|
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import {useState} from 'react';
|
|
2
2
|
import RequestInstance from '@janiscommerce/app-request';
|
|
3
|
-
import {isArray,
|
|
3
|
+
import {isArray, isObject, isString} from './utils';
|
|
4
4
|
import SubscribeNotifications from './utils/api/SubscribeNotifications';
|
|
5
5
|
import {getStoredToken, getFCMToken, updateStoredToken} from './utils/token';
|
|
6
6
|
import cancelNotificationsSubscription from './utils/api/cancelNotificationsSubscription';
|
|
7
|
+
import Crashlytics from './utils/crashlytics';
|
|
7
8
|
|
|
8
9
|
const usePushNotification = (environment, events, appName) => {
|
|
9
10
|
const [notificationState, setNotificationState] = useState({
|
|
@@ -28,36 +29,25 @@ const usePushNotification = (environment, events, appName) => {
|
|
|
28
29
|
|
|
29
30
|
const getSubscribedEvents = () => pushEvents;
|
|
30
31
|
|
|
31
|
-
// istanbul ignore next line
|
|
32
|
-
const getDeviceToken = async () => {
|
|
33
|
-
try {
|
|
34
|
-
const [storedToken] = await promiseWrapper(getStoredToken());
|
|
35
|
-
|
|
36
|
-
if (storedToken) return storedToken;
|
|
37
|
-
|
|
38
|
-
const fcmToken = await getFCMToken();
|
|
39
|
-
|
|
40
|
-
return fcmToken;
|
|
41
|
-
} catch (error) {
|
|
42
|
-
return null;
|
|
43
|
-
}
|
|
44
|
-
};
|
|
45
|
-
|
|
46
32
|
/**
|
|
47
33
|
* @function registerDeviceToNotifications
|
|
48
34
|
* @description This function is responsible for registering the device to the notification microservice.
|
|
49
|
-
* @returns {null}
|
|
50
35
|
*/
|
|
51
36
|
|
|
52
|
-
const registerDeviceToNotifications = async (params) => {
|
|
53
|
-
const {
|
|
37
|
+
const registerDeviceToNotifications = async (params = {}) => {
|
|
38
|
+
const {additionalInfo = null} = params;
|
|
54
39
|
try {
|
|
40
|
+
const storedToken = await getStoredToken();
|
|
41
|
+
const fcmToken = await getFCMToken();
|
|
42
|
+
const isSameToken = storedToken === fcmToken;
|
|
43
|
+
|
|
44
|
+
if (Boolean(storedToken) && isSameToken) return null;
|
|
45
|
+
|
|
55
46
|
await SubscribeNotifications(
|
|
56
|
-
{token, events: pushEvents, appName, additionalInfo},
|
|
47
|
+
{token: fcmToken, events: pushEvents, appName, additionalInfo},
|
|
57
48
|
Request,
|
|
58
49
|
);
|
|
59
|
-
|
|
60
|
-
await updateStoredToken(token);
|
|
50
|
+
await updateStoredToken(fcmToken);
|
|
61
51
|
return updateNotificationState({pushEvents});
|
|
62
52
|
} catch (error) {
|
|
63
53
|
updateNotificationState({pushEvents: [], subscribeError: error});
|
|
@@ -66,23 +56,36 @@ const usePushNotification = (environment, events, appName) => {
|
|
|
66
56
|
};
|
|
67
57
|
|
|
68
58
|
/**
|
|
69
|
-
* @function
|
|
59
|
+
* @function updateSubscription
|
|
70
60
|
* @description This function is responsible for updating the subscription to the notification service
|
|
71
61
|
* @param {object} additionalInfo all properties that will be sent as additional data in the subscription
|
|
72
62
|
* @returns {Promise}
|
|
73
63
|
*/
|
|
74
64
|
|
|
75
|
-
const
|
|
65
|
+
const updateSubscription = async (additionalInfo) => {
|
|
76
66
|
try {
|
|
77
|
-
|
|
67
|
+
Crashlytics.log('[updateSubscription] Starting update subscription');
|
|
68
|
+
|
|
69
|
+
if (!isObject(additionalInfo)) return null;
|
|
70
|
+
let token = await getStoredToken();
|
|
71
|
+
|
|
72
|
+
if (!token) {
|
|
73
|
+
token = await getFCMToken();
|
|
74
|
+
}
|
|
78
75
|
|
|
79
76
|
await SubscribeNotifications(
|
|
80
77
|
{token, events: pushEvents, appName, additionalInfo},
|
|
81
78
|
Request,
|
|
82
79
|
);
|
|
83
80
|
|
|
81
|
+
await updateStoredToken(token);
|
|
82
|
+
|
|
84
83
|
return Promise.resolve();
|
|
85
84
|
} catch (error) {
|
|
85
|
+
Crashlytics.recordError(
|
|
86
|
+
error,
|
|
87
|
+
'[updateSubscription] Error updating subscription',
|
|
88
|
+
);
|
|
86
89
|
return Promise.reject(error);
|
|
87
90
|
}
|
|
88
91
|
};
|
|
@@ -181,7 +184,7 @@ const usePushNotification = (environment, events, appName) => {
|
|
|
181
184
|
updateNotificationState,
|
|
182
185
|
getSubscribedEvents,
|
|
183
186
|
deleteReceivedNotification,
|
|
184
|
-
|
|
187
|
+
updateSubscription,
|
|
185
188
|
};
|
|
186
189
|
};
|
|
187
190
|
|
package/lib/utils/token/index.js
CHANGED
|
@@ -8,27 +8,20 @@ import AsyncStorage from '@react-native-async-storage/async-storage';
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
export const getFCMToken = async () => {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
} catch (error) {
|
|
15
|
-
return null;
|
|
16
|
-
}
|
|
11
|
+
const fcmToken = await messaging().getToken();
|
|
12
|
+
if (!fcmToken) throw new Error('FCM token is null');
|
|
13
|
+
return fcmToken;
|
|
17
14
|
};
|
|
18
15
|
|
|
19
16
|
/**
|
|
20
17
|
* @function getStoredToken
|
|
21
18
|
* @description This function is responsible for getting the stored token from AsyncStorage
|
|
22
|
-
* @returns {Promise<string>} The stored token
|
|
19
|
+
* @returns {Promise<string|null>} The stored token or null if not found
|
|
23
20
|
*/
|
|
24
21
|
|
|
25
22
|
export const getStoredToken = async () => {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
return storedToken;
|
|
29
|
-
} catch (error) {
|
|
30
|
-
return null;
|
|
31
|
-
}
|
|
23
|
+
const storedToken = await AsyncStorage.getItem('currentToken');
|
|
24
|
+
return storedToken;
|
|
32
25
|
};
|
|
33
26
|
|
|
34
27
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@janiscommerce/app-push-notification",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.1.0-beta.1",
|
|
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",
|
|
@@ -30,14 +30,15 @@
|
|
|
30
30
|
"@notifee/react-native": "^7.8.2"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
|
-
"@janiscommerce/app-
|
|
33
|
+
"@janiscommerce/app-crashlytics": ">=2.0.0",
|
|
34
34
|
"@janiscommerce/app-request": "^2.2.0",
|
|
35
|
+
"@janiscommerce/app-storage": ">=1.0.0",
|
|
35
36
|
"@react-native-async-storage/async-storage": "^1.18.1",
|
|
36
|
-
"@react-native-firebase/app": "^
|
|
37
|
-
"@react-native-firebase/messaging": "^
|
|
37
|
+
"@react-native-firebase/app": "^21.6.1",
|
|
38
|
+
"@react-native-firebase/messaging": "^21.6.1",
|
|
38
39
|
"axios": "^1.3.6",
|
|
39
|
-
"react": ">=17.0.2 <
|
|
40
|
-
"react-native": ">=0.
|
|
40
|
+
"react": ">=17.0.2 <20.0.0",
|
|
41
|
+
"react-native": ">=0.71.5 <0.82.0"
|
|
41
42
|
},
|
|
42
43
|
"devDependencies": {
|
|
43
44
|
"@babel/core": "^7.22.10",
|
|
@@ -46,6 +47,7 @@
|
|
|
46
47
|
"@babel/plugin-proposal-object-rest-spread": "^7.20.7",
|
|
47
48
|
"@babel/preset-env": "^7.22.10",
|
|
48
49
|
"@babel/runtime": "^7.12.5",
|
|
50
|
+
"@janiscommerce/app-crashlytics": "^2.2.0",
|
|
49
51
|
"@janiscommerce/app-request": "^2.2.0",
|
|
50
52
|
"@janiscommerce/app-storage": "^1.0.0",
|
|
51
53
|
"@react-native-async-storage/async-storage": "^1.18.1",
|