0

Is there any good react native library to get primary email address of android devices. There is a Stackoverflow question to get it with native android (link). I need include it in react native android app.

Update: I could get email address from following method with android native. Now I need to implement it in react native.

permission:

uses-permission android:name="android.permission.GET_ACCOUNTS"

method:

  String emailAddress = "";
    private String getEmail() {
        int i = 0;
        try {
            Account[] accounts = AccountManager.get(this).getAccountsByType(
                    "com.google");
            for (Account account : accounts) {
                if (i > 0)
                    sGoogleId = account.name;
                else
                    sGoogleId = account.name;
                i++;

            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        return emailAddress;
    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
LittleOne
  • 131
  • 1
  • 4
  • 16
  • 1
    That question's accepted answer is incorrect, insofar as it is not guaranteed to get you any valid email address. – CommonsWare Mar 18 '17 at 14:37
  • @CommonsWare Atleast I need to get a list of all account names on the device. In our case we can have a rule to restrict it to a single account. Is it possible? – LittleOne Mar 18 '17 at 14:41
  • I do not know whether React Native has any support for this. My point is that you cannot get a useful user's email address this way. If you want to get the email address that the user wants to use, ask them for their email address. – CommonsWare Mar 18 '17 at 15:00
  • @CommonsWare we can't do that. We need to restrict users based on email domain address. – LittleOne Mar 18 '17 at 15:29
  • That is impractical. There are many email apps and users that do not use the Android account system. You have no means of accessing those email addresses programmatically. Conversely, there are lots of people who might have a Google account registered, but they never use any associated Gmail account. Or, the Gmail account is not one that the user wants you to use (e.g., it is a work account). – CommonsWare Mar 18 '17 at 15:57
  • @CommonsWare: I could retrieve that email id as in updated code. Now only need to convert it to react native. – LittleOne Mar 19 '17 at 08:37

1 Answers1

0

I could solve this by using react native Native module. Device should have at-least one google account. Since it takes some time to process the request need to collect with a promise (androidManager.getAccounts().then(){}.Done(){}).

LittleOne
  • 131
  • 1
  • 4
  • 16
  • 1
    I fail to see how this is the answer? – Norfeldt Mar 09 '18 at 22:18
  • @Norfeldt Is there any wrong with above answer? It sorted my problem. What is the problem you have? – LittleOne Mar 11 '18 at 14:57
  • Trying `import { androidManager } from 'react-native'` is not possible. I can't find any docs on androidManager on the offical react native site – Norfeldt Mar 11 '18 at 14:59
  • @Norfeldt You can't use it like that. Above code is native android code. But you can use native codes in a react native apps (react native Native module). – LittleOne Mar 11 '18 at 15:30
  • Check following link. You can use that library. https://www.npmjs.com/package/react-native-account-manager – LittleOne Mar 11 '18 at 15:32
  • Thx but I already tried that - https://github.com/GoIntegro/react-native-account-manager/issues/4 – Norfeldt Mar 11 '18 at 15:47
  • @Norfeldt I have created above additional method in native code(RN Native module) and use following code in react native. It works fine. var am = NativeModules.RNAccountManagerModule; am.getAccounts() .catch((err) => { //alert('Can not read user account primary email address'); }).done((response) => {if(!response) { alert("Check whether all permissions are granted"); }else {this.setState({ sitemail: response }); //alert(this.state.sitemail) } }); }); – LittleOne Mar 11 '18 at 16:14
  • `var am = NativeModules.RNAccountManagerModule; am.getAccounts() .catch((err) => { //alert('Can not read user account primary email address'); }).done((response) => {if(!response) { alert("Check whether all permissions are granted"); }else {this.setState({ sitemail: response }); //alert(this.state.sitemail) } }); });` – LittleOne Mar 11 '18 at 16:20
  • I might be stupid at this, but I don't know how to implement it. I do know the concept about NativeModules (https://facebook.github.io/react-native/docs/native-modules-ios.html). What (and where) JAVA code do I need to drop into the android folder? – Norfeldt Mar 11 '18 at 19:43
  • Check the following links 1: [how to](https://shift.infinite.red/native-modules-for-react-native-android-ac05dbda800d) 2. [intro](http://facebook.github.io/react-native/docs/native-modules-android.html) – LittleOne Mar 12 '18 at 04:35
  • @LittleOne you should share the entire code here. – red-devil Mar 28 '22 at 09:06