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

คู่มือการผสานระบบพื้นฐานสำหรับ Flutter SDK

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

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

Anchor link to

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

ขั้นตอนการผสานระบบ

Anchor link to

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

Anchor link to

เพิ่มแพ็กเกจ pushwoosh_flutter ไปยังไฟล์ pubspec.yaml ของคุณ:

pubspec.yaml
dependencies:
flutter:
sdk: flutter
# ใช้เวอร์ชันล่าสุดจาก https://pub.dev/packages/pushwoosh_flutter
pushwoosh_flutter: ^[LATEST_VERSION]

ตรวจสอบ เวอร์ชันล่าสุด บน pub.dev

จากนั้น รันคำสั่งต่อไปนี้ในไดเรกทอรีรากของโปรเจกต์ของคุณเพื่อติดตั้ง dependency:

Terminal window
flutter pub get

ตรวจสอบอีกครั้งว่าแพ็กเกจถูกติดตั้งอย่างถูกต้อง:

Terminal window
flutter pub deps | grep pushwoosh_flutter
# ผลลัพธ์ตัวอย่าง:
# ❯ flutter pub deps | grep pushwoosh_flutter
# └── pushwoosh_flutter 2.3.11

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

Anchor link to

ในคอมโพเนนต์รากของไฟล์ main.dart ของคุณ:

  • นำเข้าแพ็กเกจ pushwoosh_flutter
  • เริ่มต้น Pushwoosh SDK
  • เรียก registerForPushNotifications() ในตรรกะการเริ่มต้นของคุณเพื่อลงทะเบียนสำหรับการแจ้งเตือนแบบพุช
main.dart
import 'package:pushwoosh_flutter/pushwoosh_flutter.dart';
void main() async {
runApp(const MyApp());
Pushwoosh.initialize({
"app_id": "__YOUR_APP_ID__",
"sender_id": "__YOUR_FCM_SENDER_ID__"
});
Pushwoosh.getInstance.registerForPushNotifications();
}

โดยที่:

  • __YOUR_APP_ID__ คือรหัสแอปพลิเคชันจาก Pushwoosh Control Panel
  • __YOUR_FCM_SENDER_ID__ คือหมายเลขโปรเจกต์ 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

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

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

เพื่อให้แน่ใจว่า Notification Service Extension ถูกผสานรวมในโปรเจกต์ Flutter ของคุณอย่างถูกต้อง คุณต้องใช้การกำหนดค่า Podfile ต่อไปนี้:

Podfile
target 'NotificationServiceExtension' do
use_frameworks!
use_modular_headers!
pod 'PushwooshXCFramework'
inherit! :search_paths
end

3.4 การติดตั้ง Dependencies สำหรับโปรเจกต์ iOS Flutter

Anchor link to

เพื่อติดตั้ง dependencies สำหรับโปรเจกต์ iOS Flutter ให้รันคำสั่งต่อไปนี้:

Terminal window
flutter run

หรือไปที่โฟลเดอร์ ios ในเทอร์มินัลแล้วรัน:

Terminal window
pod install --repo-update

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

Anchor link to

4.1 ติดตั้ง Dependencies

Anchor link to

ตรวจสอบให้แน่ใจว่าได้เพิ่ม dependencies และปลั๊กอินที่จำเป็นลงในสคริปต์ Gradle ของคุณแล้ว:

เพิ่มปลั๊กอิน Google Services Gradle ไปยัง dependencies ของ build.gradle ระดับโปรเจกต์ของคุณ:

android/build.gradle
buildscript {
dependencies {
classpath 'com.google.gms:google-services:4.3.15'
}
}

ใช้ปลั๊กอินในไฟล์ build.gradle ระดับแอปของคุณ:

app/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>:

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

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

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

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

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

Anchor link to

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

ตัวดักฟังเหตุการณ์การแจ้งเตือนแบบพุช (Push notification event listeners)

Anchor link to

ใน Pushwoosh SDK มี event listeners สองตัวที่ออกแบบมาเพื่อจัดการกับการแจ้งเตือนแบบพุช:

  • เหตุการณ์ onPushReceived จะถูกทริกเกอร์เมื่อได้รับการแจ้งเตือนแบบพุช
  • เหตุการณ์ onPushAccepted จะถูกทริกเกอร์เมื่อผู้ใช้เปิดการแจ้งเตือน

คุณควรตั้งค่า event listeners เหล่านี้ทันทีหลังจากการเริ่มต้น SDK เมื่อแอปพลิเคชันเริ่มทำงาน:

import 'package:pushwoosh_flutter/pushwoosh_flutter.dart';
class PushwooshNotificationHandler {
void setupPushListeners(Pushwoosh pushwoosh) {
pushwoosh.onPushReceived.listen((event) {
print("Push received: ${event.pushwooshMessage.payload}");
});
pushwoosh.onPushAccepted.listen((event) {
print("Push accepted: ${event.pushwooshMessage.payload}");
});
}
}

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

Anchor link to

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

import 'package:pushwoosh_flutter/pushwoosh_flutter.dart';
class Registration {
void afterUserLogin(User user) {
// ตั้งค่ารหัสผู้ใช้
Pushwoosh().setUserId(user.getId());
// ตั้งค่าอีเมลผู้ใช้
Pushwoosh().setEmail(user.getEmail());
// ลงทะเบียนหมายเลข SMS
// หมายเลข SMS และ WhatsApp ต้องอยู่ในรูปแบบ E.164 (เช่น "+1234567890") และต้องเป็นหมายเลขที่ถูกต้อง
Pushwoosh().registerSmsNumber(user.getSmsNumber());
// ลงทะเบียนหมายเลข WhatsApp
Pushwoosh().registerWhatsappNumber(user.getWhatsappNumber());
// การตั้งค่าข้อมูลผู้ใช้เพิ่มเติมเป็นแท็กสำหรับ Pushwoosh
Pushwoosh().setTags({
"age": user.getAge(),
"name": user.getName(),
"last_login": user.getLastLoginDate()
});
}
}

แท็ก (Tags)

Anchor link to

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

import 'package:pushwoosh_flutter/pushwoosh_flutter.dart';
class UpdateUser {
void afterUserUpdateProfile(User user) {
// ตั้งค่ารายการหมวดหมู่ที่ชื่นชอบ
Pushwoosh().setTags({
"favorite_categories": user.getFavoriteCategoriesList()
});
// ตั้งค่าข้อมูลการชำระเงิน
Pushwoosh().setTags({
"is_subscribed": user.isSubscribed(),
"payment_status": user.getPaymentStatus(),
"billing_address": user.getBillingAddress()
});
}
}

เหตุการณ์ (Events)

Anchor link to

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

import 'package:pushwoosh_flutter/pushwoosh_flutter.dart';
class Registration {
// ติดตามเหตุการณ์การเข้าสู่ระบบ
void afterUserLogin(User user) {
Pushwoosh().postEvent("login", {
"name": user.getName(),
"last_login": user.getLastLoginDate()
});
}
void afterUserPurchase(Product product) {
// ติดตามเหตุการณ์การซื้อ
Pushwoosh().postEvent("purchase", {
"product_id": product.getId(),
"product_name": product.getName(),
"price": product.getPrice(),
"quantity": product.getQuantity()
});
}
}

การใช้ ProGuard

Anchor link to

ดังนั้น คุณอาจได้รับ exception นี้:

java.lang.IllegalStateException: Could not find class for name: com.pushwoosh.plugin.PushwooshNotificationServiceExtension

มีสองวิธีแก้ไขในกรณีนี้:

  1. ใช้คำสั่ง flutter build apk --no-shrink เพื่อคอมไพล์โค้ดของคุณโดยไม่มีการ obfuscation
  2. หรือคุณสามารถเปิดใช้งาน ProGuard ด้วยตนเองและเพิ่มกฎที่จำเป็น

เพื่อเปิดใช้งาน ProGuard สำหรับโปรเจกต์ของคุณ ให้เพิ่มสตริงต่อไปนี้ไปยังไฟล์ build.gradle ของคุณ:

build.gradle
buildTypes {
release {
minifyEnabled true
useProguard true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.debug
}
}

จากนั้น เพิ่มกฎต่อไปนี้ไปยัง android/app/proguard-rules.pro

proguard-rules.pro
#Pushwoosh Flutter
-keep class com.pushwoosh.plugin.PushwooshPlugin { *; }
-keep class com.pushwoosh.plugin.PushwooshNotificationServiceExtension { *; }

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

Anchor link to

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