Prerequisite: A mobile application has been created on the WeChat open platform.
reference material:
https://developers.weixin.qq.com/doc/oplatform/Mobile_App/Access_Guide/ohos.html
Apply for your AppID, configure HarmonyOS application information, and submit for review
- Go to the WeChat Open Platform, create a mobile application account in the "Management Center - Mobile Applications" section, and fill in the required Bundle ID and identifier information for the HarmonyOS application in the "Platform Information" section, as shown below. Then submit for review, and once approved, you will be granted the corresponding permissions
- If you already have a mobile application, you can click "Edit" in "Management Center - Mobile Applications - Details - Development Configuration" to enter the page for modifying development information. Continue to fill in the required Bundle ID and identifier information for the HarmonyOS application in the "Platform Information" section, then submit for review and wait for approval.
Configure SDK dependencies
Open your project in DevEco Studio NEXT, using a demo project as an example:
Modify the oh-package.json5 file in the project and add a dependency for WeChat opensdk in the dependencies:
{
"name": "demo",
"version": "1.0.0",
"description": "Please describe the basic information.",
"main": "",
"author": "",
"license": "",
"dependencies": {
"@tencent/wechat_open_sdk": "1.0.7"
}
}
Modify module configuration
Modify the module.json5 file of entry to add actions (which agree to receive data from WeChat)
"wxentity.action.open"
Continue to modify the modular.json5 file of entry and add queryPlans
"querySchemes": [
"weixin",
"wxopensdk"
]
Modify build configuration
Download the "Certificate" and "Profile" from AppGallery, and configure the "signingConfig".
Note: If the signature is not configured, when calling the WeChat SDK, the WeChat APP will prompt "Third party application information verification failed"
Add SDK tool classes
Add WXApi
import * as wxopensdk from '@tencent/wechat_open_sdk';
//对应微信开放平台的APPID
const APP_ID='APPID'
// WXApi 是第三方app和微信通信的openApi接口,其实例通过WXAPIFactory获取,需要传入应用申请到的AppID(指的是移动应用Appid,不要填小程序Appid!!!)
export const WXApi = wxopensdk.WXAPIFactory.createWXAPI(APP_ID)
Add WXEventHandler
import * as wxopensdk from '@tencent/wechat_open_sdk';
export type OnWXReq = (req: wxopensdk.BaseReq) => void
export type OnWXResp = (resp: wxopensdk.BaseResp) => void
const kTag = "WXApiEventHandlerImpl"
// WXApiEventHandler为微信数据的回调
class WXApiEventHandlerImpl implements wxopensdk.WXApiEventHandler {
private onReqCallbacks: Map<OnWXReq, OnWXReq> = new Map
private onRespCallbacks: Map<OnWXResp, OnWXResp> = new Map
registerOnWXReqCallback(on: OnWXReq) {
this.onReqCallbacks.set(on, on)
}
unregisterOnWXReqCallback(on: OnWXReq) {
this.onReqCallbacks.delete(on)
}
registerOnWXRespCallback(on: OnWXResp) {
this.onRespCallbacks.set(on, on)
}
unregisterOnWXRespCallback(on: OnWXResp) {
this.onRespCallbacks.delete(on)
}
onReq(req: wxopensdk.BaseReq): void {
wxopensdk.Log.i(kTag, "onReq:%s", JSON.stringify(req))
this.onReqCallbacks.forEach((on) => {
on(req)
})
}
onResp(resp: wxopensdk.BaseResp): void {
wxopensdk.Log.i(kTag, "onResp:%s", JSON.stringify(resp))
this.onRespCallbacks.forEach((on) => {
on(resp)
})
}
}
export const WXEventHandler = new WXApiEventHandlerImpl
frequently asked questions
- How to determine if WeChat has been installed Developers can use WXApi. isWXAppInstalled() to determine if WeChat has been installed, but they need to add the following statement in module. json 5 of the developer's App module. For details, you can check if the application is accessible using canOpenLink
- WeChat login failed: unable to log in using WeChat due to failed verification of application Bundle ID information The reason for this error message is that your mobile application has been configured with HarmonyOS development information and submitted for review, but it has not been approved yet (i.e. under review or rejected). Simply resubmit and wait for approval If the status is already approved, but the error still occurs, please check if the appid+identifier+bundleId match (can be viewed in "WeChat Open Platform - Management Center - Mobile Applications - Details - Development Configuration")
- How to receive data from WeChat when launching an app on WeChat Developers need to first configure the action wxenitity.action.open as agreed upon with WeChat Developers also need to implement the onReq function for wxopensdk-WXApiEventHandler
Top comments (0)