9

On the Firebase documentation:

it says:

To receive the deep link, call the getInvitation method

However the deep links, surviving installations, seem to work even without implementing the code described there.

So, is it really needed to call the getInvitation method? what is it exactly for?

NSNoob
  • 5,548
  • 6
  • 41
  • 54
Daniele B
  • 19,801
  • 29
  • 115
  • 173

2 Answers2

3

getInvitation() is to handle the deep link intent. It is recommended to implement it as described here:

You must call getInvitation() in every activity that might be launched by the link, even though the link might be available from the intent using getIntent().getData(). Calling getInvitation() retrieves the link and clears that data so it is only processed once by your app.

random
  • 10,238
  • 8
  • 57
  • 101
  • Are **link retrieval** and **data clearing** the only functionality of `getInvitation()`? I have already managed this on my own. – Daniele B Oct 07 '16 at 13:39
  • That's what the link mentions. If you could handle it yourself then I think you're good to go and ignore this function. – random Oct 07 '16 at 16:12
0

I don't believe you have to use getInvitation(), Personally I just override 'onNewIntent' like so:

@Override
protected void onNewIntent(final Intent intent) {
  super.onNewIntent(intent);
  if (intent.getAction().equals("android.intent.action.VIEW")) {
    new Handler().postDelayed(new Runnable() {
      @Override
      public void run() {
        handleItemId(getIdFromIntent(intent));
      }
    }, 50);
  }
}

I set up a handler with postDelayed to allow the activity to set-up. You don't have to do that.

You must have an intent filter set up like this

    <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <data android:host="yourwebsite.com" android:scheme="http"/>
        <data android:host="yourwebsite.com" android:scheme="https"/>
        <data android:host="anything" android:scheme="yourappname"/>
    </intent-filter>

Then the dynamic url https://*****.app.goo.gl/?link=http://yourwebsite.com&al=yourappname://anything/method&apn=com.yourwebsite.yourappname should open your website on desktop iOS etc, and the app or playstore on android.

To receive deep links from google searches that covert from links on your website to fragments in your app you must define them. My handleItemId and getIdFromIntent methods are defined as follows.

public boolean handleItemId(int id) {
  if (id == R.id.nav_home) {
    fragment = new FragmentHome();

  } else if (id == R.id.nav_favorites) {
    fragment = new FragmentFavoritesPager();

  } else if (id == R.id.nav_contact) {
    Intent intent = new Intent(Intent.ACTION_SENDTO);
    intent.setData(Uri.parse("mailto:RateMyASVAB@gmail.com")); // only email apps should handle this
    if (intent.resolveActivity(getPackageManager()) != null) {
      startActivity(intent);
    } else {
      Toast.makeText(this, "No email app is installed", Toast.LENGTH_LONG).show();
    }
    return false;

  } else if (id == R.id.nav_settings) {
    fragment = new FragmentSettings();

  } else {
    return false;
  }
  new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
      getSupportFragmentManager()
          .beginTransaction()
          .setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out)
          .replace(R.id.content_main, fragment)
          .commitAllowingStateLoss();
    }
  },400);
  return true;
}

And getIdFromIntent

private int getIdFromIntent(Intent intent) {
  int id = R.id.nav_home;
  if (intent.getData() != null) {
    List<String> segments = intent.getData().getPathSegments();
    if (segments.size() > 0) {
      switch (segments.get(0)) {
        case "favorites":
          id = R.id.nav_favorites;
          break;
        case "contact":
          id = R.id.nav_contact;
          break;
        case "settings":
          id = R.id.nav_settings;
          break;
      }
    }
  }
  return id;
}