# Mini Program and Mobile Application Communication

In the Mini Program, the developer can do this by calling the xxxxxx.xxx Similar wmp.xxx Interface and Mobility Applications (WMPF) Client) communication.

The interface here is in two forms, which can be WMPF Unique capability interfaces provided (e.g. face payment, push messaging, printers, devices) SN Code, etc.), it can also be a developer custom in the Mini Program with WMPF Client Data communication between (Invoke) Channel)。 This article will focus on the second form of interface.

# 1. Mini Program end

# 1.1 Service instruction

We can do this by triggering a third party App In the form of a service directive, let WMPF Client The end implements some specific functional logic for us, and after the call is completed, the final result is returned to the Mini Program end.

Example code (asynchronous call)

wmpf.Channel.invoke({
  command: 'test', // The instruction is 'test' ,WMPF Client Perform specific functional logic upon receipt by the end
  success: function(res) {
    // WMPF Client End after the completion of the task, return the final result to the Mini Program end
    console.log(res.data)
  }
})

Example code (synchronous call)

try {
  const res = wmpf.Channel.invokeSync({
    command: 'test'
  })
  console.log(res.data)
} catch (and) {}

# 1.2 Listening events

wmpf It is also possible to passively receive from the WMPF Client The message sent here.

sample code

// First register a event to be called 'test' The events of the
wmpf.Channel.registerEvent({
  event: 'test',
  success(res) {
    console.log(res)
  }
})

const callback = (res) => {
  // WMPF Client Triggered at a specific time. event 'test' Event, sending data to the Mini Program side
  console.log(res.data)
}

// Listening in on registered 'test' event
wmpf.Channel.on('test', callback)

// Unlisten to registered 'test' event
wmpf.Channel.off('test', callback)

// Cancellation of registration event to be called 'test' The events of the
wmpf.Channel.unregisterEvent({
  event: 'test',
  success(res) {
    console.log(res)
  }
})

# 2. Client end

WMPF The role in the entire process is to pass through the entry parameters of the Mini Program and the return parameters of the client to the Mini Program, so the field names and meanings need to be determined by the Mini Program developer and the wmpf-cli Developer's Agreement. WMPF and wmpf-cli use ContentProvider Data interaction.

# 2.1 Service instruction

WMPF Each call generates a unique invoke_id And inserted into the device that passes to the wmpf-cli of ContentValues As a unique identifier for the call.

# Asynchronous call

When called in an Mini Program wmpf.Channel.invoke When, Client End needs to be used insert Operation completes the asynchronous call.

Call process

流程图

  • adopt insert(初始值//com.tencent.wmpf.cli.provider/invokeChannel) Touchdown wmpf-cli 。
  • adopt insert(content://com.tencent.wmpf.comm.provider/callbackInvokeChannel) Notification callback 。

sample code

override fun insert(p0: Uri, p1: ContentValues?): Uri? {
  when(sURIMatcher.match(p0)) {
    // Asynchronous method invocation
    CODE_CALLBACK_INVOKE_CHANNEL -> {
      val invokeId = p1?. getAsString(InvokeChannelConstants.Key.INVOKE_ID)
      val command = p1?. getAsString(InvokeChannelConstants.Key.COMMAND)
      val sourceData = p1?. getAsString(InvokeChannelConstants.Key.DATA)

      Log.i(DAY "invokeId: $invokeId, command: $command, sourceData: $sourceData")
      
      if (command.equals("test")) {
        // Implement specific functional logic
        mHandler.post({
          val cv = 初始值()
          cv.apply {
            put(InvokeChannelConstants.Key.INVOKE_ID, invokeId)
            put(InvokeChannelConstants.Key.COMMAND, command)
            put(InvokeChannelConstants.Key.DATA, "your data") // Return the result to the Mini Program.
          }
          try {
context?.contentResolver?.insert(InvokeChannelConstants.ContentProvider.Cli2WMPF .URI_CALLBACK_INVOKE_CHANNEL, cv)
          } catch (e: Exception) {
            Log.e(DAY "callback invoke channel error")
          }
        })
      }
    }
  }
  return null
}

# Synchronous call

When called in an Mini Program wmpf.Channel.invokeSync When, Client End needs to be used call Operation completes the synchronous call.

Call process

流程图

  • adopt call(content://com.tencent.wmpf.cli.provider/invokeChannel) Touchdown wmpf-cli 。
  • adopt Bundle Return the result directly.

sample code

// Synchronous call
override fun call(method: String, arg: String?, extras: Bundle?): Bundle? {
  Log.i(DAY "method: $method, arg: $arg, extras: $extras)
  // Implement specific functional logic
  val bundle = Bundle()
  bundle.putString("data", "call success, method: $method, arg: $arg, extras: $extras)
  // Return the result to the Mini Program.
  return bundle
}

# 2.2 Event listening

# Registering events and triggering events

When called in an Mini Program wmpf.Channel.registerEvent Will be notified when Client End. When the event registered by the Mini Program is triggered, the client needs to send the corresponding event to the Mini Program. wmpf.Channel.on Listening in.

Call process

流程图

  • adopt insert(content://com.tencent.wmpf.comm.provider/invokeChannelEvent) Register to listen for callbacks.
  • adopt insert(content://com.tencent.wmpf.comm.provider/notifyInvokeChannelEvent) Trigger events.

sample code

override fun insert(p0: Uri, p1: ContentValues?): Uri? {
  when(sURIMatcher.match(p0)) {
    // Callback Event Registration
    CODE_NOTIFY_INVOKE_CHANNEL_EVENT -> {
      val eventId = p1?. getAsString(InvokeChannelConstants.Key.EVENT_ID)
      val event = p1?. getAsString(InvokeChannelConstants.Key.EVENT)
      Log.i(DAY "register, eventId: $eventId, event: $event")
      mEventIdList.put(eventId, event)
    }
  }
  return null
}

// At a specific time to notify a registered event , and pass in the relevant data To the Mini Program end
private fun notifyEvent(counter: Int, eventId: String?, event: String?) {
  if (!mEventIdList.containsKey(eventId)) {
    return 
  }
  val cv = ContentValues()
  cv.apply {
    put(InvokeChannelConstants.Key.EVENT_ID, eventId)
    put(InvokeChannelConstants.Key.EVENT, event)
    put(InvokeChannelConstants.Key.DATA, "event$counter notify event success")
  }
  try {
    context.contentResolver.insert(InvokeChannelConstants.ContentProvider.Cli2WMPF .URI_NOTIFY_INVOKE_CHANNEL_EVENT, cv)
    Log.i(DAY " send message success, content: event$counter success")
  } catch (e: Exception) {
    Log.e(DAY "callback invoke channel error")
  }
}

# Deregistration event

When called in an Mini Program wmpf.Channel.unregisterEvent Will be notified when Client End, Client Should stop sending corresponding events.

Call process

流程图

and registerEvent For anti-logic, by delete Operation notification wmpf-cli The client, the point is to notify wmpf-cli Client release logic.

sample code

// Unregister callback events
override fun delete(p0: Uri, p1: String?, p2: Array<String>?): Int {
  if (p2?.size == 2) {
    val eventId = p2[0]
    val event = p2[1]
    mEventIdList.remove(eventId)
    Log.i(DAY "unregister success, eventId: $eventId, event: $event")
  }
  return 0
}