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

คู่มือการผสานการทำงานพื้นฐานของ Android SDK

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

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

Anchor link to

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

ขั้นตอนการผสานการทำงาน

Anchor link to

1. ตั้งค่า Firebase Cloud Messaging

Anchor link to

ขั้นแรก เชื่อมต่อโปรเจกต์ Android ของคุณกับ Firebase:

  • เปิดโปรเจกต์ของคุณใน Android Studio
  • ไปที่ Tools > Firebase > Cloud Messaging และคลิก “Set up Firebase Cloud Messaging”
  • ทำตามคำแนะนำของวิซาร์ด ในขั้นตอนสุดท้าย วิซาร์ดจะเพิ่ม dependency ที่จำเป็นลงในไฟล์ build.gradle ของคุณ

2. เพิ่ม Pushwoosh SDK

Anchor link to

เพิ่ม dependency ของไลบรารี pushwoosh ไปยัง build.gradle ของแอปพลิเคชันของคุณ:

build.gradle
implementation 'com.pushwoosh:pushwoosh-firebase:6.+'

แทนที่ ”+” ด้วยเวอร์ชันปัจจุบันของ Pushwoosh Android SDK

3. เพิ่มข้อมูลเมตาของ Pushwoosh ไปยัง AndroidManifest.xml

Anchor link to

เปิดไฟล์ AndroidManifest.xml ของคุณและเพิ่มข้อมูลเมตาต่อไปนี้ภายในแท็ก <application>:

<meta-data android:name="com.pushwoosh.appid" android:value="__YOUR_APP_CODE__" />
<meta-data android:name="com.pushwoosh.senderid" android:value="@string/fcm_sender_id" />
<meta-data android:name="com.pushwoosh.apitoken" android:value="__YOUR_DEVICE_API_TOKEN__" />

โดยที่:

  • com.pushwoosh.appid คือ Pushwoosh Application Code ของคุณ
  • com.pushwoosh.apitoken คือ Pushwoosh Device API Token ของคุณ

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

ในการตั้งค่า com.pushwoosh.senderid ให้เพิ่ม fcm_sender_id ไปยังไฟล์ res/values/strings.xml ของโปรเจกต์ของคุณ:

res/values/strings.xml
<resources>
<string name="fcm_sender_id">__YOUR_FCM_SENDER_ID__</string>
</resources>

4. โค้ดการเริ่มต้น

Anchor link to

ลงทะเบียนเพื่อรับ push notifications โดยการเพิ่มโค้ดนี้ลงในตรรกะการเริ่มต้นของคุณ:

Pushwoosh.getInstance().registerForPushNotifications()

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

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

การผสานการทำงานเพิ่มเติม

Anchor link to

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

Push notifications

Anchor link to

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

  • onMessageReceived: เมธอดนี้จะถูกเรียกใช้เมื่อได้รับ push notification
  • onMessageOpened: เมธอดนี้จะถูกเรียกเมื่อผู้ใช้โต้ตอบกับ (เปิด) การแจ้งเตือน

callbacks เหล่านี้ช่วยให้นักพัฒนาสามารถจัดการการรับและการโต้ตอบของผู้ใช้กับ push notifications ภายในแอปพลิเคชันของตนได้:

AndroidManifest.xml
<meta-data
android:name="com.pushwoosh.notification_service_extension"
android:value="com.your.package.YourNotificationServiceExtension" />
import android.util.Log
import com.pushwoosh.notification.NotificationServiceExtension
import com.pushwoosh.notification.PushMessage
class YourNotificationServiceExtension : NotificationServiceExtension() {
override fun onMessageReceived(data: PushMessage): Boolean {
Log.d("Pushwoosh", "onMessageReceived: ${data.toJson()}")
return false
}
override fun onMessageOpened(message: PushMessage) {
Log.d("Pushwoosh", "onMessageOpened: ${message.toJson()}")
}
}

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

Anchor link to

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

import com.pushwoosh.Pushwoosh
import com.pushwoosh.tags.TagsBundle
import java.util.Date
class Registration {
public void afterUserLogin(User user) {
Pushwoosh pushwoosh = Pushwoosh.getInstance()
// Set user ID
if (user.getUserId() != null) {
pushwoosh.setUserId(user.getUserId())
}
// Set user email
if (user.getEmail() != null) {
pushwoosh.setEmail(user.getEmail())
}
// Setting additional user information as tags for Pushwoosh
if (user.getUserDetails() != null) {
val details = user.getUserDetails()
val tagsBuilder = TagsBundle.Builder()
if (details.getAge() != null) {
tagsBuilder.putInt("age", details.getAge())
}
if (details.getUserName() != null) {
tagsBuilder.putString("name", details.getUserName())
}
if (details.lastLoginDate() != null) {
tagsBuilder.putDate("last_login", details.getLastLoginDate())
}
// Build and send tags
val tags = tagsBuilder.build()
if (!tags.isEmpty()) {
pushwoosh.setTags(tags)
}
}
}
}

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

import com.pushwoosh.Pushwoosh
import com.pushwoosh.tags.TagsBundle
import java.util.List
class UpdateUser {
public void afterUserUpdateProfile(User user) {
val pushwoosh = Pushwoosh.getInstance()
val tagsBuilder = TagsBundle.Builder()
// Set list of favorite categories
if (user.favoriteCategories != null && user.favoriteCategories.isNotEmpty()) {
tagsBuilder.putList("favorite_categories", user.getFavoriteCategories())
}
// Set payment information
tagsBuilder.putBoolean("is_subscribed", user.isSubscribed())
tagsBuilder.putString("payment_status", user.getPaymentStatus())
tagsBuilder.putString("billing_address", user.getBillingAddress())
// Build and send tags
pushwoosh.setTags(tagsBuilder.build())
}
}

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

import com.pushwoosh.Pushwoosh
import com.pushwoosh.inapp.InAppManager
import com.pushwoosh.tags.TagsBundle
import java.util.Date
class Registration {
public void afterUserLogin(User user) {
if (user.getUserName() != null && user.getLastLoginDate() != null) {
val attributes = TagsBundle.Builder()
.putString("name", user.getUserName())
.putDate("last_login", user.getLastLoginDate())
.build()
InAppManager.getInstance().postEvent("login", attributes)
}
}
public void afterUserPurchase(User user, Product product) {
Pushwoosh pushwoosh = Pushwoosh.getInstance()
// Track purchase event
val purchaseAttributes = TagsBundle.Builder()
.putString("product_id", product.getId())
.putString("product_name", product.getName())
.putDouble("price", product.getPrice())
.putInt("quantity", product.getQuantity())
.build()
InAppManager.getInstance().postEvent("purchase", purchaseAttributes)
// Set user tags
val tagsBuilder = TagsBundle.Builder()
tagsBuilder.putDate("last_purchase_date", Date())
val lifetimeSpend = getCurrentLifetimeSpend() + product.getPrice()
tagsBuilder.putDouble("lifetime_spend", getCurrentLifetimeSpend())
pushwoosh.setTags(tagsBuilder.build())
}
}

Rich Media

Anchor link to

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

เพื่อเปิดใช้งานฟีเจอร์ Modal Rich Media คุณต้องกำหนดค่าในไฟล์ AndroidManifest.xml ของแอปพลิเคชันของคุณโดยการเพิ่มรายการข้อมูลเมตาต่อไปนี้:

AndroidManifest.xml
<meta-data
android:name="com.pushwoosh.rich_media_type"
android:value="Modal" />
import android.util.Log
import com.pushwoosh.richmedia.RichMedia
import com.pushwoosh.richmedia.RichMediaManager
import com.pushwoosh.richmedia.RichMediaPresentingDelegate
import com.pushwoosh.inapp.view.config.ModalRichmediaConfig
import com.pushwoosh.inapp.view.config.enums.*
class MainActivity : AppCompatActivity(), RichMediaPresentingDelegate {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
RichMediaManager.setDelegate(this)
// Configure the modal rich media
val config = ModalRichmediaConfig()
.setViewPosition(ModalRichMediaViewPosition.BOTTOM)
.setPresentAnimationType(ModalRichMediaPresentAnimationType.SLIDE_UP)
.setDismissAnimationType(ModalRichMediaDismissAnimationType.SLIDE_DOWN)
// Apply the configuration
RichMediaManager.setDefaultRichMediaConfig(config)
}
override fun shouldPresent(richMedia: RichMedia): Boolean {
Log.d("Pushwoosh", "Rich media will be presented with: ${richMedia.content}")
return true
}
override fun onPresent(richMedia: RichMedia) {
Log.d("Pushwoosh", "Rich media has been presented: ${richMedia.content}")
}
override fun onClose(richMedia: RichMedia) {
Log.d("Pushwoosh", "Rich media has been closed: ${richMedia.content}")
}
override fun onError(richMedia: RichMedia, pushwooshException: PushwooshException) {
Log.e("Pushwoosh", "Failed to present rich media: ${richMedia.content}. Error: ${pushwooshException.message}")
}
}

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

Anchor link to

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