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

คู่มือการผสานรวมพื้นฐาน Unity SDK

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

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

Anchor link to

ในการผสานรวม Pushwoosh Unity SDK เข้ากับแอปของคุณ คุณจะต้องมีสิ่งต่อไปนี้:

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

Anchor link to

1. เพิ่ม Dependency ของ Pushwoosh Unity SDK

Anchor link to

นำเข้า ปลั๊กอิน push notification ของ Unity ไปยังโฟลเดอร์ Assets ของคุณใน Unity

2. การเริ่มต้น Unity SDK

Anchor link to

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

  • เริ่มต้น Pushwoosh SDK ด้วย application code และ project number ของ Firebase
  • เรียกใช้ RegisterForPushNotifications() ในตรรกะการเริ่มต้นของคุณเพื่อลงทะเบียนสำหรับ push notification
  • เพิ่ม event handler การลงทะเบียนเพื่อจัดการ event การลงทะเบียน push:
PushNotificator.cs
using UnityEngine;
using UnityEngine.UI;
public class PushNotificator : MonoBehaviour {
void Start () {
Pushwoosh.ApplicationCode = "__YOUR_APP_ID__";
Pushwoosh.FcmProjectNumber = "__YOUR_FCM_SENDER_ID__";
Pushwoosh.Instance.OnRegisteredForPushNotifications += OnRegisteredForPushNotifications;
Pushwoosh.Instance.OnFailedToRegisteredForPushNotifications += OnFailedToRegisteredForPushNotifications;
Pushwoosh.Instance.RegisterForPushNotifications();
}
void OnRegisteredForPushNotifications(string token) {
Debug.LogFormat(LogType.Log, LogOption.NoStacktrace, null, "Received token: \n{0}", token);
}
void OnFailedToRegisteredForPushNotifications(string error) {
Debug.LogFormat(LogType.Log, LogOption.NoStacktrace, null, "Error ocurred while registering to push notifications: \n{0}", error);
}
}

โดยที่:

  • __YOUR_APP_ID__ คือ application code จาก Pushwoosh Control Panel
  • __YOUR_FCM_SENDER_ID__ คือ project number ของ Firebase จาก Firebase Console

3. การตั้งค่า Native ของ iOS

Anchor link to

3.1 Capabilities

Anchor link to

ในการเปิดใช้งาน Push Notifications ในโปรเจกต์ของคุณ คุณต้องเพิ่ม capabilities บางอย่าง

ในส่วน Signing & Capabilities ให้เพิ่ม capabilities ต่อไปนี้:

  • Push Notifications
  • Background Modes หลังจากเพิ่ม capability นี้แล้ว ให้เลือกช่องสำหรับ Remote notifications

หากคุณต้องการใช้ Time Sensitive Notifications (iOS 15+) ให้เพิ่ม capability Time Sensitive Notifications ด้วย

3.2 Info.plist

Anchor link to

ใน Runner/Info.plist ของคุณ ให้ตั้งค่าคีย์ __PUSHWOOSH_DEVICE_API_TOKEN__ เป็น Pushwoosh Device API Token:

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

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

Anchor link to

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

ทำตาม ขั้นตอนในคู่มือ native เพื่อเพิ่ม extension target และโค้ด Pushwoosh ที่จำเป็นภายในนั้น

4. การตั้งค่า Native ของ Android

Anchor link to

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

Anchor link to

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

4.2 ปรับการตั้งค่า build

Anchor link to

ในส่วน Publishing Settings ของโปรไฟล์ build Android ของคุณ ให้เปิดใช้งานตัวเลือก Custom Main Manifest

4.3 เพิ่ม metadata ของ Pushwoosh

Anchor link to

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

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

สำคัญ: ตรวจสอบให้แน่ใจว่าได้ให้สิทธิ์การเข้าถึงของ token แก่แอปที่ถูกต้องใน Pushwoosh Control Panel ของคุณ เรียนรู้เพิ่มเติม

5. การผสานรวม Windows Store

Anchor link to
  1. เพิ่ม link.xml ไปยังไดเรกทอรี Assets/ ของคุณด้วยเนื้อหาต่อไปนี้:
<linker>
<assembly fullname="PushSDK" preserve="all"/>
</linker>
  1. คุณต้อง Associate App with the Store ในโปรเจกต์ Visual Studio ที่ส่งออกด้วย ตรวจสอบให้แน่ใจว่าแอปของคุณได้รับการลงนามด้วยใบรับรองที่ตรงกับ Publisher Identity ของคุณ

เลือกใช้ capability Internet (Client) ในแท็บ Capabilities ของ .appxmanifest ของคุณในโปรเจกต์ Visual Studio ที่ส่งออก

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

Anchor link to
  1. Build และรันโปรเจกต์
  2. ไปที่ Pushwoosh Control Panel และ ส่ง push notification
  3. คุณควรเห็นการแจ้งเตือนในแอป

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

Anchor link to

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

Event listener ของ Push notification

Anchor link to

ใน Pushwoosh SDK มี event listener สองตัวที่ออกแบบมาเพื่อจัดการ push notification:

  • event OnPushNotificationsReceived จะถูกทริกเกอร์เมื่อได้รับ push notification
  • event OnPushNotificationsOpened จะถูกทริกเกอร์เมื่อผู้ใช้เปิดการแจ้งเตือน

คุณควรตั้งค่า event listener เหล่านี้ทันทีหลังจากการเริ่มต้น SDK ใน PushNotificator.cs:

PushNotificator.cs
using UnityEngine;
using UnityEngine.UI;
public class PushNotificator : MonoBehaviour {
void Start () {
Pushwoosh.ApplicationCode = "__YOUR_APP_ID__";
Pushwoosh.FcmProjectNumber = "__YOUR_FCM_SENDER_ID__";
Pushwoosh.Instance.OnPushNotificationsReceived += OnPushNotificationsReceived;
Pushwoosh.Instance.OnPushNotificationsOpened += OnPushNotificationsOpened;
Pushwoosh.Instance.RegisterForPushNotifications();
}
void OnPushNotificationsReceived(string payload) {
Debug.LogFormat(LogType.Log, LogOption.NoStacktrace, null, "Received push notificaiton: \n{0}", payload);
}
void OnPushNotificationsOpened(string payload) {
Debug.LogFormat(LogType.Log, LogOption.NoStacktrace, null, "Opened push notificaiton: \n{0}", payload);
}

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

Anchor link to

ด้วยการมุ่งเน้นไปที่พฤติกรรมและความชอบของผู้ใช้แต่ละราย คุณสามารถส่งมอบเนื้อหาที่ปรับให้เหมาะกับแต่ละบุคคล ซึ่งนำไปสู่ความพึงพอใจและความภักดีของผู้ใช้ที่เพิ่มขึ้น

public class Registration {
public void afterUserLogin(User user) {
// Set user ID
Pushwoosh.Instance.SetUserId(user.getId());
// Set user email
Pushwoosh.Instance.SetEmail(user.getEmail());
// Setting additional user information as tags for Pushwoosh
Pushwoosh.Instance.SetIntTag("Age", user.getAge());
Pushwoosh.Instance.SetStringTag("Name", user.getName());
Pushwoosh.Instance.SetStringTag("LastLoginDate", user.getLastLoginDate());
}
}

Tag คือคู่ของคีย์-ค่าที่กำหนดให้กับผู้ใช้หรืออุปกรณ์ ช่วยให้สามารถแบ่งกลุ่มตามคุณลักษณะต่างๆ เช่น ความชอบหรือพฤติกรรม ทำให้สามารถส่งข้อความแบบกำหนดเป้าหมายได้

public class UpdateUser {
public void afterUserUpdateProfile(User user) {
// Set list of favorite categories
Pushwoosh.Instance.SetListTag(
"favorite_categories": user.getFavoriteCategoriesList()
);
// Set payment information
Pushwoosh.Instance.SetStringTag("is_subscribed", user.isSubscribed());
Pushwoosh.Instance.SetStringTag("payment_status", user.getPaymentStatus());
Pushwoosh.Instance.SetStringTag("billing_address", user.getBillingAddress());
}
}

Event คือการกระทำหรือเหตุการณ์เฉพาะของผู้ใช้ภายในแอปที่สามารถติดตามเพื่อวิเคราะห์พฤติกรรมและทริกเกอร์ข้อความหรือการกระทำที่สอดคล้องกัน

public class Registration {
// Track login event
public void afterUserLogin(User user) {
Pushwoosh.Instance.PostEvent("login", new Dictionary<string, object>() {
{ "name", user.getName() },
{ "last_login", user.getLastLoginDate() }
});
}
public void afterUserPurchase(Product product) {
// Track purchase event
Pushwoosh.Instance.PostEvent("purchase", new Dictionary<string, object>() {
{ "product_id", product.getId() },
{ "product_name", product.getName() },
{ "price", product.getPrice() },
{ "quantity", product.getQuantity() }
});
}
}

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

Anchor link to

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