@inngageregistry/inngage-react 3.0.5 → 3.0.7
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/README.md +40 -0
- package/package.json +6 -6
- package/src/Inngage.ts +39 -52
- package/src/components/Inapp.tsx +11 -5
- package/src/index.d.ts +42 -1
- package/src/notificationsListener.ts +64 -77
package/README.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
<h1 align="center">
|
|
2
|
+
<b>
|
|
3
|
+
<a href="https://inngage.com.br/"><img src="https://inngage.com.br/wp-content/uploads/2022/03/inngage-small.png" /></a><br>
|
|
4
|
+
</b>
|
|
5
|
+
</h1>
|
|
6
|
+
|
|
7
|
+
<p align="center">Pacote Inngage para aplicativos React Native para otimização de campanhas de marketing usando notificações push.</p>
|
|
8
|
+
|
|
9
|
+
<p align="center">
|
|
10
|
+
<a href="https://inngage.com.br/"><b>Website</b></a> •
|
|
11
|
+
<a href="https://inngage.readme.io/docs/react-native-sdk-20"><b>Documentation</b></a>
|
|
12
|
+
</p>
|
|
13
|
+
|
|
14
|
+
<div align="center">
|
|
15
|
+
|
|
16
|
+
[](https://www.npmjs.org/package/@inngageregistry/inngage-react)
|
|
17
|
+
|
|
18
|
+
</div>
|
|
19
|
+
|
|
20
|
+
## Instalação
|
|
21
|
+
### Instalando as dependências
|
|
22
|
+
```bash
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@react-native-async-storage/async-storage": "^1.18.1",
|
|
25
|
+
"@react-native-firebase/app": "^17.4.2",
|
|
26
|
+
"@react-native-firebase/messaging": "^17.4.2",
|
|
27
|
+
"react-native-device-info": "^10.6.0",
|
|
28
|
+
"react-native-inappbrowser-reborn": "^3.7.0",
|
|
29
|
+
"react-native-localize": "^2.2.6",
|
|
30
|
+
"react-native-snap-carousel": "^3.9.1"
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
Ao adicioná-las no seu ```package.json```, utilize o comando ```npm install```.
|
|
34
|
+
### Importar FCM Token (API Key)
|
|
35
|
+
Para aproveitar nossos SDKs, será necessário importar o FCM Token (API Key) do seu projeto Firebase em nossa plataforma.
|
|
36
|
+
### Instalando a SDK
|
|
37
|
+
Utilize o NPM:
|
|
38
|
+
```bash
|
|
39
|
+
$ npm i @inngageregistry/inngage-react
|
|
40
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inngageregistry/inngage-react",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.7",
|
|
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",
|
|
@@ -19,15 +19,15 @@
|
|
|
19
19
|
},
|
|
20
20
|
"homepage": "https://github.com/inngage/inngage-react#readme",
|
|
21
21
|
"dependencies": {
|
|
22
|
+
"@react-native-async-storage/async-storage": "^1.18.1",
|
|
23
|
+
"@react-native-community/push-notification-ios": "^1.11.0",
|
|
22
24
|
"@react-native-firebase/app": "^17.4.2",
|
|
23
25
|
"@react-native-firebase/messaging": "^17.4.2",
|
|
24
|
-
"react-native-
|
|
25
|
-
"react-native-snap-carousel": "^3.9.1",
|
|
26
|
-
"@react-native-async-storage/async-storage": "^1.18.1",
|
|
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
29
|
"react-native-push-notification": "^8.1.1",
|
|
30
|
-
"react-native-
|
|
30
|
+
"react-native-snap-carousel": "^3.9.1"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@types/jest": "^29.5.1",
|
package/src/Inngage.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Platform,
|
|
3
|
+
PermissionsAndroid,
|
|
3
4
|
LogBox,
|
|
4
5
|
AppRegistry,
|
|
5
6
|
} from "react-native";
|
|
@@ -13,56 +14,50 @@ import { formatDate, subscriptionRequestAdapter } from "./utils";
|
|
|
13
14
|
import notificationsListener, { notificationsListenerProps } from "./notificationsListener";
|
|
14
15
|
import { subscriptionApi, eventsApi } from "./services/inngage";
|
|
15
16
|
|
|
16
|
-
// ---
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
// --- Get Firebase Access ------/
|
|
18
|
+
const getFirebaseAccess = async (): Promise<string | null> => {
|
|
19
|
+
try {
|
|
20
|
+
const apiLevel = await DeviceInfo.getApiLevel();
|
|
21
|
+
|
|
22
|
+
if (apiLevel >= 33) {
|
|
23
|
+
await PermissionsAndroid.request(
|
|
24
|
+
PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS
|
|
25
|
+
);
|
|
26
|
+
}
|
|
20
27
|
|
|
21
|
-
|
|
28
|
+
const authStatus = await firebase.messaging().requestPermission();
|
|
22
29
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
messageArray.push(remoteMessage);
|
|
30
|
+
const enabled =
|
|
31
|
+
authStatus === firebase.messaging.AuthorizationStatus.AUTHORIZED ||
|
|
32
|
+
authStatus === firebase.messaging.AuthorizationStatus.PROVISIONAL;
|
|
28
33
|
|
|
29
|
-
|
|
30
|
-
|
|
34
|
+
if (enabled) {
|
|
35
|
+
let fcmToken = await AsyncStorage.getItem('fcmToken');
|
|
36
|
+
console.log('Old token: ', fcmToken);
|
|
31
37
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
await firebase.messaging().registerDeviceForRemoteMessages();
|
|
44
|
-
|
|
45
|
-
const permission = await firebase.messaging().hasPermission();
|
|
46
|
-
if (permission === firebase.messaging.AuthorizationStatus.NOT_DETERMINED) {
|
|
47
|
-
try {
|
|
48
|
-
await firebase.messaging().requestPermission();
|
|
49
|
-
} catch (e) {
|
|
50
|
-
console.error(e)
|
|
51
|
-
return resolve(firebaseToken);
|
|
38
|
+
if (!fcmToken) {
|
|
39
|
+
if (!firebase.messaging().isDeviceRegisteredForRemoteMessages) {
|
|
40
|
+
await firebase.messaging().registerDeviceForRemoteMessages();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const newFcmToken = await firebase.messaging().getToken();
|
|
44
|
+
|
|
45
|
+
if (newFcmToken) {
|
|
46
|
+
await AsyncStorage.setItem('fcmToken', newFcmToken);
|
|
47
|
+
console.log('New token: ', newFcmToken);
|
|
48
|
+
return newFcmToken;
|
|
52
49
|
}
|
|
53
50
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
console.error(error)
|
|
58
|
-
return resolve(firebaseToken)
|
|
59
|
-
}
|
|
60
|
-
return resolve(firebaseToken)
|
|
61
|
-
} catch (err) {
|
|
62
|
-
console.error(err)
|
|
63
|
-
return resolve(firebaseToken)
|
|
51
|
+
|
|
52
|
+
console.log('Authorization status:', authStatus);
|
|
53
|
+
return fcmToken;
|
|
64
54
|
}
|
|
65
|
-
|
|
55
|
+
|
|
56
|
+
return null;
|
|
57
|
+
} catch (error) {
|
|
58
|
+
console.log('Erro no getFirebaseAccess: ', error);
|
|
59
|
+
throw error;
|
|
60
|
+
}
|
|
66
61
|
};
|
|
67
62
|
|
|
68
63
|
interface SubscriptionProps {
|
|
@@ -95,16 +90,8 @@ interface SendEventProps {
|
|
|
95
90
|
const Inngage = {
|
|
96
91
|
// ------------ Register Notification Listener ------------//
|
|
97
92
|
RegisterNotificationListener: async (props: notificationsListenerProps) => {
|
|
98
|
-
|
|
99
|
-
try {
|
|
100
|
-
LogBox.ignoreLogs(['registerHeadlessTask'])
|
|
101
|
-
} catch (e) { }
|
|
102
|
-
try {
|
|
103
|
-
console.ignoredYellowBox = ['registerHeadlessTask'];
|
|
104
|
-
} catch (e) { }
|
|
105
93
|
try {
|
|
106
|
-
|
|
107
|
-
notificationsListener({ ...props });
|
|
94
|
+
await notificationsListener({ ...props });
|
|
108
95
|
} catch (e) {
|
|
109
96
|
console.error(e);
|
|
110
97
|
return { subscribed: false };
|
package/src/components/Inapp.tsx
CHANGED
|
@@ -64,7 +64,7 @@ export const Inapp = (props: InappProps) => {
|
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
const itemStyles = styleItem({msg, checkBG})
|
|
67
|
+
const itemStyles = styleItem({ msg, checkBG })
|
|
68
68
|
|
|
69
69
|
const chooseRef = () => {
|
|
70
70
|
if (index == 0) {
|
|
@@ -225,9 +225,9 @@ export const Inapp = (props: InappProps) => {
|
|
|
225
225
|
const onLoad = async () => {
|
|
226
226
|
let temp: any = []
|
|
227
227
|
const messages = JSON.parse(await AsyncStorage.getItem('inngage') ?? '[]')
|
|
228
|
-
|
|
228
|
+
|
|
229
229
|
console.log("Messages saved on AsyncStorage: ", JSON.stringify(messages))
|
|
230
|
-
|
|
230
|
+
|
|
231
231
|
if (messages !== null) {
|
|
232
232
|
messages.forEach((el) => {
|
|
233
233
|
if (!isEmpty(el)) {
|
|
@@ -294,7 +294,13 @@ export const Inapp = (props: InappProps) => {
|
|
|
294
294
|
layoutCardOffset={10}
|
|
295
295
|
data={data}
|
|
296
296
|
inactiveSlideOpacity={0}
|
|
297
|
-
containerCustomStyle={
|
|
297
|
+
containerCustomStyle={{
|
|
298
|
+
backgroundColor: 'white',
|
|
299
|
+
elevation: 10,
|
|
300
|
+
borderRadius: 10,
|
|
301
|
+
width: SLIDER_WIDTH * 0.8,
|
|
302
|
+
height: 480,
|
|
303
|
+
}}
|
|
298
304
|
contentContainerCustomStyle={{ justifyContent: 'center' }}
|
|
299
305
|
inactiveSlideShift={0}
|
|
300
306
|
onSnapToItem={(index) => {
|
|
@@ -315,4 +321,4 @@ export const Inapp = (props: InappProps) => {
|
|
|
315
321
|
}
|
|
316
322
|
};
|
|
317
323
|
|
|
318
|
-
const styles = styleInapp({SLIDER_WIDTH})
|
|
324
|
+
const styles = styleInapp({ SLIDER_WIDTH })
|
package/src/index.d.ts
CHANGED
|
@@ -1 +1,42 @@
|
|
|
1
|
-
|
|
1
|
+
interface notificationsListenerProps {
|
|
2
|
+
appToken: string,
|
|
3
|
+
dev?: boolean,
|
|
4
|
+
enableAlert: boolean,
|
|
5
|
+
onNotificationOpenedApp?: any,
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
interface SubscriptionProps {
|
|
9
|
+
appToken: string;
|
|
10
|
+
enableAlert: boolean;
|
|
11
|
+
dev?: boolean;
|
|
12
|
+
friendlyIdentifier?: string;
|
|
13
|
+
customFields?: any;
|
|
14
|
+
customData?: any;
|
|
15
|
+
phoneNumber?: string;
|
|
16
|
+
email?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface SendEventProps {
|
|
20
|
+
newEventRequest: {
|
|
21
|
+
app_token: string;
|
|
22
|
+
identifier: string;
|
|
23
|
+
event_name: string;
|
|
24
|
+
conversion_event: boolean;
|
|
25
|
+
conversion_value: number;
|
|
26
|
+
conversion_notid: string;
|
|
27
|
+
event_values: {
|
|
28
|
+
nome_promo: string;
|
|
29
|
+
categoria: string;
|
|
30
|
+
foto_promo: string;
|
|
31
|
+
redirect_link: string;
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
declare const Inngage: {
|
|
37
|
+
RegisterNotificationListener: (props: notificationsListenerProps) => Promise<any>;
|
|
38
|
+
Subscribe: (props: SubscriptionProps) => Promise<any>;
|
|
39
|
+
SendEvent: (props: SendEventProps) => Promise<any>;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export default Inngage;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Linking } from 'react-native'
|
|
2
2
|
import InAppBrowser from 'react-native-inappbrowser-reborn'
|
|
3
|
-
import messaging
|
|
3
|
+
import messaging from '@react-native-firebase/messaging';
|
|
4
4
|
import { showAlert } from './utils'
|
|
5
5
|
import { notificationApi } from './services/inngage'
|
|
6
6
|
import AsyncStorage from '@react-native-async-storage/async-storage';
|
|
@@ -27,40 +27,46 @@ export const linkInApp = (link: string) => {
|
|
|
27
27
|
})
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
const openLinkByType = (type: string, url: string) =>
|
|
30
|
+
const openLinkByType = (type: string, url: string) => {
|
|
31
|
+
const linkTypeHandlers: Record<string, () => void> = {
|
|
32
|
+
deep: () => Linking.openURL(url),
|
|
33
|
+
inapp: () => linkInApp(url),
|
|
34
|
+
};
|
|
31
35
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
return
|
|
36
|
+
const handler = linkTypeHandlers[type];
|
|
37
|
+
if (handler) {
|
|
38
|
+
handler();
|
|
36
39
|
}
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const openCommonNotification = ({ appToken, dev, remoteMessage, enableAlert, state }) => {
|
|
43
|
+
console.log("Notification Opened from", state)
|
|
44
|
+
|
|
45
|
+
if (!remoteMessage)
|
|
46
|
+
return
|
|
47
|
+
|
|
37
48
|
const { data } = remoteMessage
|
|
38
|
-
|
|
49
|
+
console.log("Data:", data)
|
|
50
|
+
if (!data || (data && !Object.keys(data).length))
|
|
39
51
|
return
|
|
40
|
-
|
|
52
|
+
|
|
41
53
|
const { notId, title, body, type, url } = data
|
|
42
54
|
const request = {
|
|
43
55
|
notificationRequest: {
|
|
44
56
|
id: notId,
|
|
45
57
|
app_token: appToken,
|
|
46
|
-
}
|
|
58
|
+
}
|
|
47
59
|
}
|
|
48
|
-
if (!url) {
|
|
49
|
-
|
|
50
|
-
return notificationApi(request, dev).then(() => {
|
|
51
60
|
|
|
61
|
+
if (url) {
|
|
62
|
+
Linking.canOpenURL(url).then((supported) => {
|
|
63
|
+
if (supported) {
|
|
64
|
+
supported && openLinkByType(type, url)
|
|
65
|
+
}
|
|
52
66
|
}).catch(console.error)
|
|
53
|
-
|
|
54
67
|
}
|
|
55
|
-
return Linking.canOpenURL(url).then((supported) => {
|
|
56
|
-
if (supported) {
|
|
57
|
-
|
|
58
|
-
supported && openLinkByType(type, url)
|
|
59
68
|
|
|
60
|
-
|
|
61
|
-
}).catch(console.error)
|
|
62
|
-
}
|
|
63
|
-
export const openRichNotification = (notificationData) => {
|
|
69
|
+
return notificationApi(request, dev)
|
|
64
70
|
}
|
|
65
71
|
|
|
66
72
|
export interface notificationsListenerProps {
|
|
@@ -72,71 +78,31 @@ export interface notificationsListenerProps {
|
|
|
72
78
|
export default async ({ appToken, dev, enableAlert, onNotificationOpenedApp }: notificationsListenerProps) => {
|
|
73
79
|
var messageArray: any = [];
|
|
74
80
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
id: remoteMessage.data!.notId,
|
|
81
|
-
app_token: appToken,
|
|
82
|
-
},
|
|
83
|
-
}
|
|
84
|
-
if (remoteMessage != null && remoteMessage.data!.additional_data) {
|
|
85
|
-
let msg = JSON.parse(remoteMessage.data!.additional_data)
|
|
86
|
-
console.log('first step')
|
|
87
|
-
if (msg.inapp_message == true) {
|
|
88
|
-
console.log('second step')
|
|
89
|
-
const currentMessages = await AsyncStorage.getItem('inngage');
|
|
90
|
-
if (currentMessages !== null) {
|
|
91
|
-
messageArray = JSON.parse(currentMessages);
|
|
92
|
-
}
|
|
93
|
-
messageArray.push(remoteMessage);
|
|
94
|
-
await AsyncStorage.setItem('inngage', JSON.stringify(messageArray));
|
|
95
|
-
setTimeout(() => {
|
|
96
|
-
messaging().getInitialNotification().then(notification => {
|
|
97
|
-
notificationApi(request, dev)
|
|
98
|
-
})
|
|
99
|
-
}, 3000)
|
|
100
|
-
}
|
|
101
|
-
} else if (remoteMessage != null && !remoteMessage.data!.additional_data) {
|
|
102
|
-
setTimeout(() => {
|
|
103
|
-
messaging().getInitialNotification().then(notification => {
|
|
104
|
-
if (!remoteMessage.data!.url) {
|
|
105
|
-
notificationApi(request, dev) // TODO, responsible for triggering the notification api multiple times
|
|
106
|
-
} else {
|
|
107
|
-
notificationApi(request, dev)
|
|
108
|
-
Linking.canOpenURL(remoteMessage.data!.url).then((supported) => {
|
|
109
|
-
if (supported) {
|
|
110
|
-
|
|
111
|
-
supported && openLinkByType(remoteMessage.data!.type, remoteMessage.data!.url)
|
|
112
|
-
|
|
113
|
-
}
|
|
114
|
-
}).catch(error => console.error(error))
|
|
115
|
-
}
|
|
116
|
-
})
|
|
117
|
-
}, 3000)
|
|
118
|
-
}
|
|
119
|
-
})
|
|
81
|
+
if (typeof onNotificationOpenedApp == 'function') {
|
|
82
|
+
messaging().getInitialNotification().then((value) => {
|
|
83
|
+
onNotificationOpenedApp(value?.data);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
120
86
|
|
|
121
|
-
|
|
122
|
-
console.log(
|
|
123
|
-
|
|
124
|
-
});
|
|
87
|
+
const handleBackgroundMessage = async (remoteMessage: any) => {
|
|
88
|
+
console.log('Message handled in the background!', remoteMessage);
|
|
89
|
+
}
|
|
125
90
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
onNotificationOpenedApp(remoteMessage);
|
|
91
|
+
const handleNotificationOpenedApp = async (remoteMessage: any) => {
|
|
92
|
+
await openCommonNotification({ appToken, dev, remoteMessage, enableAlert, state: 'Background' })
|
|
129
93
|
}
|
|
130
94
|
|
|
131
|
-
|
|
132
|
-
|
|
95
|
+
const handleInitialNotification = async (remoteMessage: any) => {
|
|
96
|
+
await openCommonNotification({ appToken, dev, remoteMessage, enableAlert, state: 'Closed' })
|
|
97
|
+
}
|
|
133
98
|
|
|
99
|
+
const handleForegroundMessage = async (remoteMessage: any) => {
|
|
134
100
|
try {
|
|
135
101
|
PushNotification.configure({
|
|
136
102
|
onNotification: function (notification) {
|
|
137
103
|
console.log('LOCAL NOTIFICATION ==>', notification)
|
|
138
104
|
|
|
139
|
-
openCommonNotification({ appToken, dev, remoteMessage, enableAlert, state: '
|
|
105
|
+
openCommonNotification({ appToken, dev, remoteMessage, enableAlert, state: 'Foreground' })
|
|
140
106
|
},
|
|
141
107
|
popInitialNotification: true,
|
|
142
108
|
requestPermissions: true
|
|
@@ -186,5 +152,26 @@ export default async ({ appToken, dev, enableAlert, onNotificationOpenedApp }: n
|
|
|
186
152
|
showAlert(remoteMessage.data!.title, remoteMessage.data!.body)
|
|
187
153
|
}
|
|
188
154
|
}
|
|
189
|
-
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
messaging().setBackgroundMessageHandler(handleBackgroundMessage)
|
|
158
|
+
messaging().onNotificationOpenedApp(handleNotificationOpenedApp)
|
|
159
|
+
messaging().getInitialNotification().then(async (remoteMessage) => {
|
|
160
|
+
console.log("Remote message ID:", remoteMessage?.messageId)
|
|
161
|
+
try {
|
|
162
|
+
const lastRemoteMessageId = await AsyncStorage.getItem('LAST_REMOTE_MESSAGE_ID');
|
|
163
|
+
const newRemoteMessageId = remoteMessage?.messageId;
|
|
164
|
+
|
|
165
|
+
console.log("Last RM Id:", lastRemoteMessageId);
|
|
166
|
+
console.log("New RM Id:", newRemoteMessageId);
|
|
167
|
+
|
|
168
|
+
if (newRemoteMessageId && lastRemoteMessageId !== newRemoteMessageId) {
|
|
169
|
+
await AsyncStorage.setItem('LAST_REMOTE_MESSAGE_ID', newRemoteMessageId);
|
|
170
|
+
return handleInitialNotification(remoteMessage);
|
|
171
|
+
}
|
|
172
|
+
} catch (e) {
|
|
173
|
+
console.log(e);
|
|
174
|
+
}
|
|
175
|
+
})
|
|
176
|
+
messaging().onMessage(handleForegroundMessage)
|
|
190
177
|
}
|