ข้ามไปยังเนื้อหา

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

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

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

Anchor link to

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

Anchor link to

1. เพิ่ม Pushwoosh Unity SDK

Anchor link to

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

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 เท่านั้น

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

Anchor link to

SDK จำเป็นต้องใช้ External Dependency Manager for Unity (EDM4U) เพื่อจัดการ dependencies ของ Android และ iOS

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

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

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

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

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

Anchor link to

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

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

Anchor link to

4.1 Capabilities

Anchor link to

หลังจาก 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

Anchor link to

เพิ่ม Pushwoosh Device API Token ไปยัง Info.plist ของคุณ:

Info.plist
<key>Pushwoosh_API_TOKEN</key>
<string>__PUSHWOOSH_DEVICE_API_TOKEN__</string>

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

Anchor link to

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

ทำตาม คู่มือเนทีฟ เพื่อเพิ่ม extension target

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

Anchor link to

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

Anchor link to

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

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

Anchor link to

เพิ่ม Pushwoosh Device API Token ไปยัง Assets/Plugins/Android/AndroidManifest.xml ของคุณภายในแท็ก <application>:

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

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

Anchor link to
  1. Build และรันโปรเจกต์บนแพลตฟอร์มเป้าหมายของคุณ
  2. ให้สิทธิ์สำหรับ push notifications เมื่อมีข้อความแจ้ง
  3. ไปที่ Pushwoosh Control Panel และ ส่ง push notification

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

Anchor link to

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

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

Anchor link to

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

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

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

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();
}

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

Anchor link to

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

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

// 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);
}
});

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

// ติดตาม 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" }
});

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

Anchor link to

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

// เปิดใช้งานการสื่อสาร
Pushwoosh.Instance.SetCommunicationEnabled(true);
// ปิดใช้งานการสื่อสาร
Pushwoosh.Instance.SetCommunicationEnabled(false);
// ตรวจสอบสถานะปัจจุบัน
bool isEnabled = Pushwoosh.Instance.IsCommunicationEnabled();

การจัดการ Badge

Anchor link to

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

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

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

Anchor link to

หากคุณพบปัญหาใดๆ ระหว่างกระบวนการผสานรวม โปรดดูที่ส่วน การสนับสนุนและชุมชน