# คู่มือการผสานรวม Unity SDK เบื้องต้น

คู่มือนี้จะแนะนำขั้นตอนการผสานรวม Pushwoosh Unity SDK เข้ากับแอปพลิเคชันของคุณ

## ข้อกำหนดเบื้องต้น

<Aside type="note" title="ข้อกำหนด">
 -  [บัญชี Pushwoosh](https://sso.pushwoosh.com/login)
 -  [โปรเจกต์ Pushwoosh](/th/product/first-steps/start-with-your-project/create-your-project) ที่ตั้งค่าไว้ในบัญชีของคุณ
 - Unity 2021.3 หรือใหม่กว่า
 - **สำหรับ iOS:**
    - แพลตฟอร์ม iOS ที่กำหนดค่าเพื่อส่ง push notifications เราแนะนำให้ใช้ [Token-Based Authentication](/th/developer/first-steps/connect-messaging-services/ios-configuration/ios-token-based-configuration/) ซึ่งเป็นวิธีที่ง่ายที่สุด
    - ตั้งค่า Gateway เป็น `Sandbox` เพื่อส่ง push ไปยัง simulator
 - **สำหรับ Android:**
    -  [แพลตฟอร์ม Android ที่กำหนดค่าแล้ว](/th/developer/first-steps/connect-messaging-services/android-configuration/android-firebase-configuration)
    - `project number` (หรือที่เรียกว่า Sender ID), ไฟล์ `google-services.json`, และ `package name` จากโปรเจกต์ Firebase ของคุณ
    - โปรเจกต์ Firebase ที่เชื่อมต่อกับแอปพลิเคชัน Android ของคุณ ทำตาม [คู่มือการตั้งค่า Firebase](https://firebase.google.com/docs/android/setup#manually_add_firebase) หากจำเป็น
 - `Pushwoosh Application Code` และ [Pushwoosh Device API Token](/th/developer/api-reference/api-access-token/#device-api-token) ของคุณจาก Pushwoosh Control Panel
</Aside>

## ขั้นตอนการผสานรวม

### 1. เพิ่ม Pushwoosh Unity SDK

<Tabs>
  <TabItem label="UPM ผ่าน Scoped Registry (แนะนำ)">

เพิ่มข้อมูลต่อไปนี้ลงใน `Packages/manifest.json` ของคุณ:

```json title="Packages/manifest.json"
{
  "dependencies": {
    "com.pushwoosh.unity.core": "6.2.7",
    "com.pushwoosh.unity.android": "6.2.7",
    "com.pushwoosh.unity.ios": "6.2.7"
  },
  "scopedRegistries": [
    {
      "name": "npmjs",
      "url": "https://registry.npmjs.org",
      "scopes": ["com.pushwoosh"]
    }
  ]
}
```

เพิ่มเฉพาะแพ็คเกจแพลตฟอร์มที่คุณต้องการ ตัวอย่างเช่น ละเว้น `com.pushwoosh.unity.android` หากคุณกำหนดเป้าหมายเป็น iOS เท่านั้น

  </TabItem>
  <TabItem label="UPM ผ่าน Git URL">

ใน Unity ไปที่ **Window > Package Manager > + > Add package from git URL** และเพิ่ม URL ต่อไปนี้ทีละรายการ:

```
https://github.com/Pushwoosh/pushwoosh-unity.git?path=com.pushwoosh.unity.core
https://github.com/Pushwoosh/pushwoosh-unity.git?path=com.pushwoosh.unity.android
https://github.com/Pushwoosh/pushwoosh-unity.git?path=com.pushwoosh.unity.ios
```

  </TabItem>
  <TabItem label=".unitypackage">

ดาวน์โหลด `Pushwoosh.unitypackage` จาก [GitHub Releases](https://github.com/Pushwoosh/pushwoosh-unity/releases) และนำเข้าผ่าน **Assets > Import Package > Custom Package**

  </TabItem>
</Tabs>

### 2. ติดตั้ง External Dependency Manager

SDK จำเป็นต้องใช้ [External Dependency Manager for Unity (EDM4U)](https://github.com/googlesamples/unity-jar-resolver) เพื่อจัดการ dependencies ของ Android และ iOS

เพิ่ม scoped registry ต่อไปนี้ลงใน `Packages/manifest.json` ของคุณ:

```json
{
  "scopedRegistries": [
    {
      "name": "package.openupm.com",
      "url": "https://package.openupm.com",
      "scopes": ["com.google.external-dependency-manager"]
    }
  ]
}
```

จากนั้นเพิ่มแพ็คเกจไปยัง dependencies ของคุณ:

```json
"com.google.external-dependency-manager": "1.2.183"
```

### 3. เริ่มต้นการทำงานของ SDK

สร้างสคริปต์ `PushNotificator.cs` และแนบไปกับ GameObject ใดก็ได้ใน scene:

```csharp title="PushNotificator.cs"
using UnityEngine;
using System.Collections.Generic;

public class PushNotificator : MonoBehaviour
{
    void Start()
    {
        Pushwoosh.ApplicationCode = "XXXXX-XXXXX";
        Pushwoosh.FcmProjectNumber = "XXXXXXXXXXXX";

        Pushwoosh.Instance.OnRegisteredForPushNotifications += (token) => {
            Debug.Log("Push token: " + token);
        };

        Pushwoosh.Instance.OnFailedToRegisteredForPushNotifications += (error) => {
            Debug.Log("Registration failed: " + error);
        };

        Pushwoosh.Instance.RegisterForPushNotifications();
    }
}
```

แทนที่:
- `XXXXX-XXXXX` ด้วย Pushwoosh Application Code ของคุณ
- `XXXXXXXXXXXX` ด้วยหมายเลขโปรเจกต์ Firebase ของคุณ (สำหรับ Android เท่านั้น)

### 4. การตั้งค่าแบบเนทีฟสำหรับ iOS

#### 4.1 Capabilities

หลังจาก build โปรเจกต์ iOS จาก Unity แล้ว ให้เปิดโปรเจกต์ Xcode ที่สร้างขึ้นและเพิ่ม capabilities ต่อไปนี้ใน **Signing & Capabilities**:

- **Push Notifications**
- **Background Modes** โดยเลือก **Remote notifications**

สำหรับ Time Sensitive Notifications (iOS 15+) ให้เพิ่ม capability **Time Sensitive Notifications** ด้วย

#### 4.2 Info.plist

เพิ่ม [Pushwoosh Device API Token](/th/developer/api-reference/api-access-token/#device-api-token) ไปยัง `Info.plist` ของคุณ:

```xml title="Info.plist"
<key>Pushwoosh_API_TOKEN</key>
<string>__PUSHWOOSH_DEVICE_API_TOKEN__</string>
```

#### 4.3 การติดตามการส่งข้อความ

เพิ่ม target Notification Service Extension ไปยังโปรเจกต์ Xcode ของคุณ ซึ่งจำเป็นสำหรับการติดตามการส่งที่แม่นยำและ Rich Media บน iOS

ทำตาม [คู่มือเนทีฟ](/th/developer/pushwoosh-sdk/ios-sdk/setting-up-pushwoosh-ios-sdk/basic-integration-guide/#4-message-delivery-tracking) เพื่อเพิ่ม extension target

### 5. การตั้งค่าแบบเนทีฟสำหรับ Android

#### 5.1 เพิ่มไฟล์กำหนดค่า Firebase

วางไฟล์ `google-services.json` ลงในไดเรกทอรี **Assets** ของโปรเจกต์ Unity ของคุณ

#### 5.2 เพิ่มข้อมูลเมตาของ Pushwoosh

เพิ่ม [Pushwoosh Device API Token](/th/developer/api-reference/api-access-token/#device-api-token) ไปยัง `Assets/Plugins/Android/AndroidManifest.xml` ของคุณภายในแท็ก `<application>`:

```xml title="AndroidManifest.xml"
<meta-data android:name="com.pushwoosh.apitoken" android:value="__YOUR_DEVICE_API_TOKEN__" />
```

<Aside type="caution">
ตรวจสอบให้แน่ใจว่าได้ให้สิทธิ์การเข้าถึงของ token กับแอปที่ถูกต้องใน Pushwoosh Control Panel ของคุณ [เรียนรู้เพิ่มเติม](/th/developer/api-reference/api-access-token/#edit-token)
</Aside>

### 6. รันโปรเจกต์

1. Build และรันโปรเจกต์บนแพลตฟอร์มเป้าหมายของคุณ
2. ให้สิทธิ์สำหรับ push notifications เมื่อมีข้อความแจ้ง
3. ไปที่ Pushwoosh Control Panel และ [ส่ง push notification](/th/product/messaging-channels/push-notifications/send-push-notifications/one-time-push)

## การผสานรวมเพิ่มเติม

ณ จุดนี้ คุณสามารถส่งและรับ push notifications ได้แล้ว ส่วนต่อไปนี้จะครอบคลุมฟังก์ชันการทำงานหลักของ SDK

### ตัวดักจับ event ของ Push notification

SDK มีตัวดักจับ event สองตัวสำหรับจัดการ push notifications:

- `OnPushNotificationsReceived` — ทำงานเมื่อมี push notification เข้ามา
- `OnPushNotificationsOpened` — ทำงานเมื่อผู้ใช้แตะที่การแจ้งเตือน

ตั้งค่าตัวดักจับเหล่านี้ระหว่างการเริ่มต้น SDK:

```csharp title="PushNotificator.cs"
void Start()
{
    Pushwoosh.ApplicationCode = "XXXXX-XXXXX";
    Pushwoosh.FcmProjectNumber = "XXXXXXXXXXXX";

    Pushwoosh.Instance.OnPushNotificationsReceived += (payload) => {
        Debug.Log("Push received: " + payload);
    };

    Pushwoosh.Instance.OnPushNotificationsOpened += (payload) => {
        Debug.Log("Push opened: " + payload);
    };

    Pushwoosh.Instance.RegisterForPushNotifications();
}
```

### การกำหนดค่าผู้ใช้

ปรับแต่ง push notifications ให้เป็นแบบส่วนตัวโดยการระบุผู้ใช้และตั้งค่าคุณสมบัติต่างๆ:

```csharp
// ตั้งค่า User ID สำหรับการติดตามข้ามอุปกรณ์
Pushwoosh.Instance.SetUserId("user-123");

// ตั้งค่าอีเมลผู้ใช้
Pushwoosh.Instance.SetEmail("user@example.com");

// ตั้งค่าผู้ใช้ด้วย ID และอีเมล
Pushwoosh.Instance.SetUser("user-123", new List<string> { "user@example.com" });

// ตั้งค่าภาษาที่ต้องการ
Pushwoosh.Instance.SetLanguage("en");
```

### Tags

Tags คือคู่ของ key-value ที่กำหนดให้กับอุปกรณ์ ช่วยให้สามารถแบ่งกลุ่มผู้ใช้และส่งข้อความแบบกำหนดเป้าหมายได้:

```csharp
// Tag แบบสตริง
Pushwoosh.Instance.SetStringTag("favorite_category", "electronics");

// Tag แบบจำนวนเต็ม
Pushwoosh.Instance.SetIntTag("purchase_count", 5);

// Tag แบบลิสต์
Pushwoosh.Instance.SetListTag("interests", new List<object> { "sports", "music", "tech" });

// ดึง Tag ทั้งหมด
Pushwoosh.Instance.GetTags((tags, error) => {
    if (error != null) {
        Debug.Log("Error: " + error.Message);
        return;
    }
    foreach (var tag in tags) {
        Debug.Log(tag.Key + ": " + tag.Value);
    }
});
```

### Events

ติดตามการกระทำของผู้ใช้เพื่อวิเคราะห์พฤติกรรมและกระตุ้นการส่งข้อความอัตโนมัติ:

```csharp
// ติดตาม event การล็อกอิน
Pushwoosh.Instance.PostEvent("login", new Dictionary<string, object> {
    { "username", "user-123" },
    { "login_type", "email" }
});

// ติดตาม event การซื้อ
Pushwoosh.Instance.PostEvent("purchase", new Dictionary<string, object> {
    { "product_id", "SKU-001" },
    { "price", 29.99 },
    { "currency", "USD" }
});
```

### การตั้งค่าการสื่อสาร

อนุญาตให้ผู้ใช้เลือกรับหรือไม่รับ push notifications ผ่านโปรแกรมได้:

```csharp
// เปิดใช้งานการสื่อสาร
Pushwoosh.Instance.SetCommunicationEnabled(true);

// ปิดใช้งานการสื่อสาร
Pushwoosh.Instance.SetCommunicationEnabled(false);

// ตรวจสอบสถานะปัจจุบัน
bool isEnabled = Pushwoosh.Instance.IsCommunicationEnabled();
```

### การจัดการ Badge

ควบคุมหมายเลข badge ของแอปบนแพลตฟอร์มที่รองรับ:

```csharp
// ตั้งค่า badge เป็นหมายเลขที่กำหนด
Pushwoosh.Instance.SetBadgeNumber(3);

// เพิ่มค่า badge
Pushwoosh.Instance.AddBadgeNumber(1);

// ล้างค่า badge
Pushwoosh.Instance.SetBadgeNumber(0);
```

## การแก้ไขปัญหา

หากคุณพบปัญหาใดๆ ระหว่างกระบวนการผสานรวม โปรดดูที่ส่วน [การสนับสนุนและชุมชน](/th/developer/pushwoosh-sdk/support-and-community)