2017-10-16 129 views
0

我是firebase的新手。我使用的是舊版本的代碼,其中我放入的參數至少爲url工作。但現在當點擊鏈接時,瀏覽器打開,並且找不到請求url的400錯誤。它只有在動態鏈接的作品在Firebase動態鏈接中傳遞參數不起作用

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(); 
       } 
       Log.v("ENCODED URI: ", encodedUri); 
       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")); 
      } 
     }); 


    } 

主要活動的OnCreate代碼:

setContentView(R.layout.activity_main); 
     button = (Button) findViewById(R.id.button); 



     FirebaseDynamicLinks.getInstance().getDynamicLink(getIntent()) 
       .addOnSuccessListener(this, new OnSuccessListener<PendingDynamicLinkData>() { 
        @Override 
        public void onSuccess(PendingDynamicLinkData data) { 
         if (data == null) { 
          Log.d("NULL DATA ", "getInvitation: no data"); 
          return; 
         } 

         // Get the deep link 
         Uri deepLink = data.getLink(); 
         String requestId2 = deepLink.getQueryParameter("extra1"); 


         // Handle the deep link 
         // [START_EXCLUDE] 
         Log.d("DEEP LINK URL ", "deepLink:" + deepLink); 
         if (deepLink != null) { 

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

             startActivity(intent); 
            } 
         } 
         // [END_EXCLUDE] 
        } 
       }) 
       .addOnFailureListener(this, new OnFailureListener() { 
        @Override 
        public void onFailure(@NonNull Exception e) { 
         Log.w("onFailure: ", "getDynamicLink:onFailure", e); 
        } 
       }); 

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> 

我如何把參數,然後把它在的onSuccess ?謝謝

+0

嘗試在控制檯中或通過代碼創建與構建器的鏈接:https://firebase.google.com/docs/dynamic-links/android/create –

+0

@DimaRostopira這就是說,在Android中,參數只是最低應用版本和後備網址。但我想添加一個帶有字符串值的鍵,以便在應用程序打開時使用它 – L3G3NDj

+0

是否需要使用參數?你不能像'https:// example.com/value1/value2'那樣使用它嗎? –

回答

1

我最終通過試錯得出的問題是代碼的編碼部分。當我刪除了:

encodedUri = URLEncoder.encode(APP_URI.toString(), "UTF-8"); 

一部分,剛剛通過APP_URI的深層鏈接就像

Uri deepLink = Uri.parse("https://eh62u.app.goo.gl/y6N7/?link="+APP_URI); 

甚至使用Builder作爲構建鏈接:

Uri.Builder URLbuilder = new Uri.Builder() 
       .scheme("https") 
       .authority(Constants.DOMAIN) 
       .path("/") 
       .appendQueryParameter("link", getBaseUri(value)) 
       .appendQueryParameter("apn", context.getPackageName()); 

它的工作。沒問題。檢索參數是通常的。