Prerequisites
Before adding the data synchronization software development kit (SDK), ensure that your project is integrated with mPaaS. For more information, see Integrate mPaaS capabilities.
Install the mppm plug-in. For more information, see Install the mppm tool.
Add the SDK
When you run mppm init and mppm sdk, select the Mobile Synchronization option.

Initialize the framework
Before using Data Synchronization, initialize the mPaaS framework:
export default class EntryAbilityStage extends AbilityStage {
async onCreate() {
const app = this.context;
MPFramework.create(app);
const instance: MPFramework = MPFramework.instance;
const ctx: Context = instance.context
}
}Import the interface
import {MPSync} from '@mpaas/sync-service'Related APIs
API name | Description |
initialize(Context context) | Initializes the interface and the data synchronization service. |
appToForeground() | Notifies the client SDK that the app has started. This establishes network connectivity with the server. Call this API each time the app returns to the foreground. |
appToBackground() | Notifies the client SDK that the app has moved to the background. This disconnects the network connection to the server. Call this API each time the app is sent to the background. |
updateUserInfo(sessionId:string) | Call this API when the logon information, such as |
clearUserInfo() | Logs off the user. |
registerBiz(biz:string, syncCallback:ISyncCallback) | Registers a |
unregisterBiz(bizType:string) | Unregisters the specified synchronization configuration. After unregistering, the client SDK no longer calls the |
reportMsgReceived(syncMessag:SyncMessage ) | After receiving data in the |
isConnected():Promise<boolean> | Checks if the data synchronization service is running normally. |
addConectionListener(listener:connectionListener) | Adds a listener for the connection status. |
removeConnectionListener(listener:connectionListener) | Removes the connection status listener. |
Usage example
export default class EntryAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
..
// Perform initialization at the start of the ability.
MPFramework.create(this.context); // Initialize
MPFramework.instance.userId = example_userid; // Set the user ID
MPSync.initialize(); // Initialize sync
..
}
}
// Page code example
connectListener:ConnectionListener = {
onConnectionStateChanged: (connectionEvent: ConnectionEvent) => {
let type = connectionEvent.getEventType();
switch (type) {
case ConnectionType.Connecting:
this.connectionMsg = "Connecting"
break;
case ConnectionType.Unknown:
this.connectionMsg = "Unknown"
break;
case ConnectionType.Connected:
this.connectionMsg = "Connected"
break;
case ConnectionType.Disconnected:
this.connectionMsg = "Disconnected"
break;
case ConnectionType.ConnectFailed:
let message = connectionEvent.getExtras();
this.connectionMsg = "Connection failed:" + message
break;
}
}
onPageShow(): void {
MPSync.appToForeground();
MPSync.addConnectionListener(this.connectListener)
}
build() {
Row() {
Column({ space: 20 }) {
Button('Initialize', { type: ButtonType.Capsule, stateEffect: true })
.onClick(() => {
let index = 1;
let callback:ISyncCallback= { // Example of a message receiving callback
onReceiveMessage:(message:SyncMessage):void=>{
let msg = message.msgData;
let obj:object[] = JSON.parse(msg);
let str = "Message received:\n";
obj.forEach(value=>{
let m:string = getMessageObjString(value);
str += "message:"+index+":"+ m+'\n'
index++
})
this.message = str
},
onReceiveCommand:(message:SyncCommand):void=>{
}
}
MPSync.registerBiz("oneUserSync",callback) // Register the biz
}).width('90%').margin({ top: 15 })
Button('Bind User', { type: ButtonType.Capsule, stateEffect: true })
.onClick(() => {
MPSync.updateUserInfo("harmony_session") // Bind the user
}).width('90%').margin({ top: 15 })
}
}
.height('100%').alignItems(VerticalAlign.Top)
}