@inngageregistry/inngage-react 3.0.6 → 3.1.0
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/package.json +1 -1
- package/src/Inngage.ts +1 -25
- package/src/components/Inapp.tsx +11 -5
- package/src/index.d.ts +42 -1
- package/src/notificationsListener.ts +75 -76
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inngageregistry/inngage-react",
|
|
3
|
-
"version": "3.0
|
|
3
|
+
"version": "3.1.0",
|
|
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",
|
package/src/Inngage.ts
CHANGED
|
@@ -14,22 +14,6 @@ import { formatDate, subscriptionRequestAdapter } from "./utils";
|
|
|
14
14
|
import notificationsListener, { notificationsListenerProps } from "./notificationsListener";
|
|
15
15
|
import { subscriptionApi, eventsApi } from "./services/inngage";
|
|
16
16
|
|
|
17
|
-
// --- handle background message ------/
|
|
18
|
-
const backgroundNotificationHandler = async remoteMessage => {
|
|
19
|
-
var messageArray: any = [];
|
|
20
|
-
console.log("Remote message:", JSON.stringify(remoteMessage));
|
|
21
|
-
|
|
22
|
-
console.log('Called backgroundNotificationHandler');
|
|
23
|
-
|
|
24
|
-
const currentMessages = await AsyncStorage.getItem('messages');
|
|
25
|
-
if (currentMessages !== null) {
|
|
26
|
-
messageArray = JSON.parse(currentMessages);
|
|
27
|
-
}
|
|
28
|
-
messageArray.push(remoteMessage);
|
|
29
|
-
|
|
30
|
-
await AsyncStorage.setItem('messages', JSON.stringify(messageArray));
|
|
31
|
-
};
|
|
32
|
-
|
|
33
17
|
// --- Get Firebase Access ------/
|
|
34
18
|
const getFirebaseAccess = async (): Promise<string | null> => {
|
|
35
19
|
try {
|
|
@@ -106,16 +90,8 @@ interface SendEventProps {
|
|
|
106
90
|
const Inngage = {
|
|
107
91
|
// ------------ Register Notification Listener ------------//
|
|
108
92
|
RegisterNotificationListener: async (props: notificationsListenerProps) => {
|
|
109
|
-
|
|
110
|
-
try {
|
|
111
|
-
LogBox.ignoreLogs(['registerHeadlessTask'])
|
|
112
|
-
} catch (e) { }
|
|
113
|
-
try {
|
|
114
|
-
console.ignoredYellowBox = ['registerHeadlessTask'];
|
|
115
|
-
} catch (e) { }
|
|
116
93
|
try {
|
|
117
|
-
|
|
118
|
-
notificationsListener({ ...props });
|
|
94
|
+
await notificationsListener({ ...props });
|
|
119
95
|
} catch (e) {
|
|
120
96
|
console.error(e);
|
|
121
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,41 +27,64 @@ 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
68
|
|
|
58
|
-
|
|
69
|
+
return notificationApi(request, dev)
|
|
70
|
+
}
|
|
59
71
|
|
|
72
|
+
const handleUniqueRemoteMessage = async (
|
|
73
|
+
remoteMessage: { messageId?: string },
|
|
74
|
+
handleInitialNotification: (value: { messageId?: string }) => void
|
|
75
|
+
) => {
|
|
76
|
+
try {
|
|
77
|
+
const lastRemoteMessageId = await AsyncStorage.getItem('LAST_REMOTE_MESSAGE_ID');
|
|
78
|
+
const newRemoteMessageId = remoteMessage?.messageId;
|
|
79
|
+
|
|
80
|
+
if (newRemoteMessageId && lastRemoteMessageId !== newRemoteMessageId) {
|
|
81
|
+
await AsyncStorage.setItem('LAST_REMOTE_MESSAGE_ID', newRemoteMessageId);
|
|
82
|
+
handleInitialNotification(remoteMessage);
|
|
60
83
|
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}
|
|
84
|
+
} catch (e) {
|
|
85
|
+
console.log(e);
|
|
86
|
+
}
|
|
87
|
+
};
|
|
65
88
|
|
|
66
89
|
export interface notificationsListenerProps {
|
|
67
90
|
appToken: string,
|
|
@@ -72,71 +95,36 @@ export interface notificationsListenerProps {
|
|
|
72
95
|
export default async ({ appToken, dev, enableAlert, onNotificationOpenedApp }: notificationsListenerProps) => {
|
|
73
96
|
var messageArray: any = [];
|
|
74
97
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}
|
|
84
|
-
|
|
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
|
-
})
|
|
98
|
+
if (typeof onNotificationOpenedApp == 'function') {
|
|
99
|
+
messaging().getInitialNotification().then(async (value) => {
|
|
100
|
+
onNotificationOpenedApp(value?.data);
|
|
101
|
+
// console.log("Remote message ID:", value?.messageId)
|
|
102
|
+
if (value !== null)
|
|
103
|
+
handleUniqueRemoteMessage(value, async (value) => {
|
|
104
|
+
await handleInitialNotification(value);
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
}
|
|
120
108
|
|
|
121
|
-
|
|
122
|
-
console.log(
|
|
123
|
-
|
|
124
|
-
});
|
|
109
|
+
const handleBackgroundMessage = async (remoteMessage: any) => {
|
|
110
|
+
console.log('Message handled in the background!', remoteMessage);
|
|
111
|
+
}
|
|
125
112
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
onNotificationOpenedApp(remoteMessage);
|
|
113
|
+
const handleNotificationOpenedApp = async (remoteMessage: any) => {
|
|
114
|
+
await openCommonNotification({ appToken, dev, remoteMessage, enableAlert, state: 'Background' })
|
|
129
115
|
}
|
|
130
116
|
|
|
131
|
-
|
|
132
|
-
|
|
117
|
+
const handleInitialNotification = async (remoteMessage: any) => {
|
|
118
|
+
await openCommonNotification({ appToken, dev, remoteMessage, enableAlert, state: 'Closed' })
|
|
119
|
+
}
|
|
133
120
|
|
|
121
|
+
const handleForegroundMessage = async (remoteMessage: any) => {
|
|
134
122
|
try {
|
|
135
123
|
PushNotification.configure({
|
|
136
124
|
onNotification: function (notification) {
|
|
137
125
|
console.log('LOCAL NOTIFICATION ==>', notification)
|
|
138
126
|
|
|
139
|
-
openCommonNotification({ appToken, dev, remoteMessage, enableAlert, state: '
|
|
127
|
+
openCommonNotification({ appToken, dev, remoteMessage, enableAlert, state: 'Foreground' })
|
|
140
128
|
},
|
|
141
129
|
popInitialNotification: true,
|
|
142
130
|
requestPermissions: true
|
|
@@ -186,5 +174,16 @@ export default async ({ appToken, dev, enableAlert, onNotificationOpenedApp }: n
|
|
|
186
174
|
showAlert(remoteMessage.data!.title, remoteMessage.data!.body)
|
|
187
175
|
}
|
|
188
176
|
}
|
|
189
|
-
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
messaging().setBackgroundMessageHandler(handleBackgroundMessage)
|
|
180
|
+
messaging().onNotificationOpenedApp(handleNotificationOpenedApp)
|
|
181
|
+
messaging().getInitialNotification().then(async (remoteMessage) => {
|
|
182
|
+
// console.log("Remote message ID:", remoteMessage?.messageId)
|
|
183
|
+
if (remoteMessage !== null)
|
|
184
|
+
handleUniqueRemoteMessage(remoteMessage, async (value) => {
|
|
185
|
+
await handleInitialNotification(value);
|
|
186
|
+
});
|
|
187
|
+
})
|
|
188
|
+
messaging().onMessage(handleForegroundMessage)
|
|
190
189
|
}
|