-
Notifications
You must be signed in to change notification settings - Fork 5
/
App.js
272 lines (248 loc) · 8.87 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import React, {Component} from 'react';
import {NavigationContainer} from '@react-navigation/native';
import {createNativeStackNavigator} from '@react-navigation/native-stack';
import AsyncStorage from '@react-native-async-storage/async-storage';
import {Platform} from 'react-native';
//Screens
import HomeScreen from './screens/HomeScreen';
import PersonalizeScreen from './screens/PersonalizeScreen';
import ChatScreen from './screens/ChatScreen';
import SubviewChatScreen from './screens/SubviewChatScreen';
import MultiThreadChatScreen from './screens/MultiThreadChatScreen';
import TestDeeplinkingScreen from './screens/testDeeplinkingScreen';
import TestDeeplinkingScreen2 from './screens/testDeeplinkingScreen2';
import {mobileMessaging} from 'infobip-mobile-messaging-react-native-plugin';
import {webRTCUI} from 'infobip-mobile-messaging-react-native-plugin';
import Colors from './constants/Colors';
import MyMessageStorage from './constants/MyMessageStorage';
import NativeDialogManagerAndroid from 'react-native/Libraries/NativeModules/specs/NativeDialogManagerAndroid';
import {Rationale} from 'react-native';
const Stack = createNativeStackNavigator();
const myMessageStorage = MyMessageStorage;
export default class App extends Component {
androidGeoProminentDisclosureAcceptedKey: string =
'androidGeoProminentDisclosureAcceptedKey';
isAndroidGeoProminentDisclosureAccepted(): Promise<Boolean> {
return AsyncStorage.getItem(
this.androidGeoProminentDisclosureAcceptedKey,
).then(value => {
if (value === null) {
return Promise.resolve(false);
}
return Promise.resolve(JSON.parse(value));
});
}
configuration = {
applicationCode: 'Your mobile push profile application code',
webRTCUI: {
configurationId: 'Your webrtc push configuration id'
},
ios: {
notificationTypes: ['alert', 'badge', 'sound'],
logging: true,
},
messageStorage: myMessageStorage,
inAppChatEnabled: true,
fullFeaturedInAppsEnabled: true,
geofencingEnabled: false,
loggingEnabled: true,
};
constructor() {
super();
this.state = {
logInfo: '...',
};
this.subscriptions = [];
if (Platform.OS === 'ios' || !this.configuration.geofencingEnabled) {
this.initMobileMessaging();
return;
}
/* Starting Mobile Messaging SDK for android in case geofencingEnabled: true */
// 1. Setup geo disclosure dialog
// Per Google requirements - "If your app accesses location in the background, you must provide an in-app disclosure of your data access, collection, use, and sharing."
let androidGeoDisclosureDialog = {
message:
'This application collects location data to be able to trigger notification about entering geo region even when the app is closed or not in use.',
buttonPositive: 'accept',
buttonNeutral: 'decline',
};
// 2. Setup rationale which will be displayed if it's required for asking geofencing permissions.
//It'll be displayed before showing settings screen with location permissions.
let androidGeoPermissionsRationaleOptions = {
title: 'Update location settings',
message:
'Allow application to access your location all the time to let it trigger geo notifications even when app is closed or not in use',
buttonNegative: 'cancel',
buttonPositive: 'settings',
};
// 3. Check was geo disclosure dialog accepted or not
this.isAndroidGeoProminentDisclosureAccepted().then(accepted => {
if (accepted === true) {
this.initMobileMessagingWithRequestingGeoPermissions(
androidGeoPermissionsRationaleOptions,
);
return;
}
// 4. Show geo disclosure dialog if it's not accepted
// Example application displays this dialog until user accepts it.
NativeDialogManagerAndroid.showAlert(
androidGeoDisclosureDialog,
() => {},
(action, buttonKey) => {
console.log('Button key: ' + buttonKey);
if (buttonKey === -1) {
//accepted
AsyncStorage.setItem(
this.androidGeoProminentDisclosureAcceptedKey,
JSON.stringify(true),
);
// 4.1 Request geofencing permissions and initialize Mobile Messaging
this.initMobileMessagingWithRequestingGeoPermissions(
androidGeoPermissionsRationaleOptions,
);
} else {
// 4.2 initialize Mobile Messaging
// Initialize will be called with geofencingEnabled: true, user can give permissions later.
// Geofencing won't work until permissions are granted.
this.initMobileMessaging();
}
},
);
});
}
componentDidMount() {
mobileMessaging.supportedEvents.concat(
mobileMessaging.inAppChatEvents,
).forEach(event => {
let subscription = mobileMessaging.subscribe(
event,
this.handleMobileMessagingEvent,
);
this.subscriptions.push(subscription);
});
}
componentWillUnmount() {
this.subscriptions.forEach(subscription => {
mobileMessaging.unsubscribe(subscription);
});
}
handleMobileMessagingEvent = value => {
this.updateLogInfo('Event: ' + JSON.stringify(value));
};
initMobileMessagingWithRequestingGeoPermissions(
androidGeoPermissionsRationale: Rationale,
) {
//Request geofencing permissions using mobileMessaging method, you can create your own implementation if provided isn't suite.
mobileMessaging
.requestAndroidLocationPermissions(androidGeoPermissionsRationale)
.then(granted => {
if (!granted) {
console.log('Required Geofencing permissions are not granted.');
}
// Initialize will be called with geofencingEnabled: true, user can give permissions later.
// Geofencing won't work until permissions are granted.
this.initMobileMessaging();
});
}
initMobileMessaging() {
mobileMessaging.init(
this.configuration,
() => {
this.updateLogInfo('MobileMessaging started');
webRTCUI.enableChatCalls(
() => console.log("Calls enabled"),
error => console.log("Calls enable error "+JSON.stringify(error))
)
},
error => this.updateLogInfo('MobileMessaging error: ' + JSON.stringify(error)),
);
}
updateLogInfo(info) {
console.log(info);
this.setState({logInfo: info});
}
render() {
return (
<NavigationContainer>
<Stack.Navigator
initialRouteName="HomeScreen"
screenOptions={{
contentStyle: {backgroundColor: Colors.tintWhite},
headerTintColor: 'white',
headerStyle: {backgroundColor: Colors.primary500},
}}>
<Stack.Screen name="HomeScreen" component={HomeScreen} />
<Stack.Screen
name="PersonalizeScreen"
component={PersonalizeScreen}
options={{title: 'Personalize'}}
/>
<Stack.Screen
name="ChatScreen"
component={ChatScreen}
options={{
title: 'My Chat Title',
headerStyle: {
backgroundColor: Colors.primary500,
},
headerTintColor: Colors.tintWhite,
headerTitleStyle: {
fontWeight: 'bold',
},
}}
/>
<Stack.Screen
name="SubviewChatScreen"
component={SubviewChatScreen}
options={{
title: 'Subview Chat',
headerTintColor: Colors.tintWhite,
headerTitleStyle: {
fontWeight: 'bold',
},
}}
/>
<Stack.Screen
name="MultiThreadChatScreen"
component={MultiThreadChatScreen}
options={{
title: 'Multi-Thread Chat',
headerTintColor: Colors.tintWhite,
headerTitleStyle: {
fontWeight: 'bold',
},
}}
/>
<Stack.Screen
name="TestDeeplinking"
component={TestDeeplinkingScreen}
options={{
title: 'Test Deeplinking',
headerStyle: {
backgroundColor: Colors.primary500,
},
headerTintColor: Colors.tintWhite,
headerTitleStyle: {
fontWeight: 'bold',
},
}}
/>
<Stack.Screen
name="TestDeeplinking2"
component={TestDeeplinkingScreen2}
options={{
title: 'Test Deeplinking2',
headerStyle: {
backgroundColor: Colors.primary500,
},
headerTintColor: Colors.tintWhite,
headerTitleStyle: {
fontWeight: 'bold',
},
}}
/>
</Stack.Navigator>
</NavigationContainer>
);
}
}