Avery235
Avery235

Reputation: 5306

Expo get unique device id without ejecting

This library allows you to get unique device id / Mac address of Android devices, which doesn't change after reinstallation.

Expo.Constants.deviceId changes after every reinstallation (even if the app version number is the same).

Is there a way to get an unique id for Android that doesn't change after reinstallation (at least for if it's the same version), without ejecting?

Upvotes: 29

Views: 29326

Answers (6)

Dmitro_
Dmitro_

Reputation: 591

Use Application.getAndroidId() (Application.androidId, for SDK < 50) from expo-application. Id will not change after reinstall or update. The value may change if a factory reset is performed on the device or if an APK signing key changes. https://docs.expo.dev/versions/latest/sdk/application/#applicationandroidid

Example:

import * as Application from 'expo-application';
import { Platform } from 'expo-modules-core';
import * as SecureStore from 'expo-secure-store';
import Constants from 'expo-constants';

const getDeviceId = async () => {
  if (Platform.OS === 'android') {
    // for SDK < 50
    // return Application.androidId;
 
    return Application.getAndroidId();

  } else {
    let deviceId = await SecureStore.getItemAsync('deviceId');

    if (!deviceId) {
      deviceId = Constants.deviceId; //or generate uuid
      await SecureStore.setItemAsync('deviceId', deviceId);
    }

    return deviceId;
  }
}

Upvotes: 10

Ernestyno
Ernestyno

Reputation: 857

Best way to do is use ANDROID_ID for android or IDFV for iOS.

For Expo here is package: https://docs.expo.dev/versions/latest/sdk/application/#applicationgetiosidforvendorasync

My code:

const newUniqueId = Platform.OS === 'android'
            ? Application.androidId ?? uuid.v4().toString()
            : await Application.getIosIdForVendorAsync() ?? uuid.v4().toString();

let uniqueId = null;
try {
    uniqueId = await SecureStore.getItemAsync('uniqueId');
} catch (error) {
    LoggerService.logToSentry(error);
}

try {
    if (!uniqueId) {
        uniqueId = newUniqueId;
        await SecureStore.setItemAsync('uniqueId', uniqueId);
    }
} catch (error) {
    uniqueId = newUniqueId;
    LoggerService.logToSentry(error);
}

return uniqueId;

If some one use only uuid.v4() and stores with SecureStore after app deletion all data will be deleted except iOS phones. On Android all App data will be deleted with stored keys

Upvotes: 3

Morris S
Morris S

Reputation: 2594

For Expo IOS theres currently very limited options, Since Apple forbids getting private device info. We will need to create our own unique identifier below.

Solution:

My solution is a combination of uuid and Expo SecureStore works for IOS and Android.

import * as SecureStore from 'expo-secure-store';
import 'react-native-get-random-values';
import { v4 as uuidv4 } from 'uuid';


let uuid = uuidv4();
await SecureStore.setItemAsync('secure_deviceid', JSON.stringify(uuid));
let fetchUUID = await SecureStore.getItemAsync('secure_deviceid');
console.log(fetchUUID)

This solution will work even if app gets reinstalled, or if user switches devices and copy's all data to new device. (Expo.Constants.deviceId is deprecated and will be removed in Expo SDK 44).

Full Example:

To check if you already stored the uuid in SecureStore

import * as SecureStore from 'expo-secure-store';
import 'react-native-get-random-values';
import { v4 as uuidv4 } from 'uuid';

let uuid = uuidv4();
let fetchUUID = await SecureStore.getItemAsync('secure_deviceid');
  //if user has already signed up prior
  if (fetchUUID) {
    uuid = fetchUUID
  }
await SecureStore.setItemAsync('secure_deviceid', JSON.stringify(uuid));
console.log(uuid)

Upvotes: 32

nrooban
nrooban

Reputation: 197

To get the device UniqueId in expo project,

  1. npm install react-native-device-info
  2. expo run:ios or expo run:android

Please note: expo start will throw an error

enter image description here

Upvotes: -2

Bharat Varma
Bharat Varma

Reputation: 162

Just use getuniqueid() method from react-native-device-info. Works on iOS and android to uniquely identify a device .

Upvotes: -2

Keiser
Keiser

Reputation: 44

Guess you can use facebook module for this purpose. https://docs.expo.io/versions/latest/sdk/facebook-ads/#currentdevicehash

Not sure what happens under hood - but looks like it unique between app reinstal, device restart etc.

Upvotes: 0

Related Questions