简体   繁体   English

Firebase Android:处理深层链接

[英]Firebase Android: handle deep links

On the Firebase documentation : 在Firebase 文档中

it says: 它说:

To receive the deep link, call the getInvitation method 要接收深层链接,请调用getInvitation方法

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? 那么,是否真的需要调用getInvitation方法? what is it exactly for? 究竟是为了什么?

getInvitation() is to handle the deep link intent. getInvitation()用于处理深层链接意图。 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(). 您必须在链接可能启动的每个活动中调用getInvitation(),即使使用getIntent()。getData()可以从intent中获得该链接。 Calling getInvitation() retrieves the link and clears that data so it is only processed once by your app. 调用getInvitation()会检索链接并清除该数据,因此只能由您的应用处理一次。

I don't believe you have to use getInvitation(), Personally I just override 'onNewIntent' like so: 我不相信你必须使用getInvitation(),我个人只是覆盖'onNewIntent',如下所示:

@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. 我使用postDelayed设置了一个处理程序,以允许设置活动。 You don't have to do that. 你不必那样做。

You must have an intent filter set up like this 您必须设置这样的intent过滤器

    <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. 然后动态网址https://*****.app.goo.gl/?link=http://yourwebsite.com&al=yourappname://anything/method&apn=com.yourwebsite.yourappname应在桌面上打开您的网站iOS等,以及Android上的应用程序或Playstore。

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. 我的handleItemId和getIdFromIntent方法定义如下。

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 并且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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM