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

คู่มือการย้าย (Migration guide)

การย้ายจาก Android SDK 5 ไปยัง 6

Anchor link to
  1. ย้ายโปรเจกต์ของคุณไปยัง AndroidX ตาม เอกสารอย่างเป็นทางการ
  2. เพิ่มโมดูลใหม่ในไฟล์ build.gradle ของแอป หากคุณใช้ Firebase Cloud Messaging
build.gradle
implementation 'com.pushwoosh:pushwoosh-firebase:+'

สำหรับผู้ที่ใช้ Amazon และไม่ได้ใช้ Firebase ไม่จำเป็นต้องทำการเปลี่ยนแปลงใดๆ

การย้ายจาก Android SDK เวอร์ชันก่อนหน้า

Anchor link to

คลาสและเมธอดส่วนใหญ่ของ Pushwoosh ได้ถูกเลิกใช้งานแล้ว (deprecated) ตั้งแต่เวอร์ชัน 5.0 และบางส่วนได้ถูกลบออกไปแล้ว คลาส PushManager, BasePushMessageReceiver, BaseRegistrationReceiver, SendPushTagsCallback, PushFragment และ PushEventListener ยังคงใช้งานได้ในฐานะส่วนหนึ่งของไลบรารี com.pushwoosh:pushwoosh-deprecated แต่เราขอแนะนำให้ย้ายไปใช้ API ใหม่โดยเร็วที่สุด

PushManager

Anchor link to

PushManager ประกอบด้วยเมธอดต่างๆ สำหรับฟีเจอร์ที่แตกต่างกัน ซึ่งตอนนี้เมธอดเหล่านี้ได้ถูกแยกไปยังคลาสและไลบรารีต่างๆ ดังนี้:
com.pushwoosh:pushwoosh: Pushwoosh, PushwooshNotificationSettings, PushwooshInApp
com.pushwoosh:pushwoosh-badge: PushwooshBadge
com.pushwoosh:pushwoosh-location: PushwooshLocation
com.pushwoosh:pushwoosh-beacon: PushwooshBeacon

BaseRegistrationReceiver

Anchor link to

BaseRegistrationReceiver เคยถูกใช้เพื่อจัดการเหตุการณ์การลงทะเบียนและยกเลิกการลงทะเบียน push notification ซึ่งตอนนี้ receiver นี้ถูกแทนที่ด้วยกลไก callback แบบง่าย:

Pushwoosh.getInstance().registerForPushNotifications(result -> {
if (result.isSuccess()) {
String token = result.getData();
// handle successful registration
}
else {
PushwooshException exception = result.getException();
// handle registration error
}
});

BasePushMessageReceiver

Anchor link to

BasePushMessageReceiver เคยถูกใช้เพื่อเลียนแบบพฤติกรรมการแจ้งเตือนของ iOS เมื่อได้รับการแจ้งเตือน push ขณะที่แอปทำงานอยู่เบื้องหน้า (foreground) ซึ่งทำได้โดยการยกเลิกการแจ้งเตือนที่เข้ามาและเรียกใช้ callback onMessageReceive วิธีนี้ค่อนข้างยุ่งยากและต้องมีการลงทะเบียนด้วยตนเองเมื่อแอปพลิเคชันทำงาน (active) และยกเลิกการลงทะเบียนเมื่อแอปพลิเคชันไปทำงานอยู่เบื้องหลัง (background)
receiver นี้ถูกแทนที่ด้วย NotificationServiceExtension:

AndroidManifest.xml
<meta-data
android:name="com.pushwoosh.notification_service_extension"
android:value="com.your.package.name.YourNotificationServiceExtension"/>
YourNotificationServiceExtension.java
public class YourNotificationServiceExtension extends NotificationServiceExtension {
@Override
public boolean onMessageReceived(final PushMessage message) {
if (isAppOnForeground()) {
Handler mainHandler = new Handler(getApplicationContext().getMainLooper());
mainHandler.post(() -> {
handlePush(message);
});
// this indicates that notification should not be displayed
return true;
}
return false;
}
@Override
protected void startActivityForPushMessage(PushMessage message) {
super.startActivityForPushMessage(message);
handlePush(message);
}
@MainThread
private void handlePush(PushMessage message) {
// TODO: handle push message
}
}

Extension นี้ยังใช้เพื่อจัดการเหตุการณ์เมื่อได้รับการแจ้งเตือน (notification arrival) และการตอบรับการแจ้งเตือน (accept events) ซึ่งมาแทนที่โค้ดที่ยุ่งยากทั้งหมดที่เคยใช้ในการผนวกเข้ากับ Activity ด้วยตนเอง

PushFragment

Anchor link to

PushFragment เป็นทางเลือกที่เรียบง่ายกว่าสำหรับการผนวกที่ซับซ้อนซึ่งเกี่ยวข้องกับ Activity lifecycle แต่ในทางกลับกัน มันจำเป็นต้องมีการสืบทอด (inheritance) จาก FragmentActivity และใช้ Fragment lifecycle ที่ซับซ้อนกว่าโดยนัย
ปัจจุบัน PushFragment และ PushEventListener ถูกแทนที่ด้วย Pushwoosh#registerForPushNotifications(Callback) และ NotificationServiceExtension

Custom Push Broadcast Receiver (PW_NOTIFICATION_RECEIVER)

Anchor link to

PW_NOTIFICATION_RECEIVER เคยถูกใช้เพื่อปรับแต่งพฤติกรรมเมื่อผู้ใช้คลิกที่การแจ้งเตือน ซึ่งช่วยให้สามารถจัดการการแจ้งเตือนนอก Activity context และเปิด Activity ต่างๆ ได้ขึ้นอยู่กับเนื้อหาของการแจ้งเตือน การผนวกลักษณะนี้ใช้ API ภายในของ Pushwoosh SDK ซึ่งไม่มีอยู่อีกต่อไปแล้ว
ปัจจุบัน receiver นี้ถูกแทนที่โดยสมบูรณ์ด้วย NotificationServiceExtension:

YourNotificationServiceExtension.java
public class YourNotificationServiceExtension extends NotificationServiceExtension {
@Override
protected void startActivityForPushMessage(PushMessage message) {
// super.startActivityForPushMessage() starts default launcher activity
// or activity marked with ${applicationId}.MESSAGE action.
// Simply do not call it to override this behaviour.
// super.startActivityForPushMessage(message);
// start your activity instead:
Intent launchIntent = new Intent(getApplicationContext(), YourActivity.class);
launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
// (Optional) pass notification data to Activity
launchIntent.putExtra(Pushwoosh.PUSH_RECEIVE_EVENT, message.toJson().toString());
context.startActivity(launchIntent);
}
}

Notification Factory

Anchor link to

นอกจากนี้ยังมีการเปลี่ยนแปลงที่เข้ากันไม่ได้ (breaking changes) บางประการที่เกี่ยวข้องกับ notification factory:

1. AbsNotificationFactory ถูกแทนที่ด้วย NotificationFactory
2. เมธอด AbsNotificationFactory#onPushReceived(PushData) และ AbsNotificationFactory#onPushHandle(Activity) ถูกแทนที่ด้วยคลาส NotificationServiceExtension (onMessageReceived, startActivityForPushMessage)
3. DefaultNotificationFactory ถูกแทนที่ด้วย PushwooshNotificationFactory
4. PushData ถูกแทนที่ด้วย PushMessage

In-App Messages

Anchor link to

1. InAppFacade ถูกแทนที่ด้วย PushwooshInApp
2. มีการเพิ่มอ็อบเจกต์ pushwoosh สำหรับ JavaScript native interface ซึ่งมี API ดังต่อไปนี้:
getHwid(): string - คืนค่า pushwoosh hwid สำหรับอุปกรณ์ปัจจุบัน
getVersion(): string - คืนค่าเวอร์ชันของ Pushwoosh SDK ปัจจุบัน
postEvent(event: string, attributes?: object, successCallback?: function, errorCallback?: function) - ส่งคำขอ postEvent
sendTags(tags: object) - ส่งแท็กที่เชื่อมโยงกับอุปกรณ์ปัจจุบัน
getTags(successCallback: function, errorCallback?: function) - คืนค่าแท็กที่เชื่อมโยงกับอุปกรณ์ปัจจุบัน
closeInApp() - ปิดหน้า In-App ที่เป็น html

แบ่งปันข้อเสนอแนะของคุณกับเรา

Anchor link to

ข้อเสนอแนะของคุณช่วยให้เราสร้างประสบการณ์ที่ดียิ่งขึ้น เราจึงยินดีรับฟังหากคุณพบปัญหาใดๆ ในระหว่างขั้นตอนการผนวก SDK หากคุณประสบปัญหาใดๆ โปรดอย่าลังเลที่จะแบ่งปันความคิดเห็นของคุณกับเรา ผ่านแบบฟอร์มนี้