1

I need my Android application to redirect to a specific activity upon pressing the email link using Firebase authentication.

My application currently redirects to the launcher activity. I would like it to redirect to the profile activity but am using the username activity for this example. Any help is appreciated.

My Code:

MainActivity.java

@Override
public void onEmailReadListener(String email) {

    ActionCodeSettings actionCodeSettings =  buildActionCodeSettings();
    sendSignInLink(email, actionCodeSettings);
    //StartActivityForResult(true, actionCodeSettings);

}

//Email Verification
public ActionCodeSettings buildActionCodeSettings() {
    // [START auth_build_action_code_settings]
    ActionCodeSettings actionCodeSettings =
            ActionCodeSettings.newBuilder()
                    // URL you want to redirect back to. The domain (www.example.com) for this
                    // URL must be whitelisted in the Firebase Console.
                    .setUrl("https://example.page.link/nYJz")      
                    // This must be true
                    .setHandleCodeInApp(true)
                    .setAndroidPackageName(
                            "com.example.AppName",
                            false, /* installIfNotAvailable */
                            "23"    /* minimumVersion */)
                    .build();
    // [END auth_build_action_code_settings]

    return actionCodeSettings;
}

    public void sendSignInLink(String email, ActionCodeSettings actionCodeSettings) {
    // [START auth_send_sign_in_link]
    FirebaseAuth auth = FirebaseAuth.getInstance();
    auth.sendSignInLinkToEmail(email, actionCodeSettings)
            .addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if (task.isSuccessful()) {
                        Log.d(TAG, "Email sent.");
                    }
                    else{
                        Log.e(TAG, "onComplete: Failed=" + task.getException().getMessage());
                    }
                }
            });
    // [END auth_send_sign_in_link]
}

AndroidManifest.xml

<activity android:name=".Activities.Login.UserCreation.UsernameActivity">

        <intent-filter android:autoVerify="true">
            <category android:name="android.intent.category.BROWSABLE" />
            <category android:name="android.intent.category.DEFAULT"/>
            <action android:name="android.intent.action.VIEW"/>
            <data android:host="atlas-cd0f2.firebaseapp.com/" android:scheme="https" />
            <data android:host="https://example.page.link/" android:scheme="https" />
        </intent-filter>

 </activity>

UsernameActivity

public class UsernameActivity extends AppCompatActivity {

//create username for account
//TODO disable back; create cancel button
//TODO make ender button press next!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Button next;
TextInputEditText username;
Intent nextPass;
String mUsername;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_username);

    next = findViewById(R.id.nextOne);
    next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

                if (checkUsername() == true) {

                    mUsername = username.getText().toString();
                    nextPass.putExtra("username", mUsername);
                    startActivity(nextPass);

                }

                else {

                    Toast.makeText(getApplicationContext(), "Invalid Username", Toast.LENGTH_LONG).show();

                }

            }

        });

    nextPass = new Intent(UsernameActivity.this, PasswordActivity.class);

}

public boolean checkUsername() {

    Log.v("user0", "0");

    username = findViewById(R.id.username);

    int j;

    String strUsername = username.getText().toString();

    Log.v("user1", strUsername);

    if (strUsername.length() >= 6 && strUsername.length() <= 30){

        Log.v("user2", "2");

        char[] chUsername = strUsername.toCharArray();

        char[] badChar = {'&', '=', '_', '\'', '-', '+', ',', '<', '>', '.'};

        for (int i = 0; i < chUsername.length; i++) {
            Log.v("user3", String.valueOf(i));
            Log.v("user3.2", String.valueOf(chUsername[i]));

            for (j = 0; j < badChar.length; j++) {
                Log.v("user3.5", String.valueOf(j));

                if (chUsername[i] == badChar[j]) {

                    Log.v("user4", String.valueOf(j));
                    Log.v("user5", String.valueOf(i));

                    j = 0;
                    Log.v("user6", String.valueOf(j));
                    return false;

                }

            }

        }

        return true;

    }
    Log.v("user8", "8");

    return false;

}

}

StartActivity in AndroidManifest This is the activity with the launcher designation

<activity android:name=".Activities.Login.StartActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
  • Can you post the code for the `UsernameActivity` please? This is a bit unusual, I think you might be opening the `MainActivity` due to some condition – Nizar Jan 20 '20 at 18:33
  • @Nizar I uploaded the UsernameActivity, do you have any ideas on what condition it could be? – Alex Michael Jan 23 '20 at 16:54
  • I'll check it in a bit, I need to try it myself to be honest, there's nothing that seems wrong in this... **Are you trying to go from the MainActivity to the UserNameActivity with a DeepLink, in the application itself?** – Nizar Jan 23 '20 at 17:01
  • @Nizar No, the user is sent an email link which brings them back into the application to a specific activity, currently set to the username activity. Unfortunately it is not sending them to the username activity, it sends them to the launcher activity, which is the base behavior of deep linking from what I have read but if I use an intent filter as I have from what I understand it should lead me to the username activity. – Alex Michael Jan 23 '20 at 17:11
  • I think I know where the problem is, I just want to test it first. However, can you please share the `MainActivity` part of the `AndroidManifest`, I'm curious if it has any `deep-links` – Nizar Jan 23 '20 at 17:12
  • @Nizar Ive just posted the android manifest code for the StartActivity which is the activity with the Launcher designation (the activity the app returns to upon receiving the intent). – Alex Michael Jan 23 '20 at 17:17
  • Alright, I posted an answer that hopefully should fix your issue. Kindly let me know if it works or not... – Nizar Jan 23 '20 at 17:46

1 Answers1

0

The data tags in the Manifest should look like this

<data
    android:host="example.page.link"
    android:scheme="https" />
<data
    android:host="atlas-cd0f2.firebaseapp.com"
    android:scheme="https" />

You have to make sure that the android:host exactly matches the host, without any scheme (such as https) or prefix (such as /something).

Nizar
  • 2,034
  • 1
  • 20
  • 24