跳到内容

迁移指南

从 Android SDK 5 迁移到 6

Anchor link to
  1. 根据官方文档将您的项目迁移到 AndroidX。
  2. 如果您正在使用 Firebase Cloud Messaging,请在应用的 build.gradle 文件中添加一个新模块。
build.gradle
implementation 'com.pushwoosh:pushwoosh-firebase:+'

对于使用 Amazon 且不使用 Firebase 的用户,无需进行任何更改。

从早期版本的 Android SDK 迁移

Anchor link to

自 5.0 版本发布以来,大多数 Pushwoosh 类和方法已被弃用,其中一些已不再存在。PushManagerBasePushMessageReceiverBaseRegistrationReceiverSendPushTagsCallbackPushFragmentPushEventListener 类仍作为 com.pushwoosh:pushwoosh-deprecated 库的一部分提供,但建议尽快迁移到新的 API。

PushManager

Anchor link to

PushManager 包含用于不同功能的各种方法。这些方法现在已拆分到不同的类和库中: com.pushwoosh:pushwooshPushwooshPushwooshNotificationSettingsPushwooshInAppcom.pushwoosh:pushwoosh-badgePushwooshBadgecom.pushwoosh:pushwoosh-locationPushwooshLocationcom.pushwoosh:pushwoosh-beaconPushwooshBeacon

BaseRegistrationReceiver

Anchor link to

BaseRegistrationReceiver 用于处理推送注册和取消注册事件。此接收器现在已被简单的回调机制取代:

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 通知行为。这是通过取消传入通知并调用 onMessageReceive 回调来实现的。这种方式笨拙且需要在应用变为活动状态时手动注册,在应用进入后台时取消注册。 此接收器已被 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
}
}

此扩展还用于处理通知到达和接受事件,从而取代了手动 Activity 集成中使用的所有笨拙代码。

PushFragment

Anchor link to

PushFragment 是复杂 Activity 生命周期集成的一种轻量级替代方案。但另一方面,它需要 FragmentActivity 继承并隐式使用了更复杂的 Fragment 生命周期。 PushFragmentPushEventListener 现在已被 Pushwoosh#registerForPushNotifications(Callback)NotificationServiceExtension 取代。

自定义推送广播接收器 (PW_NOTIFICATION_RECEIVER)

Anchor link to

PW_NOTIFICATION_RECEIVER 曾用于自定义用户点击通知时的行为。这允许在 Activity 上下文之外处理通知,并根据通知内容打开不同的 Activity。此集成使用了 Pushwoosh SDK 的内部 API,该 API 已不再存在。 此接收器现在已被 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);
}
}

通知工厂

Anchor link to

通知工厂也发生了一些重大变更:

1. AbsNotificationFactory 已被 NotificationFactory 取代。 2. AbsNotificationFactory#onPushReceived(PushData)AbsNotificationFactory#onPushHandle(Activity) 方法已被 NotificationServiceExtension 类(onMessageReceivedstartActivityForPushMessage)取代。 3. DefaultNotificationFactory 已被 PushwooshNotificationFactory 取代。 4. PushData 已被 PushMessage 取代。

应用内消息

Anchor link to

1. InAppFacade 已被 PushwooshInApp 取代。 2. pushwoosh 对象已引入 JavaScript 原生接口,具有以下 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() - 关闭应用内 HTML 页面。

与我们分享您的反馈

Anchor link to

您的反馈有助于我们创造更好的体验,因此如果您在 SDK 集成过程中遇到任何问题,我们很乐意听取您的意见。如果您遇到任何困难,请随时通过此表单与我们分享您的想法。