user3233160
user3233160

Reputation: 11

Push notification android GCM

I'm a beginner with android apps.

I found this tutorial and it works, but in permissions is GET_ACCOUNTS, and when app is run there is display input for name and email. How I can make change, without input name and email, just taking from GET_ACCOUNTS? I don't understand it. Can anybody explain how?

Upvotes: 1

Views: 87

Answers (1)

In my last apk, I had the same issue. But I found in This answer.

I hope it's useful

In manifest check the permission:

<uses-permission android:name="android.permission.GET_ACCOUNTS" />

And in the method:

public String getEmail(){
    String possibleEmail= "no email";
    Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
    Account[] accounts = AccountManager.get(ctx).getAccounts();
    for (Account account : accounts) {
        if (emailPattern.matcher(account.name).matches()) {
            possibleEmail = account.name;              
        }
    }

    return possibleEmail;

}

And in example change to:

protected void onRegistered(Context context, String registrationId) {
    Log.i(TAG, "Device registered: regId = " + registrationId);
    displayMessage(context, "Your device registred with GCM");
    Log.d("NAME", MainActivity.name);
    ServerUtilities.register(context, MainActivity.name, getEMail(), registrationId);
}

Upvotes: 1

Related Questions