How to get the device uniqueid


#1

//this is my code
//its showing error undefine is not an object "RNDevice.Uniqueid;

import React, { Component } from ‘react’;

import { Platform, StyleSheet, View, Text, TouchableOpacity } from ‘react-native’;

import DeviceInfo from ‘react-native-device-info’;

export default class Myproject extends Component {

constructor(){

super();

this.state={

  DeviceID : ' '

}

}

getDeviceID=()=>{

var id = DeviceInfo.getUniqueID();

this.setState({

  DeviceID : id
 
})

}

render() {

return (

  <View style={styles.MainContainer}>

  
      <Text style={{textAlign: 'center', fontSize: 20, marginBottom: 10}}>{this.state.DeviceID}</Text>

      
      <TouchableOpacity onPress={this.getDeviceID} activeOpacity={0.5} style={styles.button} >

        <Text style={styles.TextStyle}> CLICK HERE TO GET DEVICE UNIQUE ID </Text>

      </TouchableOpacity>

  
  </View>
        
);

}
}

const styles = StyleSheet.create({

MainContainer :{

justifyContent: ‘center’,
alignItems: ‘center’,
flex:1,
paddingTop: (Platform.OS == ‘ios’ ? 20 : 0)

},

button: {

paddingTop: 10,
paddingBottom: 10,
width: ‘90%’,
backgroundColor: ‘#4CAF50’,
},

TextStyle:{
color:’#fff’,
textAlign:‘center’,
}

});


#2

Hey @aman123,

This is really hard to read. Can you throw it in a Snack? Also, one preliminary thought is that if you haven’t ejected, you won’t be able to use the react-native-device-info library as it requires you to make native code configurations.

Cheers,

Adam


#3

I think You can get unique expo token for mobile unique-ness.
then you can split string.

For Example :-

async getDeviceToken() {
const { status: existingStatus } = await Permissions.getAsync(
Permissions.NOTIFICATIONS
);
let finalStatus = existingStatus;

// only ask if permissions have not already been determined, because
// iOS won't necessarily prompt the user a second time.
if (existingStatus !== 'granted') {
  // Android remote notification permissions are granted during the app
  const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
  finalStatus = status;
}

// Stop here if the user did not grant permissions
if (finalStatus !== 'granted') {
  return;
}
// Get the token that uniquely identifies this device
// let DeviceToken = "";
try {
  let DeviceToken = await Notifications.getExpoPushTokenAsync();
  this.setState({ DeviceToken: DeviceToken });
  console.log('Our DeviceToken on login ' + DeviceToken);
} catch (error) {
  console.log('Our token error', error);
}

}

Output :- ExponentPushToken[XXXXXXXXXXXXXXXXXX]

sorry if i am wrong.


#4

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.