HMS Core Drive Kit allows your app to send file change notifications to users so that they can synchronize files in the cloud and those stored locally. To use the Changes API, you need a server that can receive changes and push them to your app in real time. After receiving the changes, the app updates local files at the request of the user.
The following figure shows how to register your server domain name with HUAWEI Drive.

Parameter | Type | Mandatory/Optional | Description |
|---|---|---|---|
X-HW-Channel-ID | varchar(255) | Mandatory | Unique user ID that you defined, which is sent to the Drive server through the Changes.Subscribe API. |
X-HW-Channel-Token | varchar(255) | Optional | User token that you defined, which is contained in the request for calling the Changes.Subscribe API and will be verified by your server. |
X-HW-Channel-Expiration | varchar(255) | Optional | Date and time when the notification channel expires. Format: Tue, 18 Jun 2019 14:21:46 GMT. |
X-HW-Resource-ID | varchar(255) | Mandatory | Resource ID, which identifies the resource being tracked on this channel. When the resource changes, a notification will be sent. |
X-HW-Resource-URI | varchar(255) | Mandatory | Version-specific identifier of a resource. |
X-HW-Resource-State | varchar(255) | Mandatory | Resource status. sync: The subscription is successful and the channel is successfully created. add: The resource is added. remove: The resource is deleted. update: The resource properties are updated. recycle: The resource is moved to the recycle bin. unrecycle: The resource is restored from the recycle bin. change: The resource is changed. |
X-HW-Message-Number | Int64 | Mandatory | Message number. A later message number is always greater than an earlier one, but the numbers may not be consecutive. The number of a sync message is always 1. |
X-HW-Changed | varchar(255) | Optional | File change details. This parameter is valid only when the value of State is update. content: The resource content is updated. properties: The resource properties are updated. parents: The parent folder of the resource is changed. Currently this value is not used. children: The files in the parent folder of the resource are changed. |
Configure your server to send a change notification to your app if files in the cloud have been updated. Drive does not limit how your server delivers the change notification. However, we recommend that you use the HUAWEI Push service, which enables your server to deliver notifications to designated apps. To use the HUAWEI Push service, you will need to integrate the HMS Core Push SDK (Push SDK for short) into your app. For details, please refer to Service Introduction. If you do not use the HUAWEI Push service, ensure that your server can implement the push function.
Configure your server to obtain startCursor and subscribe to the file change notification function through the Changes.subscribe API.
Configure your app to call the Changes.GetStartCursor API to obtain the cursor value required by the Changes.Subscribe API and then call the Changes.Subscribe API to subscribe to the file change notification function. The Changes.Subscribe API will return the subscribed Channel object and your app needs to pass channelToken in the Channel object to your server, for authenticating subsequent messages returned. In addition, you can also configure your app to perform desired operations such as updating changes and stopping file change notification based on the Channel object.
/**
* Subscribe to file change notifications.
* @param context Context.
* @param id UUID or similar unique string that identifies this channel.
*/
private void watchChanges(Context context, String id) {
try {
Drive drive = buildDrive();
StartCursor startCursor = drive.changes().getStartCursor().execute();
String cursor = startCursor.getStartCursor();
Channel content = new Channel();
// id can be customized but must be consistent with the one used in the Channels.stop API.
content.setId(id);
// Fixed value web_hook.
content.setType("web_hook");
// Server URL for receiving changes.
content.setUrl("https://addr.xxx.com:8086/path/to/webhook");
content.setExpirationTime(System.currentTimeMillis() + 300 * 1000L);
Channel channel = drive.changes().subscribe(cursor, content).execute();
// Return the channelToken to the server URL.
String channelToken = channel.getUserToken();
SharedPreferences prefs = context.getSharedPreferences("channel_config", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("startPageVersion", cursor);
editor.putString("resourceId", channel.getResourceId());
editor.commit();
} catch (Exception e) {
Log.e(TAG, "watchChanges error: " + e.toString());
}
Call the Changes.GetStartCursor API and Changes.Subscribe API in the subthread. The related operation process takes a while and requires an Internet connection.
After receiving the change notification, the app calls the Changes.List API to obtain user file changes. Note that the input parameter cursor is the one that the Changes.GetStartCursor API passed to the Changes.Subscribe API.
/**
* Obtain file changes.
*
* @param cursor Cursor passed to the Changes.Subscribe API.
*/
private List<Change> getChangesList(String cursor) {
List<Change> changes = null;
try {
Drive drive = buildDrive();
Drive.Changes.List listRequest = drive.changes().list(cursor);
listRequest.setFields("*");
ChangeList changeList = listRequest.execute();
changes = changeList.getChanges();
} catch (Exception e) {
Log.e(TAG, "doChangesList error: " + e.toString());
}
return changes;
}
Call the Changes.List API in the subthread. The process of obtaining file changes takes a while and requires an Internet connection.