Account management API (4.x)

Updated at:

This topic describes how to use the Android SDK 4.x to bind, replace, and unbind an app account on the current device.

Note

The APIs and examples in this topic are based on Mobile Push Android SDK 4.0.0. If you are using SDK version 3.x, see the corresponding 3.x API documentation.

Prerequisites

Before making an API call, initialize the SDK by calling PushServiceFactory.init() and complete push registration. Ensure the onSuccess callback of the register method is triggered. Obtain the CloudPushService instance by calling PushServiceFactory.getCloudPushService(). The examples in this topic use a non-null CommonCallback. If an operation fails, check the errorCode and errorMessage from the onFailed callback.

Bind an account (bindAccount)

Bind an app account to the current device to enable targeted push notifications to that account. A device can be bound to only one account at a time, but an account can be associated with multiple devices. To switch the account on a device, you can directly bind the new account. This action overwrites the existing binding, eliminating the need to first unbind the old account.

API definition

void bindAccount(String account, CommonCallback callback);

Class

CloudPushService

Parameters

Parameter

Type

Required

Description

account

String

Yes

The app account. Use a stable, unique identifier from your app's account system. This value cannot be null or empty and must not exceed 64 bytes.

callback

CommonCallback

No

An optional callback to handle the operation result. We recommend providing this to receive success or failure notifications.

Return value and callback

This method returns void. The onSuccess(String response) callback is triggered on successful binding. The onFailed(String errorCode, String errorMessage) callback is triggered on failure, providing an error code and error message.

Code example

CloudPushService pushService = PushServiceFactory.getCloudPushService();
pushService.bindAccount("app_user_001", new CommonCallback() {
    @Override
    public void onSuccess(String response) {
        Log.i("PushDemo", "bindAccount success");
    }

    @Override
    public void onFailed(String errorCode, String errorMessage) {
        Log.e("PushDemo", "bindAccount failed: " + errorCode + ", " + errorMessage);
    }
});
val pushService = PushServiceFactory.getCloudPushService()
pushService.bindAccount("app_user_001", object : CommonCallback {
    override fun onSuccess(response: String?) {
        Log.i("PushDemo", "bindAccount success")
    }

    override fun onFailed(errorCode: String?, errorMessage: String?) {
        Log.e("PushDemo", "bindAccount failed: $errorCode, $errorMessage")
    }
})

Unbind an account (unbindAccount)

Disassociates the currently bound account from the device. You can call this API when a user logs out. This operation does not delete the app account or affect its bindings on other devices.

API definition

void unbindAccount(CommonCallback callback);

Class

CloudPushService

Parameters

Parameter

Type

Required

Description

callback

CommonCallback

No

An optional callback to handle the operation result. We recommend providing this to receive success or failure notifications.

Return value and callback

This method returns void. The onSuccess(String response) callback is triggered on successful unbinding. The onFailed(String errorCode, String errorMessage) callback is triggered on failure, providing an error code and error message.

Code example

CloudPushService pushService = PushServiceFactory.getCloudPushService();
pushService.unbindAccount(new CommonCallback() {
    @Override
    public void onSuccess(String response) {
        Log.i("PushDemo", "unbindAccount success");
    }

    @Override
    public void onFailed(String errorCode, String errorMessage) {
        Log.e("PushDemo", "unbindAccount failed: " + errorCode + ", " + errorMessage);
    }
});
val pushService = PushServiceFactory.getCloudPushService()
pushService.unbindAccount(object : CommonCallback {
    override fun onSuccess(response: String?) {
        Log.i("PushDemo", "unbindAccount success")
    }

    override fun onFailed(errorCode: String?, errorMessage: String?) {
        Log.e("PushDemo", "unbindAccount failed: $errorCode, $errorMessage")
    }
})

Result verification

Check the onSuccess or onFailed callback to determine the operation result. After a successful binding, ensure that you have configured the necessary Android notification permissions and third-party channels. You can then test the setup by sending a notification to the target account by using the corresponding AppKey.

References

For the integration process, see Android SDK Integration (4.x).

If you are using SDK version 3.x, see Account Management API (3.x).