คู่มือการผสานรวมพื้นฐาน Capacitor SDK
ส่วนนี้มีข้อมูลเกี่ยวกับวิธีการผสานรวม Pushwoosh Capacitor SDK เข้ากับแอปพลิเคชันของคุณ
ข้อกำหนดเบื้องต้น
Anchor link toในการผสานรวม Pushwoosh Capacitor SDK เข้ากับแอปของคุณ คุณจะต้องมีสิ่งต่อไปนี้:
ขั้นตอนการผสานรวม
Anchor link to1. เพิ่ม Dependency ของ Pushwoosh Capacitor SDK
Anchor link toเพิ่ม dependency ของ Pushwoosh Capacitor SDK ไปยังโปรเจกต์ของคุณ:
npm install pushwoosh-capacitor-pluginซิงค์การกำหนดค่า Capacitor:
npx cap sync2. การเริ่มต้น Capacitor SDK
Anchor link toในไฟล์ JavaScript หลักของคุณ ให้ import และเริ่มต้น Pushwoosh SDK:
import { Pushwoosh } from 'pushwoosh-capacitor-plugin';
// Initialize the SDKPushwoosh.onDeviceReady({ appid: "__YOUR_APP_CODE__", projectid: "__YOUR_FCM_SENDER_ID__"});
// Register for push notificationsPushwoosh.registerDevice() .then(result => { console.log("Push token:", result.pushToken); // Handle successful registration }) .catch(error => { console.error("Failed to register device:", error); // Handle registration error });โดยที่:
__YOUR_APP_ID__คือ application code จาก Pushwoosh Control Panel__YOUR_FCM_SENDER_ID__คือ project number ของ Firebase จาก Firebase Console
3. การตั้งค่า Native ของ iOS
Anchor link to3.1 Capabilities
Anchor link toในการเปิดใช้งาน Push Notifications ในโปรเจกต์ของคุณ คุณต้องเพิ่ม capabilities บางอย่าง
ในส่วน Signing & Capabilities ให้เพิ่ม capabilities ต่อไปนี้:
Push NotificationsBackground 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:
<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 to4.1 ติดตั้ง dependencies
Anchor link toตรวจสอบให้แน่ใจว่าได้เพิ่ม dependencies และ plugin ที่จำเป็นลงในสคริปต์ Gradle ของคุณแล้ว:
เพิ่ม Google Services Gradle plugin ไปยัง dependencies ใน build.gradle ระดับโปรเจกต์ของคุณ:
buildscript { dependencies { classpath 'com.google.gms:google-services:4.3.15' }}ใช้ plugin ในไฟล์ build.gradle ระดับแอปของคุณ:
apply plugin: 'com.google.gms.google-services'4.2 เพิ่มไฟล์การกำหนดค่า Firebase
Anchor link toวางไฟล์ google-services.json ลงในโฟลเดอร์ android/app ในไดเรกทอรีโปรเจกต์ของคุณ
4.3 เพิ่ม metadata ของ Pushwoosh
Anchor link toใน main/AndroidManifest.xml ของคุณ ให้เพิ่ม Pushwoosh Device API Token ภายในแท็ก <application>:
<meta-data android:name="com.pushwoosh.apitoken" android:value="__YOUR_DEVICE_API_TOKEN__" />สำคัญ: ตรวจสอบให้แน่ใจว่าได้ให้สิทธิ์การเข้าถึงของ token แก่แอปที่ถูกต้องใน Pushwoosh Control Panel ของคุณ เรียนรู้เพิ่มเติม
5. รันโปรเจกต์
Anchor link to- บิวด์และรันโปรเจกต์
- ไปที่ Pushwoosh Control Panel และ ส่ง push notification
- คุณควรจะเห็นการแจ้งเตือนในแอป
การผสานรวมเพิ่มเติม
Anchor link toณ จุดนี้ คุณได้ผสานรวม SDK เรียบร้อยแล้วและสามารถส่งและรับ push notification ได้ ตอนนี้เรามาดูฟังก์ชันการทำงานหลักกัน
Listener สำหรับ event ของ Push notification
Anchor link toใน Pushwoosh Capacitor SDK มี callback method สองตัวสำหรับจัดการ push notification:
pushReceivedCallbackจะถูกเรียกเมื่อได้รับ push notificationpushOpenedCallbackจะถูกเรียกเมื่อผู้ใช้เปิดการแจ้งเตือน
คุณควรตั้งค่า callback เหล่านี้ทันทีหลังจากการเริ่มต้น SDK:
import { Pushwoosh } from 'pushwoosh-capacitor-plugin';
// Set up push received callbackawait Pushwoosh.pushReceivedCallback((notification, err) => { if (err) { console.error("Failed to process received notification:", err); } else { console.log("Push received:", JSON.stringify(notification)); // Handle the received notification }});
// Set up push opened callbackawait Pushwoosh.pushOpenedCallback((notification, err) => { if (err) { console.error("Failed to process opened notification:", err); } else { console.log("Push opened:", JSON.stringify(notification)); // Handle the opened notification }});การกำหนดค่าผู้ใช้
Anchor link toด้วยการมุ่งเน้นไปที่พฤติกรรมและความชอบของผู้ใช้แต่ละราย คุณสามารถส่งมอบเนื้อหาที่ปรับให้เหมาะกับแต่ละบุคคล ซึ่งนำไปสู่ความพึงพอใจและความภักดีของผู้ใช้ที่เพิ่มขึ้น
import { Pushwoosh } from 'pushwoosh-capacitor-plugin';
class Registration { async afterUserLogin(user) {
// Set user ID Pushwoosh.setUserId(user.getId);
// Setting additional user information as tags for Pushwoosh await Pushwoosh.setTags({ "age": user.getAge(), "name": user.getName(), "last_login": user.getLastLoginDate() }); }}Tags
Anchor link toTags คือคู่ key-value ที่กำหนดให้กับผู้ใช้หรืออุปกรณ์ ช่วยให้สามารถแบ่งกลุ่มตามคุณลักษณะต่างๆ เช่น ความชอบหรือพฤติกรรม ทำให้สามารถส่งข้อความแบบกำหนดเป้าหมายได้
import { Pushwoosh } from 'pushwoosh-capacitor-plugin';
class UpdateUser { async afterUserUpdateProfile(user) {
// Set list of favorite categories await Pushwoosh.setTags({ "favorite_categories": user.getFavoriteCategoriesList() });
// Set payment information await Pushwoosh.setTags({ "is_subscribed": user.isSubscribed(), "payment_status": user.getPaymentStatus(), "billing_address": user.getBillingAddress() }); }}Events
Anchor link toEvents คือการกระทำหรือเหตุการณ์ที่เกิดขึ้นโดยผู้ใช้ภายในแอป ซึ่งสามารถติดตามเพื่อวิเคราะห์พฤติกรรมและกระตุ้นให้เกิดข้อความหรือการกระทำที่สอดคล้องกัน
import { Pushwoosh } from 'pushwoosh-capacitor-plugin';
class Registration {
// Track login event afterUserLogin(user) { Pushwoosh.postEvent("login", { "name": user.getName(), "last_login": user.getLastLoginDate() }); }
// Track purchase event afterUserPurchase(product) { Pushwoosh.postEvent("purchase", { "product_id": product.getId(), "product_name": product.getName(), "price": product.getPrice(), "quantity": product.getQuantity() }); }}การแก้ไขปัญหา
Anchor link toหากคุณพบปัญหาใดๆ ในระหว่างขั้นตอนการผสานรวม โปรดดูที่ส่วน การสนับสนุนและชุมชน