Harmony SDK API

Updated at:

Create an initialization configuration object

Create the initialization configuration object, which contains the input parameters for SDK initialization. The following code shows an example.

import {InitConfig} from '@aliyun/feedback';
 // Initialization parameters
const feedback_config  = new InitConfig({
    appkey:'appKey parameter',
    appSecret: 'appSecret parameter',
    backIconResource: $r('app.media.app_logo'),
    loadProgressColor: '#0dd0ff',
    isOpenLog: true,
    rightButtonFontSize: 16
  })

Parameter

Description

appKey

Specifies the AppKey of the application.

Data type: string

Required: Yes

Can be empty: No

Default value: None

appSecret

Specifies the AppSecret of the application.

Data type: string

Required: Yes

Can be empty: No

Default value: None

loadProgressColor

Sets the color of the webview loading progress bar.

Data type: string

Required: No

Default value: None

rightButtonFontSize

Sets the font size of the button on the right. The unit is fp.

Data type: number

Required: No

Default value: 16

isOpenLog

Specifies whether to enable logging.

Data type: boolean

Required: No

Default value: false

backIconResource

Sets the resource for the back icon.

Data type: Resource

Required: No

Default value: $r('app.media.ali_feedback_icon_back_white')

SDK initialization

You do not need to call the SDK initialization interface at application startup. Call it before you use the SDK.

init(context: Context, config: InitConfig)

Usage:

FeedbackAPI.init(getContext(), config)

Open the user feedback page

After you confirm that the parameter settings are correct, you can call the interface to open the user feedback page.

openFeedback(success?: () => void, fail?: (info: string, code?: number) => void)

Usage:

1. Open the feedback page:

FeedbackAPI.openFeedback()

2. Open the feedback page and provide a result callback:

FeedbackAPI.openFeedback(() => {
  // Opened successfully
}, (info, code) => {
  // Failed to open
  promptAction.showToast({ message: `code:${code},info:${info}` })
})

Get the number of unread feedback messages

Call this interface to retrieve the number of unread feedback messages.

getFeedbackUnreadCount(success: (count: number) => void, fail: () => void)

Usage:

FeedbackAPI.getFeedbackUnreadCount((count : number) => {
     promptAction.showToast({ message: "Number of unread messages: " + count })
}, () => {
     promptAction.showToast({ message: "Failed to get the number of unread messages" })
})

Set the default user contact information

Call this interface to set the default user contact information, which is then passed to the feedback page. The user can also manually change this information.

setDefaultUserContactInfo(contractInfo: string)

Usage:

FeedbackAPI.setDefaultUserContactInfo(“1300000XXXX”);

Set the user nickname

Call this interface to set the user nickname. The nickname is included with the feedback that the user sends. You can view the nickname in the feedback session in the console.

setUserNick(nickName: string)

Usage:

FeedbackAPI.setUserNick("xxx");

Set extended parameters for feedback messages

Call this interface to set extended parameters for feedback messages. This allows developers to add custom extended information.

setAppExtInfo(extInfo: string)

Usage:

let ext = `{"key":"value"}`
FeedbackAPI.setAppExtInfo(ext)
Important

The extended parameters must be a string in JSON format.

Permissions

When a user performs an authorization operation for the first time, the SDK requests system permissions.

You can register a callback listener to explain the purpose of the permissions before the SDK requests them.

setPermissionInterrupt(action: string, interrupt: IPermissionRequestInterrupt)

Usage:

let albumCallback: IPermissionRequestInterrupt = {
      interrupt: (context: Context, action: string, permissions: Permissions[],
        callback: InterruptCallback): void => {
          showDialog("Sensitive permission authorization required","Album", "Select a photo of the issue for feedback", callback)
      }
    }
FeedbackAPI.setPermissionInterrupt(FeedbackAPI.ACTION_ALBUM, albumCallback)
let cameraCallback: IPermissionRequestInterrupt = {
      interrupt: (context: Context, action: string, permissions: Permissions[],
        callback: InterruptCallback): void => {
           showDialog("Sensitive permission authorization required","Camera", "Take a photo of the issue for feedback", callback)
        }
    }
FeedbackAPI.setPermissionInterrupt(FeedbackAPI.ACTION_CAMERA, cameraCallback)
let microPhoneCallback: IPermissionRequestInterrupt = {
      interrupt: (context: Context, action: string, permissions: Permissions[],
        callback: InterruptCallback): void => {
           showDialog("Sensitive permission authorization required","Recording", "Record a voice description for feedback", callback)
       }
    }
FeedbackAPI.setPermissionInterrupt(FeedbackAPI.ACTION_MICROPHONE, microPhoneCallback)

// This is sample code. Replace it with the unified permission description style of your application.
private showDialog(title: string, permission: string, message: string, callback: InterruptCallback) {
    AlertDialog.show({
      title: title,
      message: `${permission}: ${message}`,
      autoCancel: true,
      alignment: DialogAlignment.Center,
      gridCount: 4,
      primaryButton: {
        value: 'Cancel',
        action: () => {
          callback.stopRequest()
        }
      },
      secondaryButton: {
        enabled: true,
        defaultFocus: true,
        style: DialogButtonStyle.HIGHLIGHT,
        value: 'Confirm',
        action: () => {
          callback.goOnRequest()
        }
      },
      cancel: () => {
        callback.stopRequest()
      }
    }
    )
  }

If a user denies a permission request for the first time, you can register a callback for subsequent requests. This callback can display a text prompt or redirect the user to the System Settings page.

setPermissionRationale(permissionRationale: IPermissionRationale)

Usage:

let rationaleCallback: IPermissionRationale = {
      onPermissionDenied: (permissions: Permissions[]): Promise<void> => {
        // Redirect to the settings page 
        return new Promise<void>((resolve, reject) => {
          let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
          let context: Context = getContext() as common.UIAbilityContext;
          atManager.requestPermissionOnSetting(context, permissions).then((result) => {
            resolve()
          }).catch((error: BusinessError) => {
            reject(error)
          })
        })
      }
    }
FeedbackAPI.setPermissionRationale(rationaleCallback)