Redeem user's phone number

2

On Android, using Java, you can retrieve the device user's phone number with the following code:

TelephonyManager tMgr = (TelephonyManager)mAppContext.
     getSystemService(Context.TELEPHONY_SERVICE);
String numero = tMgr.getLine1Number();

Is it possible to register the user's number using Ionic + Angular?

    
asked by anonymous 19.10.2017 / 16:17

1 answer

3

You can use the YES lib of ionic-native . Example:

 getInformacoesSIM() {
    this.sim.requestReadPermission().then(() => {
      this.sim.getSimInfo().then((info) => {
        console.log(info);
      }, err => {
        console.log(err);
      })
    }, () => console.log('Permission denied'))
  }

If you prefer to avoid promising, you can also use async / await

 async getInformacoesSIM() {
    try {
      let permission = await this.sim.requestReadPermission();
      let info = await this.sim.getSimInfo();
      console.log(info);
    } catch (er) {
      console.log(er);
    }
  }

According to the documentation, on return you will have the following payload :

{
  "carrierName": "Android",
  "countryCode": "us",
  "mcc": "310",
  "mnc": "260",
  "phoneNumber": "15555215554",
  "deviceId": "0000000000000000",
  "simSerialNumber": "89014103211118510720",
  "subscriberId": "310260000000000",
  "callState": 0,
  "dataActivity": 0,
  "networkType": 3,
  "phoneType": 1,
  "simState": 5,
  "isNetworkRoaming": false
}
  

NOTE: the content of phoneNumber is unreliable (see this and this   article). Sometimes phoneNumber is only an empty string.

According to README.txt of lib, not all phones are "able" to return the phone number, in those cases the phoneNumber will be represented by an empty string.

    
19.10.2017 / 17:00