คู่มือการรวมช่องทาง EngageLab
泰语
              
              # คู่มือการรวมช่องทาง Engagelab
## รับข้อมูลแอปพลิเคชัน
สร้างแอปพลิเคชันในคอนโซล หลังจากสร้างแอปพลิเคชันเสร็จแล้ว จะมีการสร้าง AppKey โดยอัตโนมัติเพื่อระบุแอปพลิเคชันนี้ สำหรับข้อมูลเพิ่มเติม โปรดดูที่ [การตั้งค่าแอปพลิเคชัน](/docs/app-push/console/basic-settings/app-info)
## นำเข้าไลบรารี
- วิธีการแบบแมนนวล  
            
            
              
              นำเข้า mt-sdk-x.x.x.aar จากโฟลเดอร์ libs ไปยังไดเรกทอรี libs
นำเข้า th-xxx-x.x.x.aar จากโฟลเดอร์ libs ไปยังไดเรกทอรี libs
            
            
              
              
- วิธีการอัตโนมัติ
            
            dependencies { // จำเป็น: แพ็คเกจหลัก implementation 'com.engagelab:engagelab:4.3.9' // ตัวอย่างเวอร์ชัน 4.3.9 // ตัวเลือก: ผู้ผลิต Google implementation 'com.engagelab.plugin:google:4.3.9' // ตัวอย่างเวอร์ชัน 4.3.9 // ตัวเลือก: ผู้ผลิต Honor implementation 'com.engagelab.plugin:honor:4.3.9' // ตัวอย่างเวอร์ชัน 4.3.9 implementation 'com.engagelab.plugin:honor_th_push:4.3.9' // ตัวอย่างเวอร์ชัน 4.3.9 // ตัวเลือก: ผู้ผลิต Huawei implementation 'com.engagelab.plugin:huawei:4.3.9' // ตัวอย่างเวอร์ชัน 4.3.9 // ตัวเลือก: ผู้ผลิต Xiaomi, เวอร์ชันภาษาจีน implementation 'com.engagelab.plugin:mi:4.3.9' // ตัวอย่างเวอร์ชัน 4.3.9 // ตัวเลือก: ผู้ผลิต Meizu implementation 'com.engagelab.plugin:meizu:4.3.9' // ตัวอย่างเวอร์ชัน 4.3.9 // ตัวเลือก: ผู้ผลิต Oppo implementation 'com.engagelab.plugin:oppo:4.3.9' // ตัวอย่างเวอร์ชัน 4.3.9 implementation 'com.engagelab.plugin:oppo_th_push:4.3.9' // ตัวอย่างเวอร์ชัน 4.3.9 // ตัวเลือก: ผู้ผลิต Vivo implementation 'com.engagelab.plugin:vivo:4.3.9' // ตัวอย่างเวอร์ชัน 4.3.9 // ตัวเลือก: ฟังก์ชันเสียงประกาศ implementation 'com.engagelab.plugin:oth_sound:4.3.9' // ตัวอย่างเวอร์ชัน 4.3.9 }
              
              
<a name="3ac7d74a"></a>
## 2. กำหนดค่า build.gradle
            
            plugins { id 'com.android.application' }
android { ...
              
              defaultConfig {
    // ชื่อแพ็คเกจแอปพลิเคชัน ต้องตรงกับในคอนโซล
    applicationId "com.engagelab.app"
    ...
            
    manifestPlaceholders = [
            // AppKey ต้องตรงกับในคอนโซล และตรงกับ packageName
            ENGAGELAB_PRIVATES_APPKEY : "YourAppKey",
            // ช่องทาง Engagelab สำหรับการวิเคราะห์ช่องทาง
            ENGAGELAB_PRIVATES_CHANNEL: "developer",
            // กระบวนการ Engagelab ที่ SDK ของ Engagelab ทำงานอยู่ หมายเหตุ: ต้องเริ่มต้นด้วย :
            ENGAGELAB_PRIVATES_PROCESS: ":remote",
            
    ]
}
            
            }
dependencies { implementation fileTree(include: ['.jar', '.aar'], dir: 'libs') }
              
              
## สร้างคอมโพเนนต์ที่จำเป็น
            
            package com.engagelab.app.component;
import com.engagelab.privates.common.component.MTCommonService;
/**
- นักพัฒนาสามารถสืบทอด MTCommonService เพื่อขยายอายุการใช้งานของการเชื่อมต่อแบบยาว
- สามารถเว้นว่างได้ */ public class UserService extends MTCommonService {
}
              
              
            
            package com.engagelab.app.component;
import android.content.Context; import android.widget.Toast;
import java.util.Arrays;
import com.engagelab.app.common.ExampleGlobal; import com.engagelab.app.listener.StatusObserver; import com.engagelab.app.log.ExampleLogger; import com.engagelab.privates.common.component.MTCommonReceiver; import com.engagelab.privates.core.api.MTCorePrivatesApi; import com.engagelab.privates.core.api.WakeMessage; import com.engagelab.privates.push.api.CustomMessage; import com.engagelab.privates.push.api.MobileNumberMessage; import com.engagelab.privates.push.api.NotificationMessage; import com.engagelab.privates.push.api.PlatformTokenMessage;
/**
- นักพัฒนาสามารถสืบทอด MTCommonReceiver เพื่อรับการเรียกกลับของวิธี SDK 
- การเรียกกลับทั้งหมดจะอยู่ในเธรดหลัก */ public class UserReceiver extends MTCommonReceiver { - private static final String TAG = "UserReceiver"; - /** - การเรียกกลับสถานะการเปิด/ปิดการแจ้งเตือน
- @param context ไม่เป็น null
- @param enable สถานะการเปิด/ปิดการแจ้งเตือน true คือเปิด false คือปิด */ @Override public void onNotificationStatus(Context context, boolean enable) { ExampleLogger.i(TAG, "onNotificationStatus:" + enable); Toast.makeText(context.getApplicationContext(), "onNotificationStatus " + enable, Toast.LENGTH_SHORT).show(); ExampleGlobal.isNotificationEnable = enable; if (StatusObserver.getInstance().getListener() != null) { StatusObserver.getInstance().getListener().onNotificationStatus(enable); } }
 - /** - การเรียกกลับสถานะการเชื่อมต่อแบบยาว
- @param context ไม่เป็น null
- @param enable สถานะการเชื่อมต่อ true คือเชื่อมต่อ false คือไม่เชื่อมต่อ */ @Override public void onConnectStatus(Context context, boolean enable) { ExampleLogger.i(TAG, "onConnectState:" + enable); Toast.makeText(context.getApplicationContext(), "onConnectStatus " + enable, Toast.LENGTH_SHORT).show(); ExampleGlobal.isConnectEnable = enable; if (StatusObserver.getInstance().getListener() != null) { StatusObserver.getInstance().getListener().onConnectStatus(enable); } // เมื่อการเชื่อมต่อแบบยาวถูกสร้างขึ้น ให้รับ registrationId if (enable) { String registrationId = MTCorePrivatesApi.getRegistrationId(context); ExampleLogger.i(TAG, "registrationId:" + registrationId); } }
 - /** - การเรียกกลับเมื่อมีข้อความแจ้งเตือนมาถึง
- @param context ไม่เป็น null
- @param notificationMessage ข้อความแจ้งเตือน */ @Override public void onNotificationArrived(Context context, NotificationMessage notificationMessage) { ExampleLogger.i(TAG, "onNotificationArrived:" + notificationMessage.toString()); }
 - /** - การเรียกกลับเมื่อไม่แสดงข้อความแจ้งเตือนในโหมดหน้าจอหน้า
- @param context ไม่เป็น null
- @param notificationMessage ข้อความแจ้งเตือน */ @Override public void onNotificationUnShow(Context context, NotificationMessage notificationMessage) { ExampleLogger.i(TAG, "onNotificationUnShow:" + notificationMessage.toString()); }
 - /** - การเรียกกลับเมื่อมีการคลิกข้อความแจ้งเตือน
- @param context ไม่เป็น null
- @param notificationMessage ข้อความแจ้งเตือน */ @Override public void onNotificationClicked(Context context, NotificationMessage notificationMessage) { ExampleLogger.i(TAG, "onNotificationClicked:" + notificationMessage.toString()); }
 - /** - การเรียกกลับเมื่อมีการลบข้อความแจ้งเตือน
- @param context ไม่เป็น null
- @param notificationMessage ข้อความแจ้งเตือน */ @Override public void onNotificationDeleted(Context context, NotificationMessage notificationMessage) { ExampleLogger.i(TAG, "onNotificationDeleted:" + notificationMessage.toString()); }
 - /** - การเรียกกลับข้อความที่กำหนดเอง
- @param context ไม่เป็น null
- @param customMessage ข้อความที่กำหนดเอง */ @Override public void onCustomMessage(Context context, CustomMessage customMessage) { ExampleLogger.i(TAG, "onCustomMessage:" + customMessage.toString()); }
 - /** - การเรียกกลับข้อความ token ของผู้ผลิต
- @param context ไม่เป็น null
- @param platformTokenMessage ข้อความ token ของผู้ผลิต */ @Override public void onPlatformToken(Context context, PlatformTokenMessage platformTokenMessage) { ExampleLogger.i(TAG, "onPlatformToken:" + platformTokenMessage.toString()); }
 
}
4. กำหนดค่า AndroidManifest.xml
              
              <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.engagelab.app">
        
    <!-- จำเป็น: สิทธิ์อินเทอร์เน็ต -->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <!-- ตัวเลือก: การสั่นสำหรับการแจ้งเตือน -->
    <uses-permission android:name="android.permission.VIBRATE" /> 
        
    <application
        android:name="com.engagelab.app.MainApplication"
        android:allowBackup="false"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.MT"
        android:usesCleartextTraffic="true"
        tools:targetApi="m"> <!-- หากใช้คำขอ HTTP ให้อนุญาต HTTP -->
                .....................................................................................................
                
        <!-- เนื่องจากฟีเจอร์การเชื่อมต่อระยะยาวของคลาสนี้ การประหยัดพลังงานโดยผู้ผลิตอาจปิดบริการที่มีชื่อแพ็กเกจที่มี "Engagelab" โดยตรง -->
        <!-- ดังนั้นนักพัฒนาจำเป็นต้องขยาย com.engagelab.app.component.MTCommonService เพื่อปรับปรุงอัตราการอยู่รอดของการเชื่อมต่อระยะยาวและเพิ่มอัตราการมาถึงของข้อความ -->
        <service
            android:name="com.engagelab.app.component.UserService"
            android:exported="false"
            android:process="${ENGAGELAB_PRIVATES_PROCESS}">
            <intent-filter>
                <action android:name="com.engagelab.privates.intent.USER_SERVICE" />
            </intent-filter>
        </service>
        <!-- ใช้สำหรับรับการเรียกกลับทางธุรกิจของ Engagelab รวมถึงสถานะการเชื่อมต่อระยะยาว/สถานะสวิตช์การแจ้งเตือน/การมาถึงของข้อความแจ้งเตือน/การคลิกข้อความแจ้งเตือน/การลบข้อความแจ้งเตือน/ข้อความที่กำหนดเอง/การเรียกกลับโทเค็นของผู้ให้บริการ -->
        <!-- ไม่จำเป็นต้องกำหนดค่ากระบวนการย่อย การเรียกกลับนี้อยู่ในกระบวนการหลักเพื่อความสะดวกในการดำเนินการทางธุรกิจ -->
        <receiver
            android:name="com.engagelab.app.component.UserReceiver"
            android:exported="false">
            <intent-filter>
                <action android:name="com.engagelab.privates.intent.USER_RECEIVER" />
            </intent-filter>
        </receiver>
    </application>
</manifest>
            
            5. การกำหนดค่าสภาพแวดล้อม
              
              package com.engagelab.app;
import android.app.Application;
import com.engagelab.privates.core.api.Address;
import com.engagelab.privates.core.api.MTCorePrivatesApi;
import com.engagelab.privates.push.api.MTPushPrivatesApi;
/**
 * ใช้เพื่อแสดงการกำหนดค่าของ ENGAGELAB-sdk
 */
public class MainApplication extends Application {
    private static final String TAG = "MainApplication";
    @Override
    public void onCreate() {
        super.onCreate();
        // การกำหนดค่าต้องทำใน application.onCreate ไม่จำเป็นต้องตรวจสอบกระบวนการ SDK มีการตรวจสอบภายใน
        MTCorePrivatesApi.configDebugMode(this, true);
        // สำหรับเวอร์ชันก่อน tag: V3.5.4-newportal-20210823-gamma.57 นักพัฒนาฝั่งหน้าต้องเรียกวิธีนี้ มิฉะนั้นจะมีปัญหาเกี่ยวกับการคลิกแจ้งเตือนเพื่อข้ามไป
        // MTPushPrivatesApi.configOldPushVersion(context.getApplicationContext());
        // เริ่มต้นการแจ้งเตือน
        // MTPushPrivatesApi.init(this);
    }
}
            
            6. สร้างคอมโพเนนต์การคลิกแจ้งเตือนเพื่อข้ามไป
              
              package com.engagelab.app.component;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import com.engagelab.app.R;
import com.engagelab.app.log.ExampleLogger;
import com.engagelab.privates.push.api.MTPushPrivatesApi;
import com.engagelab.privates.push.api.NotificationMessage;
import com.engagelab.privates.push.constants.MTPushConstants;
/**
 * ใช้เพื่อแสดงการคลิกแจ้งเตือนเพื่อข้ามไปยัง activity
 *
 * ตรวจสอบให้แน่ใจว่าไม่ได้เรียก MTPushPrivatesApi.configOldPushVersion(Context) มิฉะนั้นการคลิกแจ้งเตือนจะไม่ไปที่หน้านี้
 * <p>
 * ไม่จำเป็นต้องเรียก MTPushPrivatesApi.reportNotificationOpened(Context, String, byte, String); SDK จัดการภายใน
 */
public class UserActivity400 extends Activity {
    private static final String TAG = "UserActivity400";
    private TextView tvMessage;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intent);
        tvMessage = findViewById(R.id.tv_message);
        onIntent(getIntent());
    }
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        onIntent(intent);
    }
    private void onIntent(Intent intent) {
        try {
            Toast.makeText(this, TAG, Toast.LENGTH_SHORT).show();
            if (intent == null) {
                return;
            }
            // เริ่มต้นจากเวอร์ชัน 3.4.0 ไม่ได้ใช้ออบเจ็กต์อีกต่อไป ใช้ข้อมูล JSON แทน
            String notificationMessage = intent.getStringExtra("message_json");
            if (notificationMessage == null) {
                return;
            }
            ExampleLogger.d(TAG, "notificationMessage:" + notificationMessage.toString());
            tvMessage.setText(notificationMessage.toString());
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }
        
}
            
            7. กำหนดค่า AndroidManifest.xml สำหรับการคลิกแจ้งเตือน
              
              <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.engagelab.app">
    <!-- จำเป็น: สิทธิ์อินเทอร์เน็ต -->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <!-- ตัวเลือก: การสั่นสำหรับการแจ้งเตือน -->
    <uses-permission android:name="android.permission.VIBRATE" />
    <application
        android:name="com.engagelab.app.MainApplication"
        android:allowBackup="false"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.MT"
        android:usesCleartextTraffic="true"
        tools:targetApi="m"> <!-- หากใช้คำขอ HTTP ให้อนุญาต HTTP -->
        ..............................................................
        <!-- ใช้เพื่อแสดงการคลิกแจ้งเตือนเพื่อข้ามไป -->
        <activity
            android:name="com.engagelab.app.component.UserActivity400"
            android:exported="false"
            android:launchMode="singleTask" />
        <!-- ใช้เพื่อแสดงการคลิกแจ้งเตือนด้วยลิงก์ลึก -->
<!--        <activity-->
<!--            android:name="com.engagelab.app.component.UserActivity400"-->
<!--            android:exported="false" >-->
<!--            <intent-filter>-->
<!--                <data android:scheme="YourScheme"/>-->
<!--                <data android:host="YourHost"/>-->
<!--                <action android:name="android.intent.action.VIEW" />-->
<!--                <category android:name="android.intent.category.DEFAULT" />-->
<!--                <category android:name="android.intent.category.BROWSABLE" />-->
<!--            </intent-filter>-->
<!--        </activity>-->
            
        <!-- การตรวจสอบการถ่ายโอนในพื้นที่ที่อนุญาต: เริ่มต้นจาก MTPush 4.2.3 หลังจากกำหนดค่า ENGAGELAB_PRIVATES_TRANSFER SDK จะทำการตรวจสอบในพื้นที่ หากเปิดใช้งานการตรวจสอบในพื้นที่ โปรดกำหนดค่า Activity เป้าหมายที่จะข้ามไปที่นี่ หากมี Activity เป้าหมายหลายรายการ โปรดแยกด้วย / -->
        <meta-data
            android:name="ENGAGELAB_PRIVATES_TRANSFER"
            android:value="com.engagelab.app.component.UserActivity400" />
                
        <!-- ความต้องการของ Google push: ลบออกหากไม่ต้องการช่องทาง Google -->
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/mtpush_notification_icon" />
        <!-- ความต้องการของ Huawei push: ลบออกหากไม่ต้องการช่องทาง Huawei -->
        <meta-data
            android:name="com.huawei.messaging.default_notification_icon"
            android:resource="@drawable/mtpush_notification_icon" />
    </application>
</manifest>
            
            







