2017-10-13 64 views
0

自昨天起,我一直致力于Firebase动态链接。如果我更改意图过滤器放置,则找不到深层链接

这是我的主要活动:

private GoogleApiClient mGoogleApiClient; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     button = (Button) findViewById(R.id.button); 


     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .enableAutoManage(this, this) 
       .addApi(AppInvite.API) 
       .build(); 


     boolean autoLaunchDeepLink = true; 
     AppInvite.AppInviteApi.getInvitation(mGoogleApiClient, this, autoLaunchDeepLink) 
       .setResultCallback(
         new ResultCallback<AppInviteInvitationResult>() { 
          @Override 
          public void onResult(@NonNull AppInviteInvitationResult result) { 
           if (result.getStatus().isSuccess()) { 





            try { 
             deepLink = URLDecoder.decode(deepLink, "UTF-8"); 
            } catch (UnsupportedEncodingException e) { 
             e.printStackTrace(); 
            } 
            Uri uri = Uri.parse(deepLink); 
            // String requestId = uri.getQueryParameter("requestID"); 
            String requestId2 = uri.getQueryParameter("extra1"); 
            Log.v("EXTRA PARAMETER ",requestId2); 

            if(requestId2 == "value") { 
             intent = new Intent(getApplicationContext(), Main2Activity.class); 

             startActivity(intent); 
            } 
            // ... 
           } else { 
            Log.d("string ", "getInvitation: no deep link found."); 

           } 
          } 
         }); 


     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 


       Uri BASE_URI = Uri.parse("http://example.com/"); 

       Uri APP_URI = BASE_URI.buildUpon(). 
         appendQueryParameter("extra1", "value").build(); 


       String encodedUri = null; 
       try { 
        encodedUri = URLEncoder.encode(APP_URI.toString(), "UTF-8"); 
       } catch (UnsupportedEncodingException e) { 
        e.printStackTrace(); 
       } 
       Uri deepLink = Uri.parse("https://eh62u.app.goo.gl/y6N7/?link="+encodedUri); 
       Intent intent = new Intent(Intent.ACTION_SEND); 
       intent.setType("text/plain"); 
       intent.putExtra(Intent.EXTRA_EMAIL, ""); 
       intent.putExtra(Intent.EXTRA_SUBJECT, "GET TICKETS"); 
       intent.putExtra(Intent.EXTRA_TEXT, "Click here to get the booked tickets: " + deepLink); 

       startActivity(Intent.createChooser(intent, "Send Email")); 
      } 
     }); 
    } 


    @Override 
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {} 

    } 

我想使用意图开始Main2Activity被点击的深层链接时。 Android清单:

<application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:roundIcon="@mipmap/ic_launcher_round" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 

      <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="example.com" 
        android:scheme="https"/> 
      </intent-filter> 



     </activity> 
     <activity android:name=".Main2Activity"> 

     </activity> 
    </application> 

现在,如果我把深层链接意图过滤器在Main2Activity,当我点击链接,直接打开Main2Activity并没有进入主要活动和OnResult不叫。但是现在,当我把意向过滤器放在主要活动中时,它说没有发现深层链接。

+0

Main2Activity类绝对没有什么 – L3G3NDj

回答

0

我认为你的代码是旧版本。新版本使用Firebase动态链接更简单。该文档可以在here

读入您的主要活动,也许在的onCreate把这个代码:

FirebaseDynamicLinks.getInstance() 
      .getDynamicLink(getIntent()) 
      .addOnSuccessListener(this, new OnSuccessListener<PendingDynamicLinkData>() { 
       @Override 
       public void onSuccess(PendingDynamicLinkData pendingDynamicLinkData) { 
        // Get deep link from result (may be null if no link is found) 
        Uri deepLinkUri = null; 
        if (pendingDynamicLinkData != null) { 
         deepLinkUri = pendingDynamicLinkData.getLink(); 
        } 
        if(deepLinkUri != null){ 
         // String requestId = deepLinkUri.getQueryParameter("requestID"); 
         String requestId2 = deepLinkUri.getQueryParameter("extra1"); 
         Log.v("EXTRA PARAMETER ",requestId2); 

         if(requestId2 == "value") { 
         intent = new Intent(getApplicationContext(), Main2Activity.class); 

         startActivity(intent); 
         } 
         // ... 
        } else { 
         Log.d("string ", "getDynamicLink: no deep link found."); 
        } 
      }) 
      .addOnFailureListener(this, new OnFailureListener() { 
       @Override 
       public void onFailure(@NonNull Exception e) { 
        Log.d("string ", "getDynamicLink:onFailure", e); 
       } 
      }); 

更多的教程可以在quickstart here

+0

我从Firebase助手处获得了我的代码。即使代码已过时,它应该已经工作了,不是吗? – L3G3NDj

+0

旧版本与它有什么关系? – L3G3NDj

+0

Firebase团队正在竭尽全力让开发人员的工作变得更轻松。为什么用旧版本浪费精力?此外,如果我没有错误地使用谷歌API,我们需要在谷歌云端控制台启用它,然后创建一个API密钥等。 – faruk

0

读取你应该处理deeplink内活动的onNewIntent为好:

@Override 
public void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(getLayoutId()); 
    receiveDeeplinkIfHave(); // detect your deeplink here 
} 
@Override 
protected void onNewIntent(final Intent intent) { 
     super.onNewIntent(intent); 
     receiveDeeplinkIfHave(); // detect your deeplink here 
}