From bb5d453a2be2c7c70c75b9314de9b63b4ea30e7f Mon Sep 17 00:00:00 2001 From: Arnaud Vergnet Date: Mon, 27 Apr 2020 22:05:36 +0200 Subject: [PATCH] Replaced expo permissions by react native one --- .../generated/BasePackageList.java | 1 - package.json | 2 +- src/utils/Notifications.js | 37 ++++++++++++------- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/android/app/src/main/java/fr/amicaleinsat/application/generated/BasePackageList.java b/android/app/src/main/java/fr/amicaleinsat/application/generated/BasePackageList.java index 0b8a095..e9b026a 100644 --- a/android/app/src/main/java/fr/amicaleinsat/application/generated/BasePackageList.java +++ b/android/app/src/main/java/fr/amicaleinsat/application/generated/BasePackageList.java @@ -16,7 +16,6 @@ public class BasePackageList { new expo.modules.imageloader.ImageLoaderPackage(), new expo.modules.keepawake.KeepAwakePackage(), new expo.modules.lineargradient.LinearGradientPackage(), - new expo.modules.localization.LocalizationPackage(), new expo.modules.location.LocationPackage(), new expo.modules.permissions.PermissionsPackage(), new expo.modules.securestore.SecureStorePackage(), diff --git a/package.json b/package.json index 021965a..916508e 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,6 @@ "expo": "^37.0.0", "expo-barcode-scanner": "~8.1.0", "expo-camera": "latest", - "expo-permissions": "~8.1.0", "expo-secure-store": "~8.1.0", "i18n-js": "^3.3.0", "react": "~16.9.0", @@ -45,6 +44,7 @@ "react-native-localize": "^1.4.0", "react-native-modalize": "^1.3.6", "react-native-paper": "^3.8.0", + "react-native-permissions": "^2.1.3", "react-native-reanimated": "~1.7.0", "react-native-render-html": "^4.1.2", "react-native-safe-area-context": "0.7.3", diff --git a/src/utils/Notifications.js b/src/utils/Notifications.js index 52627f1..c4ab06e 100644 --- a/src/utils/Notifications.js +++ b/src/utils/Notifications.js @@ -1,6 +1,6 @@ // @flow -import * as Permissions from 'expo-permissions'; +import {checkNotifications, requestNotifications, RESULTS} from 'react-native-permissions'; import {Notifications} from 'expo'; import AsyncStorageManager from "../managers/AsyncStorageManager"; import LocaleManager from "../managers/LocaleManager"; @@ -14,13 +14,22 @@ const EXPO_TOKEN_SERVER = 'https://etud.insa-toulouse.fr/~amicale_app/expo_notif * @returns {Promise} */ export async function askPermissions() { - const {status: existingStatus} = await Permissions.getAsync(Permissions.NOTIFICATIONS); - let finalStatus = existingStatus; - if (existingStatus !== 'granted') { - const {status} = await Permissions.askAsync(Permissions.NOTIFICATIONS); - finalStatus = status; - } - return finalStatus === 'granted'; + return new Promise(((resolve, reject) => { + checkNotifications().then(({status, settings}) => { + if (status === RESULTS.GRANTED) + resolve(); + else if (status === RESULTS.BLOCKED) + reject() + else { + requestNotifications().then(({status, settings}) => { + if (status === RESULTS.GRANTED) + resolve(); + else + reject(); + }); + } + }); + })); } /** @@ -33,12 +42,12 @@ export async function askPermissions() { export async function initExpoToken() { let token = AsyncStorageManager.getInstance().preferences.expoToken.current; if (token === '') { - try { - await askPermissions(); - let expoToken = await Notifications.getExpoPushTokenAsync(); - // Save token for instant use later on - AsyncStorageManager.getInstance().savePref(AsyncStorageManager.getInstance().preferences.expoToken.key, expoToken); - } catch (e) {} + askPermissions().then(() => { + Notifications.getExpoPushTokenAsync().then((token) => { + // Save token for instant use later on + AsyncStorageManager.getInstance().savePref(AsyncStorageManager.getInstance().preferences.expoToken.key, token); + }); + }); } }