@inngageregistry/inngage-react 2.3.3 → 3.0.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.
@@ -1,13 +1,12 @@
1
- import { Linking, Alert } from 'react-native'
1
+ import { Linking } from 'react-native'
2
2
  import InAppBrowser from 'react-native-inappbrowser-reborn'
3
3
  import messaging, { firebase } from '@react-native-firebase/messaging';
4
- import DeviceInfo from 'react-native-device-info'
5
- import { showAlert, showAlertLink } from './utils'
6
- import { notificationApi } from './inngageApi'
4
+ import { showAlert } from './utils'
5
+ import { notificationApi } from './services/inngage'
7
6
  import AsyncStorage from '@react-native-async-storage/async-storage';
8
- import PushNotification from 'react-native-push-notification'
7
+ import PushNotification, { Importance } from 'react-native-push-notification'
9
8
 
10
- export const linkInApp = (link) => {
9
+ export const linkInApp = (link: string) => {
11
10
  InAppBrowser.open(link, {
12
11
  dismissButtonStyle: 'cancel',
13
12
  preferredBarTintColor: 'gray',
@@ -28,7 +27,7 @@ export const linkInApp = (link) => {
28
27
  })
29
28
  }
30
29
 
31
- const openLinkByType = (type, url) => (type === 'deep' ? Linking.openURL(url) : linkInApp(url))
30
+ const openLinkByType = (type: string, url: string) => (type === 'deep' ? Linking.openURL(url) : linkInApp(url))
32
31
 
33
32
  export const openCommonNotification = (notificationData) => {
34
33
  const { appToken, dev, remoteMessage, enableAlert, state } = notificationData
@@ -47,40 +46,43 @@ export const openCommonNotification = (notificationData) => {
47
46
  },
48
47
  }
49
48
  if (!url) {
50
-
49
+
51
50
  return notificationApi(request, dev).then(() => {
52
-
53
- }).catch(console.log)
51
+
52
+ }).catch(console.error)
54
53
 
55
54
  }
56
55
  return Linking.canOpenURL(url).then((supported) => {
57
56
  if (supported) {
58
-
59
- supported && openLinkByType(type, url)
60
-
57
+
58
+ supported && openLinkByType(type, url)
59
+
61
60
  }
62
- }).catch(console.log)
61
+ }).catch(console.error)
63
62
  }
64
63
  export const openRichNotification = (notificationData) => {
65
64
  }
66
65
 
66
+ export interface notificationsListenerProps {
67
+ appToken: string,
68
+ dev?: boolean,
69
+ enableAlert: boolean,
70
+ onNotificationOpenedApp?: any,
71
+ }
72
+ export default async ({ appToken, dev, enableAlert, onNotificationOpenedApp }: notificationsListenerProps) => {
73
+ var messageArray: any = [];
67
74
 
68
-
69
- export default async ({ appToken, dev, enableAlert, onNotificationOpenedApp }) => {
70
- var messageArray = [];
71
-
72
-
73
75
  firebase.messaging().setBackgroundMessageHandler(async (remoteMessage) => {
74
76
  console.log('Push received: Background')
75
77
 
76
78
  const request = {
77
79
  notificationRequest: {
78
- id: remoteMessage.data.notId,
80
+ id: remoteMessage.data!.notId,
79
81
  app_token: appToken,
80
82
  },
81
83
  }
82
- if (remoteMessage != null && remoteMessage.data.additional_data) {
83
- let msg = JSON.parse(remoteMessage.data.additional_data)
84
+ if (remoteMessage != null && remoteMessage.data!.additional_data) {
85
+ let msg = JSON.parse(remoteMessage.data!.additional_data)
84
86
  console.log('first step')
85
87
  if (msg.inapp_message == true) {
86
88
  console.log('second step')
@@ -96,20 +98,20 @@ export default async ({ appToken, dev, enableAlert, onNotificationOpenedApp }) =
96
98
  })
97
99
  }, 3000)
98
100
  }
99
- } else if (remoteMessage != null && !remoteMessage.data.additional_data) {
101
+ } else if (remoteMessage != null && !remoteMessage.data!.additional_data) {
100
102
  setTimeout(() => {
101
103
  messaging().getInitialNotification().then(notification => {
102
- if (!remoteMessage.data.url) {
103
- notificationApi(request, dev)
104
+ if (!remoteMessage.data!.url) {
105
+ notificationApi(request, dev) // TODO, responsible for triggering the notification api multiple times
104
106
  } else {
105
107
  notificationApi(request, dev)
106
- Linking.canOpenURL(remoteMessage.data.url).then((supported) => {
108
+ Linking.canOpenURL(remoteMessage.data!.url).then((supported) => {
107
109
  if (supported) {
108
-
109
- supported && openLinkByType(remoteMessage.data.type, remoteMessage.data.url)
110
-
110
+
111
+ supported && openLinkByType(remoteMessage.data!.type, remoteMessage.data!.url)
112
+
111
113
  }
112
- }).catch(error => console.log(error))
114
+ }).catch(error => console.error(error))
113
115
  }
114
116
  })
115
117
  }, 3000)
@@ -121,9 +123,9 @@ export default async ({ appToken, dev, enableAlert, onNotificationOpenedApp }) =
121
123
  openCommonNotification({ appToken, dev, remoteMessage, enableAlert, state: 'Background' })
122
124
  });
123
125
 
124
- if(typeof onNotificationOpenedApp == 'function') {
126
+ if (typeof onNotificationOpenedApp == 'function') {
125
127
  const remoteMessage = await messaging().getInitialNotification();
126
- onNotificationOpenedApp(remoteMessage);
128
+ onNotificationOpenedApp(remoteMessage);
127
129
  }
128
130
 
129
131
  firebase.messaging().onMessage(async (remoteMessage) => {
@@ -131,46 +133,45 @@ export default async ({ appToken, dev, enableAlert, onNotificationOpenedApp }) =
131
133
 
132
134
  try {
133
135
  PushNotification.configure({
134
- onNotification: function(notification) {
136
+ onNotification: function (notification) {
135
137
  console.log('LOCAL NOTIFICATION ==>', notification)
136
138
 
137
139
  openCommonNotification({ appToken, dev, remoteMessage, enableAlert, state: 'foreground' })
138
-
139
140
  },
140
- channelId: "high_importance_channel",
141
- priority: "high",
142
141
  popInitialNotification: true,
143
142
  requestPermissions: true
144
143
  })
145
-
146
- } catch (e) {
147
- console.log(e)
148
144
 
149
- }
150
- try{
151
-
152
- PushNotification.presentLocalNotification({
153
- autoCancel: true,
154
- bigText:remoteMessage.data.body,
155
- title: remoteMessage.data.title,
156
- message: '',
157
- vibrate: true,
158
- vibration: 300,
145
+ PushNotification.createChannel({
146
+ channelId: 'high_importance_channel',
147
+ channelName: 'default',
148
+ importance: Importance.HIGH,
159
149
  playSound: true,
160
150
  soundName: 'default',
161
-
162
- })
163
- console.log('LOCAL NOTIFICATION : ')
164
- console.log(remoteMessage)
165
- }catch(e){
166
- onsole.log('LOCAL NOTIFICATION ERROR: ')
167
- console.log(e)
168
- }
169
-
151
+ vibrate: true
152
+ }, (created) => console.log(`createChannel returned '${created}'`));
170
153
 
154
+ } catch (e) {
155
+ console.error(e)
156
+ }
157
+ try {
158
+ PushNotification.localNotification({
159
+ autoCancel: true,
160
+ title: remoteMessage.data!.title,
161
+ message: remoteMessage.data!.body,
162
+ vibration: 300,
163
+ channelId: "high_importance_channel"
164
+ });
165
+
166
+ console.log('LOCAL NOTIFICATION : ')
167
+ console.log(remoteMessage)
168
+ } catch (e) {
169
+ console.error('LOCAL NOTIFICATION ERROR: ')
170
+ console.error(e)
171
+ }
171
172
 
172
- if (remoteMessage != null && remoteMessage.data.additional_data) {
173
- let msg = JSON.parse(remoteMessage.data.additional_data)
173
+ if (remoteMessage != null && remoteMessage.data!.additional_data) {
174
+ let msg = JSON.parse(remoteMessage.data!.additional_data)
174
175
  if (msg.inapp_message == true) {
175
176
  const currentMessages = await AsyncStorage.getItem('inngage');
176
177
  if (currentMessages !== null) {
@@ -179,13 +180,11 @@ export default async ({ appToken, dev, enableAlert, onNotificationOpenedApp }) =
179
180
  messageArray.push(remoteMessage);
180
181
  await AsyncStorage.setItem('inngage', JSON.stringify(messageArray));
181
182
  }
182
- } else if (remoteMessage != null && !remoteMessage.data.additional_data) {
183
- console.log(remoteMessage.data.title)
183
+ } else if (remoteMessage != null && !remoteMessage.data!.additional_data) {
184
+ console.log(remoteMessage.data!.title)
184
185
  if (enableAlert) {
185
- showAlert(remoteMessage.data.title, remoteMessage.data.body)
186
+ showAlert(remoteMessage.data!.title, remoteMessage.data!.body)
186
187
  }
187
188
  }
188
189
  });
189
-
190
-
191
190
  }
@@ -0,0 +1,42 @@
1
+ const baseUrlHook = {
2
+ [false as any]: 'https://api.inngage.com.br/v1',
3
+ [undefined as any]: 'https://api.inngage.com.br/v1',
4
+ [null as any]: 'https://api.inngage.com.br/v1',
5
+ [true as any]: 'https://apid.inngage.com.br/v1',
6
+ }
7
+
8
+ const requestConfigFactory = (method, request) => {
9
+
10
+ let header: any = {
11
+ Accept: 'application/json',
12
+ 'Content-Type': 'application/json',
13
+ }
14
+ try{
15
+ if (request?.registerSubscriberRequest?.authKey) {
16
+ header = {
17
+ ...header,
18
+ Authorization: request.registerSubscriberRequest.authKey
19
+ }
20
+ }
21
+ }catch(e){
22
+ console.error(e)
23
+ }
24
+ let objToSend = {
25
+ method,
26
+ body: JSON.stringify(request),
27
+ headers: header
28
+ }
29
+
30
+ return objToSend
31
+ }
32
+
33
+ export const fetchClient = (method, requestBody, path, isDev = false): Promise<Response> => {
34
+ return new Promise((resolve) => {
35
+ const url = String(baseUrlHook[isDev as any]).concat(path)
36
+ const request = requestConfigFactory(method, requestBody)
37
+ console.log(request)
38
+ fetch(url, request)
39
+ .then(resolve)
40
+ .catch(err => console.error('Fetch Error', err))
41
+ })
42
+ }
@@ -0,0 +1,17 @@
1
+ import { fetchClient } from './handler'
2
+
3
+ export const subscriptionApi = (request, dev = false) => {
4
+ if (dev)
5
+ console.log('subscriptionApi', request)
6
+ return fetchClient('POST', request, '/subscription/', !!dev)
7
+ }
8
+ export const notificationApi = (request, dev = false) => {
9
+ if (dev)
10
+ console.log('notificationApi', request)
11
+ return fetchClient('POST', request, '/notification/', !!dev)
12
+ }
13
+ export const eventsApi = (request, dev = false) => {
14
+ if (dev)
15
+ console.log('eventsApi', request)
16
+ return fetchClient('POST', request, '/events/newEvent/', !!dev)
17
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,51 @@
1
+ import { Alert } from 'react-native'
2
+
3
+ export const formatDate = (timestamp) => {
4
+ if (!timestamp) {
5
+ return null
6
+ }
7
+ return new Date(timestamp).toISOString()
8
+ }
9
+
10
+ export const showAlert = (title: string, body: string) => {
11
+ Alert.alert(title, body, [{ text: 'OK', onPress: () => console.log('OK Pressed') }], {
12
+ cancelable: false,
13
+ })
14
+ }
15
+
16
+ export const showAlertLink = (title: string, body: string, appName: string, link: string) => {
17
+ return new Promise((resolve) => {
18
+ Alert.alert(
19
+ title,
20
+ `${body}\n\n${link}`,
21
+ [{ text: 'NO', onPress: () => resolve(false) }, { text: 'OK', onPress: () => resolve(true) }],
22
+ { cancelable: false },
23
+ )
24
+ })
25
+ }
26
+
27
+ export const subscriptionRequestAdapter = (sdkRequest, useCustomData, customData) => {
28
+ if (useCustomData) {
29
+ return {
30
+ registerSubscriberRequest: {
31
+ ...sdkRequest.registerSubscriberRequest,
32
+ custom_field: customData
33
+ }
34
+ }
35
+ }
36
+ return {
37
+ registerSubscriberRequest: {
38
+ ...sdkRequest.registerSubscriberRequest
39
+ }
40
+ };
41
+ }
42
+
43
+ export const isEmpty = (obj: any) => {
44
+ for (var prop in obj) {
45
+ if (obj.hasOwnProperty(prop)) {
46
+ return false;
47
+ }
48
+ }
49
+
50
+ return JSON.stringify(obj) === JSON.stringify({});
51
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es5",
4
+ "module": "es6",
5
+ "declaration": true,
6
+ "outDir": "./build",
7
+ "strict": true,
8
+ "jsx": "react-native",
9
+ "esModuleInterop": true,
10
+ "allowSyntheticDefaultImports": true,
11
+ "skipLibCheck": true,
12
+ "moduleResolution": "node",
13
+ "noImplicitAny": false,
14
+ "lib": [ "es2015" ]
15
+ },
16
+ "include": [
17
+ "**/*"
18
+ ],
19
+ "exclude": [
20
+ "node_modules",
21
+ "**/__tests__/*",
22
+ "build"
23
+ ]
24
+ }