|
@@ -1,6 +1,6 @@
|
1
|
1
|
// @flow
|
2
|
2
|
|
3
|
|
-import * as Permissions from 'expo-permissions';
|
|
3
|
+import {checkNotifications, requestNotifications, RESULTS} from 'react-native-permissions';
|
4
|
4
|
import {Notifications} from 'expo';
|
5
|
5
|
import AsyncStorageManager from "../managers/AsyncStorageManager";
|
6
|
6
|
import LocaleManager from "../managers/LocaleManager";
|
|
@@ -14,13 +14,22 @@ const EXPO_TOKEN_SERVER = 'https://etud.insa-toulouse.fr/~amicale_app/expo_notif
|
14
|
14
|
* @returns {Promise}
|
15
|
15
|
*/
|
16
|
16
|
export async function askPermissions() {
|
17
|
|
- const {status: existingStatus} = await Permissions.getAsync(Permissions.NOTIFICATIONS);
|
18
|
|
- let finalStatus = existingStatus;
|
19
|
|
- if (existingStatus !== 'granted') {
|
20
|
|
- const {status} = await Permissions.askAsync(Permissions.NOTIFICATIONS);
|
21
|
|
- finalStatus = status;
|
22
|
|
- }
|
23
|
|
- return finalStatus === 'granted';
|
|
17
|
+ return new Promise(((resolve, reject) => {
|
|
18
|
+ checkNotifications().then(({status, settings}) => {
|
|
19
|
+ if (status === RESULTS.GRANTED)
|
|
20
|
+ resolve();
|
|
21
|
+ else if (status === RESULTS.BLOCKED)
|
|
22
|
+ reject()
|
|
23
|
+ else {
|
|
24
|
+ requestNotifications().then(({status, settings}) => {
|
|
25
|
+ if (status === RESULTS.GRANTED)
|
|
26
|
+ resolve();
|
|
27
|
+ else
|
|
28
|
+ reject();
|
|
29
|
+ });
|
|
30
|
+ }
|
|
31
|
+ });
|
|
32
|
+ }));
|
24
|
33
|
}
|
25
|
34
|
|
26
|
35
|
/**
|
|
@@ -33,12 +42,12 @@ export async function askPermissions() {
|
33
|
42
|
export async function initExpoToken() {
|
34
|
43
|
let token = AsyncStorageManager.getInstance().preferences.expoToken.current;
|
35
|
44
|
if (token === '') {
|
36
|
|
- try {
|
37
|
|
- await askPermissions();
|
38
|
|
- let expoToken = await Notifications.getExpoPushTokenAsync();
|
39
|
|
- // Save token for instant use later on
|
40
|
|
- AsyncStorageManager.getInstance().savePref(AsyncStorageManager.getInstance().preferences.expoToken.key, expoToken);
|
41
|
|
- } catch (e) {}
|
|
45
|
+ askPermissions().then(() => {
|
|
46
|
+ Notifications.getExpoPushTokenAsync().then((token) => {
|
|
47
|
+ // Save token for instant use later on
|
|
48
|
+ AsyncStorageManager.getInstance().savePref(AsyncStorageManager.getInstance().preferences.expoToken.key, token);
|
|
49
|
+ });
|
|
50
|
+ });
|
42
|
51
|
}
|
43
|
52
|
}
|
44
|
53
|
|