คู่มือการผสานรวม Unity SDK เบื้องต้น
คู่มือนี้จะแนะนำขั้นตอนการผสานรวม Pushwoosh Unity SDK เข้ากับแอปพลิเคชันของคุณ
ข้อกำหนดเบื้องต้น
Anchor link toขั้นตอนการผสานรวม
Anchor link to1. เพิ่ม Pushwoosh Unity SDK
Anchor link toเพิ่มข้อมูลต่อไปนี้ลงใน 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 เท่านั้น
ใน Unity ไปที่ Window > Package Manager > + > Add package from git URL และเพิ่ม URL ต่อไปนี้ทีละรายการ:
https://github.com/Pushwoosh/pushwoosh-unity.git?path=com.pushwoosh.unity.corehttps://github.com/Pushwoosh/pushwoosh-unity.git?path=com.pushwoosh.unity.androidhttps://github.com/Pushwoosh/pushwoosh-unity.git?path=com.pushwoosh.unity.iosดาวน์โหลด Pushwoosh.unitypackage จาก GitHub Releases และนำเข้าผ่าน Assets > Import Package > Custom Package
2. ติดตั้ง External Dependency Manager
Anchor link toSDK จำเป็นต้องใช้ 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:
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 to4.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 ของคุณ:
<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 to5.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>:
<meta-data android:name="com.pushwoosh.apitoken" android:value="__YOUR_DEVICE_API_TOKEN__" />6. รันโปรเจกต์
Anchor link to- Build และรันโปรเจกต์บนแพลตฟอร์มเป้าหมายของคุณ
- ให้สิทธิ์สำหรับ push notifications เมื่อมีข้อความแจ้ง
- ไปที่ Pushwoosh Control Panel และ ส่ง push notification
การผสานรวมเพิ่มเติม
Anchor link toณ จุดนี้ คุณสามารถส่งและรับ push notifications ได้แล้ว ส่วนต่อไปนี้จะครอบคลุมฟังก์ชันการทำงานหลักของ SDK
ตัวดักจับ event ของ Push notification
Anchor link toSDK มีตัวดักจับ event สองตัวสำหรับจัดการ push notifications:
OnPushNotificationsReceived— ทำงานเมื่อมี push notification เข้ามาOnPushNotificationsOpened— ทำงานเมื่อผู้ใช้แตะที่การแจ้งเตือน
ตั้งค่าตัวดักจับเหล่านี้ระหว่างการเริ่มต้น SDK:
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
Anchor link toTags คือคู่ของ 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); }});Events
Anchor link toติดตามการกระทำของผู้ใช้เพื่อวิเคราะห์พฤติกรรมและกระตุ้นการส่งข้อความอัตโนมัติ:
// ติดตาม 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);
// เพิ่มค่า badgePushwoosh.Instance.AddBadgeNumber(1);
// ล้างค่า badgePushwoosh.Instance.SetBadgeNumber(0);การแก้ไขปัญหา
Anchor link toหากคุณพบปัญหาใดๆ ระหว่างกระบวนการผสานรวม โปรดดูที่ส่วน การสนับสนุนและชุมชน