@inngageregistry/inngage-react 3.1.1 → 3.1.2

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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/Inngage.ts +34 -32
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inngageregistry/inngage-react",
3
- "version": "3.1.1",
3
+ "version": "3.1.2",
4
4
  "description": "Inngage Plugin for React Native applications for marketing campaign optimization using Push Notification.",
5
5
  "main": "src/index.ts",
6
6
  "types": "src/index.d.ts",
@@ -26,7 +26,7 @@
26
26
  "react-native-device-info": "^10.6.0",
27
27
  "react-native-inappbrowser-reborn": "^3.7.0",
28
28
  "react-native-localize": "^2.2.6",
29
- "react-native-permissions": "^4.0.0",
29
+ "react-native-permissions": "^3.8.0",
30
30
  "react-native-push-notification": "^8.1.1",
31
31
  "react-native-snap-carousel": "^3.9.1"
32
32
  },
package/src/Inngage.ts CHANGED
@@ -7,56 +7,58 @@ import { formatDate, subscriptionRequestAdapter } from "./utils";
7
7
  import notificationsListener, { notificationsListenerProps } from "./notificationsListener";
8
8
  import { subscriptionApi, eventsApi } from "./services/inngage";
9
9
 
10
- import { PERMISSIONS, RESULTS, request } from 'react-native-permissions';
10
+ import RNPermissions, { NotificationOption, RESULTS } from 'react-native-permissions';
11
11
 
12
12
  // --- Get Firebase Access ------/
13
13
  const getFirebaseAccess = async (): Promise<string | null> => {
14
14
  try {
15
- await handleNotificationsPermission()
16
- return null;
15
+ return await handleNotificationsPermission();
17
16
  } catch (error) {
18
17
  console.log('Erro no getFirebaseAccess: ', error);
19
18
  throw error;
20
19
  }
21
20
  };
22
21
 
23
- async function handleNotificationsPermission() {
24
- const apiLevel = await DeviceInfo.getApiLevel();
25
- if (apiLevel >= 33) {
26
- const result = await request(PERMISSIONS.ANDROID.POST_NOTIFICATIONS);
27
- if (result === RESULTS.GRANTED) {
28
- await getFirebaseToken();
29
- }
30
- } else {
31
- const authStatus = await firebase.messaging().requestPermission();
32
- const enabled =
33
- authStatus === firebase.messaging.AuthorizationStatus.AUTHORIZED ||
34
- authStatus === firebase.messaging.AuthorizationStatus.PROVISIONAL;
35
-
36
- if (enabled) {
37
- await getFirebaseToken();
38
- }
22
+ const handleNotificationsPermission = async () => {
23
+ try {
24
+ const options: NotificationOption[] = ['alert', 'badge', 'sound'];
25
+ const apiLevel = await DeviceInfo.getApiLevel();
26
+ const isPermissionGranted =
27
+ apiLevel >= 33
28
+ ? ((await RNPermissions.requestNotifications(options)).status) === RESULTS.GRANTED
29
+ : (await firebase.messaging().requestPermission()) ===
30
+ firebase.messaging.AuthorizationStatus.AUTHORIZED ||
31
+ firebase.messaging.AuthorizationStatus.PROVISIONAL;
32
+
33
+ return isPermissionGranted ? await getFirebaseToken() : null;
34
+ } catch (error) {
35
+ console.error(error);
36
+ throw error;
39
37
  }
40
- }
38
+ };
41
39
 
42
40
  const getFirebaseToken = async (): Promise<string | null> => {
43
- let fcmToken = await AsyncStorage.getItem('fcmToken');
41
+ try {
42
+ let fcmToken = await AsyncStorage.getItem('fcmToken');
44
43
 
45
- if (!fcmToken) {
46
- if (!firebase.messaging().isDeviceRegisteredForRemoteMessages) {
47
- await firebase.messaging().registerDeviceForRemoteMessages();
48
- }
44
+ if (!fcmToken) {
45
+ if (!firebase.messaging().isDeviceRegisteredForRemoteMessages) {
46
+ await firebase.messaging().registerDeviceForRemoteMessages?.();
47
+ }
49
48
 
50
- const newFcmToken = await firebase.messaging().getToken();
49
+ const newFcmToken = await firebase.messaging().getToken?.();
51
50
 
52
- if (newFcmToken) {
53
- await AsyncStorage.setItem('fcmToken', newFcmToken);
54
- return newFcmToken;
51
+ if (newFcmToken) {
52
+ await AsyncStorage.setItem('fcmToken', newFcmToken);
53
+ return newFcmToken;
54
+ }
55
55
  }
56
+ return fcmToken || null;
57
+ } catch (error) {
58
+ console.error(error);
59
+ throw error;
56
60
  }
57
-
58
- return fcmToken;
59
- }
61
+ };
60
62
 
61
63
  interface SubscriptionProps {
62
64
  appToken: string,