#####[1. Enable Deep Links](#1-enable-deep-links) #####[2. Setup Facebook SDK](#2-setup-facebook-sdk-1) #####[3. Deferred Deep Linking](#3-deferred-deep-linking-quan-trọng)
## 1. Enable Deep Links
#####Add Intent Filters for Your Deep Links
Trong AndroidManifest.xml
Thêm intent filter
vào Activity như sau:
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- Enable Deep Links -->
<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:scheme="example"
android:host="gizmos" />
</intent-filter>
</activity>
#####Read Data from Incoming Intents
Lấy data từ Intent
trong Activity
tương ứng:
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = getIntent();
if (intent != null) {
String action = intent.getAction();
Uri data = intent.getData();
}
}
#####Test
Bật thiết bị ở chế độ Debug, kết nối thiết bị với máy tính và dùng adb
command sau:
$ adb shell am start
-W -a android.intent.action.VIEW
-d <URI> <PACKAGE>
Theo như ví dụ trên:
$ adb shell am start -W -a android.intent.action.VIEW -d "example://gizmos" com.deelplink.testing
- Tạo Facebook App từ developers.facebook.com
- On Facebook App Manager pages, select Settings -> + Add Platform -> Android to add Android Settings
- Edit Google Play Package Name and Class Name
Làm theo hướng dẫn sau Facebook SDK Android
- Thêm
permission
cho kết nối INTERNET:
<uses-permission android:name="android.permission.INTERNET"/>
- Thêm
meta-data
vàoapplication
:
<application android:label="@string/app_name" ...>
...
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
...
</application>
- Thêm
facebook_app_id
vàostring.xml
:
<string name="app_id">FACEBOOK_APP_ID</string>
Deferred deep linking cho phép bạn truyền thông điệp tới ứng dụng sau khi được cài đặt từ app store.
==> Với app mới chưa được cài đặt, bắt buộc phải dùng Deferred Deep Linking.
Lấy AppLinkData trong OnCreate()
của Activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Init Facebook SDK
FacebookSdk.sdkInitialize(getApplicationContext());
// Read Deferred Deep Link
AppLinkData.fetchDeferredAppLinkData(MainActivity.this,
new AppLinkData.CompletionHandler() {
@Override
public void onDeferredAppLinkDataFetched(AppLinkData appLinkData) {
if (appLinkData != null) {
Uri targetUri = appLinkData.getTargetUri(); // Example: app360://deeplink?refcode=canhdx
if (schema.equals("app360")) {
String refcode = targetUri.getQueryParameter("refcode");
Log.d("MyTag", "Refcode from FB: " + refcode);
App360SDK.setSubChannel(MainActivity.this, refcode);
}
}
}
}
);
initApp360("your-user-id");
}
private void initApp360(final String userId) {
App360SDK.initialize("appID", "appSecret", getApplicationContext(), new InitListener() {
@Override
public void onSuccess() {
SessionService.Session session = SessionManager.getCurrentSession();
if (session == null) { // no cached valid session
SessionManager.createSession(userId, new SessionManager.SessionCallback(){
@Override
public void onSuccess() {
Log.d("MyTag", "Create session successful");
Log.d("MyTag", "Current session: " + SessionManager.getCurrentSession());
Log.d("MyTag", "Scoped id: " + ScopedUser.getCurrentUser().getId() + ", Come from channel: " + ScopedUser.getCurrentUser().getChannel());
}
@Override
public void onFailure(Exception e) {
Log.e("MyTag", "Create session fail");
e.printStackTrace();
}
});
} else {
Log.d("MyTag", "Current session: " + session);
Log.d("MyTag", "Scoped id: " + ScopedUser.getCurrentUser().getId() + " Come from channel: " + ScopedUser.getCurrentUser().getChannel());
}
}
@Override
public void onFailure(Exception e) {
Log.e("MyTag", "Initialization error", e);
}
});
}
#####Test Deferred Deep Link
- Go to App Ads Helper
- Chọn App => Submit
- Chọn Test Deep Link
- Trong Test Deep Link form, nhập Deep Link. Ví dụ:
app360://deeplink?refcode=canhdx
- Đánh dấu Send Deferred checkbox và Send to Android
Deep Link đã được gửi tới thiết bị của bạn, bạn cần gỡ app sau đó cài lại để nhận được Deep Link Data.