# Mobile Phone WeChat Calling Device (Android)

The user can initiate audio and video calls to the device, and the device side requires the developer to pull up the specified page of Weixin Mini Program after receiving the message for the user to answer the call.

If you want to get various events during the call, you can use the plug-in onVoipEvent Interface.

# 1. Phone WeChat side to initiate a call

Before initiating a call, users typically need to select the device to call and the type of call (audio / video) in Weixin Mini Program.

When initiating a call, the developer needs to get the data obtained from the device in the background. pushToken , and in Weixin Mini Program call the plug-in's callWMPF interface, then jump to the plugin's Initiate Call page.

const wmpfVoip = requirePlugin('wmpf-voip').default

const roomType = 'video'
try {
  const { roomId, isSuccess } = await wmpfVoip.callWMPF({
    roomType: 'video', // 房间类型。voice: 音频房间;video: 视频房间
    sn: '设备 SN',
    modelId: '设备 modelId',
    pushToken: '从设备获取的 pushToken',
    nickName: '设备端显示的微信用户名称',
    deviceName: '我的学习机',
    envVersion: 'release', // 指定接听方使用的小程序版本,开发过程可以使用 develop
  })

  if (/* 当前不在插件页面 */) {
    // 跳转到插件的通话页面
    wx.redirectTo({
      url: wmpfVoip.CALL_PAGE_PATH,
      // 插件 2.3.9 开始支持 CALL_PAGE_PATH, 低版本请传入 'plugin-private://wxf830863afde621eb/pages/call-page-plugin/call-page-plugin',
    })
  }
} catch (e) {
  console.error('callWMPF failed:', e)
  // 参数错误的情况会通过异常抛出
  wx.showToast({
    title: '呼叫失败',
    icon: 'error',
  })
}

Be careful

  • Developers are advised to maintain the connection between sn and pushToken on the server side, obtain the pushToken in advance on the device side [and store it in the background, and refresh the pushToken before it expires.
  • When initiating a call, you need to ensure that the device is activated and connected online .
  • The message to the device will be sent directly by the WeChat background after calling the call interface, without requiring the developer to call the interface to send the message.
  • Plug-in version 2.4.0 below, using the initByCaller interface to call the device, passingbusinessType: 2。Calls initiated using the initByCaller interface require additional authorization from the user.

# 2. Accept calls on the device (Android)

After a call is initiated by the WeChat side of the phone, the device side will receive a WMPF push message .Requires the developer to process the notification and pull up Weixin Mini Program to show the call interface:

# 2.1 Binding message listening

Developers need to register message listening by calling registerPushMsgEventListener in WMPF. Note that this step must be taken before the call is initiated.

# 2.2 Display call notifications / reminders (optional)

After receiving the message, the developer can display the call notification according to the product needs (the style can be customized), or directly pull up Weixin Mini Program for the user to answer.

# 2.3 Open Weixin Mini Program to answer

The push message is a JSON character string, parsed and formatted as follows

{
  "path": "plugin-private://wxf830863afde621eb/pages/call-page-plugin/call-page-plugin?roomType=roomType&groupId=groupId&listenerId=设备sn&callerName=拨打方名称&customQuery字符串", // 小程序启动路径
  "appType": 0, // 0: 正式版 1: 开发版 2: 体验版
  "appid": "wx********" // 小程序appid
}

Developers need to use the above parameters to call WMPF launchMiniProgram Interface opens the answer interface for Weixin Mini Program.

Be careful

  • If the developer displays a custom incoming call notification after receiving the message, you can add [[ TAG-1-START]] &isClickedHangOnBtn=1` 。 At this point, the user enters the Mini Program and receives the call directly, without having to click the "Accept" button on the plug-in call page again.
  • WMPFLifeCycleManager Listens for WMPF exit events and restarts and activates WMPF to prevent message loss after WMPF exception exit.
  • To speed up incoming calls, it is recommended to call prefetchDeviceToken when message listening is receivedPull the device credentials in advance.
  • On your device, when the app pulls up Weixin Mini Program or receives a call slowly, refer to the Performance and Experience Optimization Guide .

# 3. The device ends processing the call

After the end of the device call, the developer needs to handle the page jump or close Weixin Mini Program.There are generally the following ways:

  • After the end of the jump to other pages: developers need to pass plugins setVoipEndPagePath The interface sets the page where the call ends jumping. When the developer does not set this up, it remains on the call log page.
  • After the end Weixin Mini Program cut background: developers can listen plug-in endVoip or finishVoip event , Invoke Channel](https://developers.weixin.qq.com/doc/oplatform/Miniprogram_Frame/invoke-channel.html) notification Mobile App, using [ closeWxaApp (keepRunning = true) Cut the Mini Program into the background.
  • Close Weixin Mini Program after the end: developers can listen to plug-in endVoip or finishVoip events and call wx.exitMiniProgram to close the Mini Program.