Skip to content

Instantly share code, notes, and snippets.

@Salakar
Created February 16, 2018 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Salakar/593a6e2082f3ab19cf80ebf877fb5572 to your computer and use it in GitHub Desktop.
Save Salakar/593a6e2082f3ab19cf80ebf877fb5572 to your computer and use it in GitHub Desktop.
Examples of dealing with play services versions
import firebase from 'react-native-firebase';
// disable default behaviour so we can control the flow ourselves
// this mus be called module scope (outside any classes etc)
// preferably before any other usages of RNFirebase so that
// it's disabled before internal RNFirebase logic takes over
firebase.utils().errorOnMissingPlayServices = false;
firebase.utils().promptOnMissingPlayServices = false;
/**
* Call me in your startup logic - before firebase usage
* This example does pretty much what promptOnMissingPlayServices = true does.
* @return {Promise.<T>}
*/
async function checkPlayServicesBasicExample() {
const utils = firebase.utils();
const {
isAvailable,
hasResolution,
isUserResolvableError,
} = utils.playServicesAvailability;
// all good and valid \o/
if (isAvailable) return Promise.resolve();
// if the user can resolve the issue i.e by updating play services
// then call Google Play's own/default prompting functionality
if (isUserResolvableError) {
return utils.promptForPlayServices();
}
// call Google Play's own/default resolution functionality
if (hasResolution) {
return utils.resolutionForPlayServices();
}
// There's no way to resolve play services on this device
// probably best to show a dialog / force crash the app
return Promise.reject(
new Error('Unable to find a valid play services version.')
);
}
/**
* Call me in your startup logic - before firebase usage
* This example shows usage of status codes to allow custom dialogs/user feedback.
*
* @return {Promise.<T>}
*/
async function checkPlayServicesAdvancedExample() {
const utils = firebase.utils();
const {
status,
isAvailable,
hasResolution,
isUserResolvableError,
} = utils.playServicesAvailability;
// all good and valid \o/
if (isAvailable) return Promise.resolve();
// if the user can resolve the issue i.e by updating play services
// then call Google Play's own/default prompting functionality
if (isUserResolvableError || hasResolution) {
switch (status) {
case 1:
// SERVICE_MISSING - Google Play services is missing on this device.
// show something to user
// and then attempt to install if necessary
return utils.makePlayServicesAvailable();
case 2:
// SERVICE_VERSION_UPDATE_REQUIRED - The installed version of Google Play services is out of date.
// show something to user
// and then attempt to update if necessary
return utils.resolutionForPlayServices();
// TODO handle other cases as necessary, see link below for all codes and descriptions
// TODO e.g. https://developers.google.com/android/reference/com/google/android/gms/common/ConnectionResult#SERVICE_VERSION_UPDATE_REQUIRED
default:
// some default dialog / component?
if (isUserResolvableError) return utils.promptForPlayServices();
if (hasResolution) return utils.resolutionForPlayServices();
}
}
// There's no way to resolve play services on this device
// probably best to show a dialog / force crash the app
return Promise.reject(
new Error('Unable to find a valid play services version.')
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment