คู่มือการผสานรวม Cordova SDK เบื้องต้น
ส่วนนี้ประกอบด้วยข้อมูลเกี่ยวกับวิธีการผสานรวม Pushwoosh Cordova SDK เข้ากับแอปพลิเคชันของคุณ
ข้อกำหนดเบื้องต้น
Anchor link toในการผสานรวม Pushwoosh Cordova SDK เข้ากับแอปของคุณ คุณจะต้องมีสิ่งต่อไปนี้:
ขั้นตอนการผสานรวม
Anchor link to1. เพิ่ม Pushwoosh Cordova SDK Dependency
Anchor link toเพิ่ม Pushwoosh Cordova SDK dependency ไปยังโปรเจกต์ของคุณ:
cordova plugin add pushwoosh-cordova-plugin2. การเริ่มต้น Cordova SDK
Anchor link toใน root component ของไฟล์ index.js ของคุณ:
- เริ่มต้น Pushwoosh SDK
- เรียกใช้
registerDevice()ใน logic การเริ่มต้นของคุณเพื่อลงทะเบียนรับ push notifications
function onDeviceReady() { var pushwoosh = cordova.require("pushwoosh-cordova-plugin.PushNotification");
pushwoosh.onDeviceReady({ appid: "__YOUR_APP_ID__", projectid: "__YOUR_FCM_SENDER_ID__" });
pushwoosh.registerDevice( function(status) { var pushToken = status.pushToken; // Handle successful registration }, function(status) { // Handle registration error });โดยที่:
__YOUR_APP_ID__คือ application code จาก Pushwoosh Control Panel__YOUR_FCM_SENDER_ID__คือ Firebase project number จาก Firebase Console
3. การตั้งค่า iOS Native
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คุณต้องเพิ่ม Notification Service Extension target ไปยังโปรเจกต์ของคุณ สิ่งนี้จำเป็นสำหรับการติดตามการส่งที่แม่นยำและฟีเจอร์ต่างๆ เช่น Rich Media บน iOS
ทำตาม ขั้นตอนในคู่มือ native เพื่อเพิ่ม extension target และโค้ด Pushwoosh ที่จำเป็นภายในนั้น
4. การตั้งค่า Android Native
Anchor link to4.1 ติดตั้ง dependencies
Anchor link toตรวจสอบให้แน่ใจว่า dependencies และ plugins ที่จำเป็นถูกเพิ่มลงใน Gradle scripts ของคุณ:
เพิ่ม 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 เพิ่ม Pushwoosh metadata
Anchor link toใน main/AndroidManifest.xml ของคุณ ให้เพิ่ม Pushwoosh Device API Token ภายในแท็ก <application>:
<meta-data android:name="com.pushwoosh.apitoken" android:value="__YOUR_DEVICE_API_TOKEN__" />สำคัญ: โปรดตรวจสอบให้แน่ใจว่าได้ให้สิทธิ์การเข้าถึงโทเค็นไปยังแอปที่ถูกต้องใน Pushwoosh Control Panel ของคุณ เรียนรู้เพิ่มเติม
5. รันโปรเจกต์
Anchor link to- Build และ run โปรเจกต์
- ไปที่ Pushwoosh Control Panel และ ส่ง push notification
- คุณควรเห็น notification ในแอป
การผสานรวมแบบขยาย
Anchor link toในขั้นตอนนี้ คุณได้ผสานรวม SDK แล้ว และสามารถส่งและรับ push notifications ได้แล้ว ตอนนี้ เรามาสำรวจฟังก์ชันหลักกัน
Push notification event listeners
Anchor link toใน Pushwoosh SDK มี event listeners สองตัวที่ออกแบบมาสำหรับการจัดการ push notifications:
- event
onPushReceivedจะถูกเรียกใช้เมื่อได้รับ push notification - event
onPushAcceptedจะถูกเรียกใช้เมื่อผู้ใช้เปิด notification
คุณควรตั้งค่า event listeners เหล่านี้ทันทีหลังจากเริ่มต้น SDK เมื่อแอปพลิเคชันเริ่มทำงาน:
class PushwooshNotificationHandler { setupPushListeners() {
document.addEventListener('push-receive', function(event) { console.log("Push received: " + event.notification.message); } );
document.addEventListener('push-notification', function(event) { console.log("Push accepted: " + event.notification.message); } ); }}การกำหนดค่าผู้ใช้
Anchor link toด้วยการมุ่งเน้นไปที่พฤติกรรมและความชอบของผู้ใช้แต่ละคน คุณสามารถนำเสนอเนื้อหาส่วนบุคคล ซึ่งนำไปสู่ความพึงพอใจและความภักดีของผู้ใช้ที่เพิ่มขึ้น
class Registration { afterUserLogin(user) {
// Set user ID pushwoosh.setUserId(user.getId());
// Setting additional user information as tags for Pushwoosh pushwoosh.setTags({ "age": user.getAge(), "name": user.getName(), "last_login": user.getLastLoginDate() }); }}Tags
Anchor link toTags คือคู่ key-value ที่กำหนดให้กับผู้ใช้หรืออุปกรณ์ ซึ่งช่วยให้สามารถแบ่งกลุ่มตามคุณลักษณะต่างๆ เช่น ความชอบหรือพฤติกรรม ทำให้สามารถส่งข้อความที่ตรงเป้าหมายได้
class UpdateUser { afterUserUpdateProfile(user) {
// Set list of favorite categories pushwoosh.setTags({ "favorite_categories": user.getFavoriteCategoriesList() });
// Set payment information pushwoosh.setTags({ "is_subscribed": user.isSubscribed(), "payment_status": user.getPaymentStatus(), "billing_address": user.getBillingAddress() }); }}Events
Anchor link toEvents คือการกระทำของผู้ใช้หรือเหตุการณ์เฉพาะภายในแอปที่สามารถติดตามได้เพื่อวิเคราะห์พฤติกรรมและกระตุ้นข้อความหรือการกระทำที่เกี่ยวข้อง
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หากคุณพบปัญหาใดๆ ในระหว่างกระบวนการผสานรวม โปรดดูส่วน support and community