Cross-platform communication method and device, equipment and storage medium
By determining the target interaction tasks and communication interfaces on the business side and adapting to the cross-platform native side based on preset rules, the problem of poor portability of communication between the business side and the native side is solved, and the efficiency and stability of cross-platform communication is achieved , reducing development complexity and cost.
Patent Information
- Application Number
- CN202411086607.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-08
- Publication Date
- 2024-12-27
AI Technical Summary
The communication methods between the existing business end and the native end have poor portability when crossing multi-platforms, resulting in difficult code reuse, high development complexity, high cost and low efficiency.
By determining the target interaction tasks and communication interfaces of the service end, adapting to the cross-platform native end based on preset communication rules, establishing cross-platform communication connections to achieve efficient communication between the service end and the native end.
Maintaining a consistent functional experience on different native platforms improves the efficiency and stability of cross-platform communication and reduces the difficulty and cost of development and maintenance.
Smart Images

Figure CN119202041A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of Internet technology, and in particular to a cross-platform communication method, device, equipment and storage medium. Background Art
[0002] With the development of Internet technology and the popularization of various communication devices, the demand for interaction between business-side web pages and native-side platforms is growing, and an efficient and unified communication mechanism is needed to realize data exchange and function calls between different native-side platforms of business-side web pages. Common native terminals include Web, Windows, Electron, iOS, and Android. Through the communication interaction between the business end and the native terminal, the business end can call the software services and hardware resources of the native terminal, and notify and subscribe to the native terminal on the business end, and carry out corresponding application development and maintenance on the business end, expand the functions and application scenarios of the business end, and improve the richness and convenience of user operation functions.
[0003] However, the current methods of interaction between business-side web pages and native-side platforms are usually developed based on a single platform. When communicating across multiple platforms, it is necessary to consider the differences in multi-platform host communication, and to have an in-depth understanding of each platform's communication mechanism, security rules, error handling methods, performance optimization methods, and compatibility differences between different browsers, so as to set up corresponding functional modules for different platforms. However, due to the technical differences and communication methods between different native platforms, interactive functions developed based on a single platform are often difficult to directly port to other platforms, and have poor portability. When communicating across multiple platforms, it is difficult to maintain a consistent functional experience on different platforms, which increases the difficulty of code reuse and cross-platform development, increases the complexity of development and maintenance, and is costly and inefficient. Summary of the invention
[0004] The main purpose of the present invention is to provide a cross-platform communication method, device, equipment and storage medium, aiming to solve the technical problem of poor portability of the existing communication method between the service end and the native end.
[0005] A first aspect of the present invention provides a cross-platform communication method, which includes: determining a target interaction task of a service end and a communication interface corresponding to the target interaction task; based on preset communication rules, determining a cross-platform native end corresponding to the target interaction task, and adapting the communication interface to the cross-platform native end; in response to the service end calling the communication interface, establishing a cross-platform communication connection between the service end and the corresponding cross-platform native end to execute the target interaction task.
[0006] Optionally, in a first implementation method of the first aspect of the present invention, the cross-platform native end corresponding to the target interaction task is determined based on preset communication rules, and the communication interface is adapted to the cross-platform native end, including: serializing the parameters of the target interaction task to convert the parameters into JSON format; determining the cross-platform native end corresponding to the target interaction task based on environment variables and / or user agents; determining a module name for the interaction between the communication interface and the cross-platform native end, wherein the module name is provided by the cross-platform native end; determining a preset communication mechanism corresponding to the cross-platform native end, and adapting the communication interface to the cross-platform native end based on the preset communication mechanism.
[0007] Optionally, in a second implementation method of the first aspect of the present invention, the preset communication mechanism corresponding to the cross-platform native end is determined, and the communication interface is adapted to the cross-platform native end based on the preset communication mechanism, including: if the cross-platform native end is an iOS end, the communication method between the web page and the host application is adopted to adapt the communication interface to the cross-platform native end; if the cross-platform native end is a Windows end, the communication method of the system intercepting custom requests is adopted to adapt the communication interface to the cross-platform native end; if the cross-platform native end is an Electron end or an Android end, the communication method of injecting global variables is adopted to adapt the communication interface to the cross-platform native end; if the cross-platform native end is a Web end, the communication method of accessing the parent window global object is adopted to adapt the communication interface to the cross-platform native end.
[0008] Optionally, in a third implementation manner of the first aspect of the present invention, the target interaction task includes at least one of calling a native interface, notifying a business event, and subscribing to a native event, and the communication interface includes at least one of a calling interface, a notification interface, and a subscription interface; the establishing a cross-platform communication connection between the business end and the corresponding cross-platform native end in response to the business end calling the communication interface to execute the target interaction task includes: in response to the business end calling the calling interface, establishing a cross-platform communication connection between the business end and the corresponding cross-platform native end to execute the business end calling a native interface to the cross-platform native end; and / or, in response to the business end calling the notification interface, establishing a cross-platform communication connection between the business end and the corresponding cross-platform native end to execute the business end notifying the cross-platform native end of a business event; and / or, in response to the business end calling the subscription interface, establishing a cross-platform communication connection between the business end and the corresponding cross-platform native end to execute the business end subscribing to the cross-platform native end for a native event.
[0009] Optionally, in a fourth implementation manner of the first aspect of the present invention, the calling native interface includes calling parameters, a calling target and a calling callback function, and the response to the business end calling the calling interface, establishing a cross-platform communication connection between the business end and the corresponding cross-platform native end to execute the business end calling the native interface to the cross-platform native end, including: in response to the business end calling the calling interface, establishing a cross-platform communication connection between the business end and the cross-platform native end, and generating a callback identifier and sending the callback identifier to the cross-platform native end, so that the cross-platform native end is associated with the calling callback function; in response to the cross-platform native end feeding back feedback information with the callback identifier, passing the calling parameters and the calling target to the cross-platform native end to call the native interface to the cross-platform native end.
[0010] Optionally, in a fifth implementation manner of the first aspect of the present invention, the notification business event includes notification event content and notification parameters, and in response to the business end calling the notification interface, a cross-platform communication connection is established between the business end and the corresponding cross-platform native end to execute the business end notifying the cross-platform native end of the business event, including: in response to the business end calling the notification interface, a cross-platform communication connection is established between the business end and the corresponding cross-platform native end, and the notification event content and the notification parameters are passed to the cross-platform native end to notify the cross-platform native end of the business event.
[0011] Optionally, in a sixth implementation method of the first aspect of the present invention, the subscribing to native events includes monitoring native events and receiving native notifications, the monitoring native events includes monitoring events and monitoring callback functions, and the receiving native notification events includes feedback notification events and feedback notification data; the response to the business end calling the subscription interface, establishing a cross-platform communication connection between the business end and the corresponding cross-platform native end to execute the business end to subscribe to the cross-platform native end for native events, includes: in response to the business end calling the subscription interface, establishing a cross-platform communication connection between the business end and the corresponding cross-platform native end, and passing the monitoring event and the monitoring callback function to the cross-platform native end to monitor native events to the cross-platform native end; receiving the feedback notification event and the feedback notification data fed back by the cross-platform native end based on a global object.
[0012] The second aspect of the present invention also provides a cross-platform communication device, which includes: a determination module, used to determine the target interaction task of the service end and the communication interface corresponding to the target interaction task; an adaptation module, used to determine the cross-platform native end corresponding to the target interaction task based on preset communication rules, and adapt the communication interface to the cross-platform native end; a communication module, used to establish a cross-platform communication connection between the service end and the corresponding cross-platform native end in response to the service end calling the communication interface, so as to execute the target interaction task.
[0013] The third aspect of the present invention also provides a computer device, which includes: a memory and at least one processor, wherein the memory stores instructions; the at least one processor calls the instructions in the memory so that the computer device executes the cross-platform communication method as described above.
[0014] A fourth aspect of the present invention further provides a computer-readable storage medium, on which instructions are stored, and when the instructions are executed by a processor, the cross-platform communication method as described above is implemented.
[0015] The embodiment of the present invention provides a cross-platform communication method, device, equipment and storage medium, which first determines the target interaction task of the business end and the communication interface corresponding to the target interaction task, and then determines the cross-platform native end corresponding to the target interaction task based on the preset communication rules, and adapts the communication interface to the cross-platform native end, so that it can respond to the business end calling the communication interface and establish a cross-platform communication connection between the business end and the corresponding cross-platform native end to execute the corresponding target interaction task. Through this cross-platform communication method, the communication differences between the business end web page and different native end platforms can be smoothed out, and cross-platform communication with different platforms can be achieved by calling the communication interface according to the target interaction task, so that the business end can call the function of the cross-platform native end, and realize the target interaction tasks such as notification and subscription of the business end to the cross-platform native end, which is suitable for different native end platforms and can maintain a consistent functional experience on different native end platforms, greatly improving the portability of the project, effectively improving the efficiency and stability of cross-multi-platform communication, and reducing the difficulty and cost of business end development and maintenance. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] Figure 1 This is a schematic diagram of a first embodiment of a cross-platform communication method in an embodiment of the present invention;
[0017] Figure 2 For the present invention Figure 1 A schematic diagram of a flow chart of an embodiment of Embodiment 102;
[0018] Figure 3 For the present invention Figure 1 A schematic diagram of a flow chart of an embodiment of Embodiment 103;
[0019] Figure 4 This is a schematic diagram of functional modules of an embodiment of a cross-platform communication device according to an embodiment of the present invention;
[0020] Figure 5 The figure is a functional module diagram of an embodiment of a computer device in an embodiment of the present invention. DETAILED DESCRIPTION
[0021] The terms "first", "second", "third", "fourth", etc. (if any) in the specification and claims of the present invention and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchanged where appropriate, so that the embodiments described herein can be implemented in an order other than that illustrated or described herein. In addition, the terms "including" or "having" and any variations thereof are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units that are clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0022] For ease of understanding, the specific process of the cross-platform communication method in the embodiment of the present invention is described below. The cross-platform communication method of this embodiment is applied to an electronic device, which may be a terminal, such as a smart phone, a laptop, a tablet computer, a desktop computer, etc., or a development board, a chip system, etc. Please refer to Figure 1 , Figure 1 This is a schematic diagram of a first embodiment of a cross-platform communication method in an embodiment of the present invention. In this embodiment, the cross-platform communication method includes:
[0023] 101. Determine a target interaction task of a service end and a communication interface corresponding to the target interaction task;
[0024] In this embodiment, the service end is an end used to receive and execute the target interactive task, which is usually presented to the user in the form of a web page. The target interactive task is an interactive service configured on the service end based on the communication connection between the service end and the cross-platform native end. For example, the target interactive task is usually intended to call certain functions of the native end through the service end, notify the native end through the service end, monitor the native end events through the service end, or receive event notifications sent by the native end to the service end through the service end. For different target interactive tasks, corresponding different communication interfaces are usually set. These communication interfaces are used to establish a communication connection between the service end and the cross-platform native end and transmit the data and instructions required for the target interactive task.
[0025] In an optional embodiment, the target interaction task includes at least one of calling a native interface, notifying a business event, and subscribing to a native event, and the communication interface includes at least one of a calling interface, a notification interface, and a subscription interface.
[0026] In this optional embodiment, the target interaction task of calling the native interface usually involves the function or resource of the cross-platform native end that the business end needs to call. In this case, the business end will determine a corresponding calling interface and send a calling request to the cross-platform native end through this interface. After receiving the request, the cross-platform native end will execute the corresponding function and return the result to the business end. The target interaction task of notifying the business event means that the business end needs to send a certain notification to the cross-platform native end to inform the native end of the state of the business end or the occurrence of certain events. In order to achieve this goal, the business end will determine a notification interface and send a notification through this interface. After receiving the notification, the cross-platform native end can execute the corresponding processing logic. The target interaction task of subscribing to native events means that the business end hopes to monitor certain events of the cross-platform native end and execute the corresponding processing logic when these events occur. For this, the business end will determine a subscription interface and send a subscription request to the cross-platform native end through this interface. After receiving the subscription request, the cross-platform native end will send a notification to the business end when the corresponding event occurs.
[0027] 102. Based on the preset communication rules, determine the cross-platform native terminal corresponding to the target interactive task, and adapt the communication interface to the cross-platform native terminal;
[0028] In this embodiment, the preset communication rule is a rule for determining the cross-platform native end corresponding to the target interactive task and adapting the corresponding cross-platform native end, wherein the cross-platform native end refers to one of multiple adaptable native end platforms, such as one of the native end platforms such as Web, Windows, Electron, iOS, and Andriod. Determine the cross-platform native end corresponding to the target interactive task, that is, determine the native end where the target interactive task is executed. The cross-platform native end is usually the native end corresponding to the native system run by the business end, or the native end specially specified by the target interactive task. The adaptation between the communication interface and the cross-platform native end is to enable the communication connection with the native end faster after the business end calls the communication interface and passes in the relevant data or instructions, and selects a communication scheme that conforms to the communication mechanism of the cross-platform native end, so as to more effectively and reliably execute the corresponding target interactive task. After the adaptation based on the preset communication rule, when the business end needs to perform the target interactive task, it only needs to call the corresponding communication interface to establish a communication connection with the cross-platform native end, thereby executing the corresponding target interactive task.
[0029] In an alternative embodiment, see Figure 2 , based on the preset communication rules, determine the cross-platform native end corresponding to the target interaction task, and adapt the communication interface to the cross-platform native end, including:
[0030] 1021. Serialize the parameters of the target interactive task to convert the parameters into JSON format;
[0031] 1022. Determine a cross-platform native terminal corresponding to the target interactive task based on the environment variable and / or the user agent;
[0032] 1023. Determine a module name for interaction between the communication interface and the cross-platform native end, wherein the module name is provided by the cross-platform native end;
[0033] 1024. Determine a preset communication mechanism corresponding to the cross-platform native end, and adapt the communication interface to the cross-platform native end based on the preset communication mechanism.
[0034] In this optional embodiment, serializing the parameters of the target interactive task can ensure the consistency and readability of the data during the transmission process, wherein the data in JSON (JavaScript Object Notation) JSON format is a lightweight data exchange format, usually a serialized object or array, and by converting the parameters into JSON format, the parameters of the target interactive task can be parsed and used in different platforms, data structures and language forms, making the parameters more compatible, complete and portable, easy to understand and operate, and can effectively improve the efficiency, flexibility and reliability of cross-platform communication.
[0035] In this optional embodiment, environment variables (Environment Variables) and user agents (User Agent, UA) are common identification information in the native system, where environment variables are variables used to define the program running environment in the native system, and their values can be referenced during program operation and can be used to obtain information about the native system, while user agents are special string headers sent to the server by the browser or other client used by the user, and can be used to identify and distinguish different native systems. Through environment variables and / or user agents, the cross-platform native end corresponding to the business end can be more accurately determined, which facilitates the subsequent adaptation of the business end to the cross-platform native end.
[0036] In this optional embodiment, the module name for the communication interface to interact with the cross-platform native end is determined in order to establish an accurate communication connection between the business end and the native end. The module name is usually provided by the native end and is used to identify a specific module or component in the native end that can communicate with the business end. Only through agreement and native interaction can the communication between the communication interface and the different functions of the cross-platform native end be associated. For example, for the two modules window.webkit.messageHandlers.publishHandler and window.webkit.messageHandlers.invokeHandler agreed upon by the iOS platform, it can be agreed that publishHandler is responsible for event interaction and invokeHandler is responsible for interface interaction. By determining the correct module name, it can be ensured that the communication interface can correctly call the functions of the cross-platform native end, send notifications or subscribe to events, thereby ensuring the accuracy and reliability of cross-platform communication.
[0037] In this optional embodiment, the preset communication mechanism corresponding to the cross-platform native terminal is determined in order to select a suitable communication scheme to ensure compatibility and stability between the communication interface and the cross-platform native terminal. Among them, the preset communication mechanism is usually a set of communication rules and protocols pre-set according to factors such as the type of the native terminal, the operating system, and the network environment. Different preset communication mechanisms need to be adopted for different native terminal platforms. By determining the preset communication mechanism corresponding to the cross-platform native terminal, the communication scheme that best matches the cross-platform native terminal can be selected, thereby improving the efficiency and stability of cross-platform communication.
[0038] In an optional embodiment, determining a preset communication mechanism corresponding to the cross-platform native terminal, and adapting the communication interface to the cross-platform native terminal based on the preset communication mechanism specifically includes:
[0039] (1) If the cross-platform native end is an iOS end, the communication method between the web page and the host application is adopted to adapt the communication interface to the cross-platform native end;
[0040] (2) If the cross-platform native end is a Windows end, the communication method of the system intercepting the custom request is adopted to adapt the communication interface to the cross-platform native end;
[0041] (3) If the cross-platform native end is an Electron end or an Android end, a communication method of injecting global variables is used to adapt the communication interface to the cross-platform native end;
[0042] (4) If the cross-platform native end is a Web end, a communication method of accessing the global object of the parent window is adopted to adapt the communication interface to the cross-platform native end.
[0043] In this optional embodiment, when the cross-platform native end is the iOS end, since the iOS end has strict restrictions on the execution of the content of the business end web page, the web content will usually run in the context of a host application such as WKWebView or UIWebView. In this case, the communication method between the web page and the host application can be adopted. For example, on the iOS end, the proxy method of the WKScriptMessageHandler protocol can be used, and the window.webkit.messageHandlers module is used in the communication interface to achieve the adaptation of the cross-platform native end and the iOS end, so that the business end can communicate with the iOS end through the communication interface.
[0044] In this optional embodiment, when the cross-platform native end is the Windows end, due to the openness and flexibility of the Windows end, it can run more custom operations. In the Windows environment, when the business end needs to communicate with the native end, a communication method in which the system intercepts custom requests can be used. This method requires triggering a corresponding custom request on the business end, and the cross-platform native end intercepts the request and parses the URL (Uniform Resource Locator) field information in the request, and then adapts the communication interface to the Windows native end based on the parsing protocol and URL field information agreed upon by the business end, so that the business end can communicate with the Windows end through the communication interface.
[0045] In this optional embodiment, when the cross-platform native end is the Electron end or the Android end, since both the Electron end and the Android end support the JavaScript operating environment, communication can be carried out by injecting global variables. For example, in Electron, the preload capability of the platform WebView component can be used to input a global custom module into JavaScript for communication. The communication capability is provided based on the ipcRenderer module. Through the IPC (Inter-Process Communication) mechanism between the main process and the rendering process, specific global variables or objects are injected into the rendering process, thereby allowing the business end code to access these global variables or objects through the communication interface to adapt to the native end, so that communication with the Electron end can be achieved through the communication interface at the business end. Similarly, on the Android end, the addJavaScriptInterface interface provided by the WebView component can be used to expose the methods or objects of the native end to the JavaScript environment, so as to achieve the adaptation of the communication interface to the cross-platform native end, so that communication with the Android end can be achieved through the communication interface at the business end.
[0046] In this optional embodiment, when the cross-platform native end is a Web end, since the business end itself runs in a Web environment, a communication method of accessing the parent window global object can be used. This method is usually applicable to the case where the parent window page and the business end page are in the same domain, and the business end is embedded in other Web pages. In this case, the parent-level custom communication module can be accessed by accessing parent.window using the communication interface, so as to adapt to the parent window or other nested Web pages, so that the business end can communicate with the Web end through the communication interface.
[0047] In this optional embodiment, it should be noted that the method of determining the preset communication mechanism corresponding to the cross-platform native end and adapting the communication interface to the cross-platform native end based on the preset communication mechanism is not limited to the application in the above-mentioned native platforms. The cross-platform native end can also be other mobile operating system native ends such as HarmonyOS, HyperOS, Windows Phone, etc., or desktop operating system native ends such as Linux, macOS, etc., and no further details will be given on this, as long as it can be adapted and communicated according to the development mode agreed upon by different native end platforms.
[0048] 103. In response to the service end calling the communication interface, a cross-platform communication connection is established between the service end and the corresponding cross-platform native end to execute the target interaction task.
[0049] In this embodiment, after determining the preset communication mechanism corresponding to the cross-platform native end and successfully adapting the communication interface to the cross-platform native end, the business end can establish a cross-platform communication connection with the corresponding cross-platform native end by calling this adapted communication interface, so as to be able to perform the corresponding target interaction task, ensuring that the business end and the native end can exchange information and data efficiently and stably. In this process, the establishment of the cross-platform communication connection is automatic, and the user does not need to care about the underlying communication details. It only needs to call the communication interface on the business end, configure the relevant parameters, and then wait for the execution result of the target interaction task, which greatly improves the development efficiency and user experience, making data sharing and function calls between the business end and the cross-platform native end simpler and more convenient. In addition, the cross-platform communication connection can adopt appropriate encryption and authentication mechanisms, and strictly control the authority of the communication interface to ensure the stability and security of cross-platform communication. This embodiment does not make specific restrictions on this security mechanism.
[0050] In an alternative embodiment, see Figure 3 , in response to the service end calling the communication interface, establishing a cross-platform communication connection between the service end and the corresponding cross-platform native end to perform the target interaction task, including:
[0051] 1031. In response to the service end calling the calling interface, establishing a cross-platform communication connection between the service end and the corresponding cross-platform native end, so as to execute the service end calling the native interface to the cross-platform native end; and / or
[0052] 1032. In response to the service end calling the notification interface, establishing a cross-platform communication connection between the service end and the corresponding cross-platform native end, so as to execute the service end notifying the cross-platform native end of the service event; and / or
[0053] 1033. In response to the business end calling the subscription interface, a cross-platform communication connection is established between the business end and the corresponding cross-platform native end to execute the business end to subscribe to the native event from the cross-platform native end.
[0054] In this optional embodiment, when it is necessary to call the native function provided by the cross-platform native end, the business end will call the adapted communication interface to trigger the establishment of a communication connection with the native end. After receiving the call request from the business end, the native end will execute the corresponding function according to the native interface specified in the request, and return the execution result to the business end. This method allows the business end to use the rich functions provided by the native end, such as file operations, system settings, hardware control, etc., thereby improving the functionality and user experience of the business end. At the same time, when certain important events occur on the business end, such as user clicks, data updates, etc., the business end can send notifications to the native end by calling the communication interface. After receiving these notifications, the native end can perform corresponding processing according to the business logic, such as updating the UI, triggering callback functions, etc. This communication method realizes real-time interaction between the business end and the native end, ensuring the correctness and integrity of the business logic. In addition, if it is necessary to monitor or process certain events generated by the native end, such as system notifications, hardware status changes, etc., the business end can call the communication interface to subscribe to these events to the native end. When these events occur, the native end will actively send a notification to the business end. After receiving the notification, the business end can perform corresponding processing, so that the business end can perceive the state changes of the native end in real time and make corresponding responses and operations.
[0055] In this optional embodiment, by providing a variety of communication interfaces and interaction methods, the communication between the business end and the cross-platform native end is made more flexible and efficient. Whether calling a native interface, notifying a business event, or subscribing to a native event, it can be achieved through the adapted communication interface, thereby simplifying the development process and improving development efficiency. At the same time, this communication method also ensures the correctness and integrity of the business logic, allowing cross-platform applications to run stably and efficiently on various native platforms.
[0056] In an optional embodiment, calling the native interface includes calling parameters, calling targets, and calling callback functions. In response to the service end calling the calling interface, a cross-platform communication connection is established between the service end and the corresponding cross-platform native end to execute the service end calling the native interface to the cross-platform native end, including:
[0057] (1) In response to the service end calling the calling interface, a cross-platform communication connection is established between the service end and the cross-platform native end, and a callback identifier is generated and sent to the cross-platform native end, so that the cross-platform native end associates and calls the callback function.
[0058] (2) In response to the feedback information with the callback identifier fed back by the cross-platform native end, the calling parameter and the calling target are passed to the cross-platform native end to call the native interface to the cross-platform native end.
[0059] In this optional embodiment, calling the interface of the cross-platform native end usually involves complex parameter passing and callback parameter processing. Therefore, for the target interaction task of calling the native interface, a configuration including calling parameters, calling targets and calling callback functions can be preset, wherein the calling parameters are the configuration parameters required for calling the native interface, the calling target is the name or identifier of the called native interface or a native method, and the calling callback function is used to trigger the callback after the call, so that the callback carries the result of the call to understand the implementation of calling the native interface.
[0060] In this optional embodiment, when the service end calls the call interface, the interface generates a callback identifier and passes it to the cross-platform native end. The callback identifier is equivalent to a label, which is used to identify and associate a specific callback function in the subsequent interface call process of the cross-platform native end. After receiving this callback identifier, the cross-platform native end will associate it with the corresponding callback function to ensure that the function specified by the service end can be accurately called back after the call is completed. When the cross-platform native end is ready to receive the call parameters and the call target, it will send a feedback message with a callback identifier to the service end. This feedback information is used to indicate that the service end can pass the call parameters and the call target to the native end. After receiving this feedback information, the service end will send the previously prepared call parameters and call targets to the cross-platform native end, thereby triggering the call of the native interface. In this way, the service end can easily call various native interfaces provided by the cross-platform native end to realize complex functions and operations. At the same time, due to the introduction of the callback identifier, the system can also ensure that the function specified by the service end can be accurately called back after the call is completed, thereby ensuring the integrity and correctness of the entire call process.
[0061] In this optional embodiment, specifically, the calling interface can be a specific function interface bridge.invoke(command, params, callback), through which communication and interaction with the native can be carried out directly. Among them, command is the calling target, params is the calling parameter, and callback is the calling callback function. When the calling interface is called, a callbackId will be generated inside it, that is, the callback identifier is passed to the cross-platform native end. The callbackId will be stored in a map object, associated with the incoming call callback function, and the callbackId will be sent to the cross-platform native end. After the cross-platform native end processes the calling logic of the native interface, it will send the corresponding result and the corresponding callbackId to the calling interface. After the calling interface obtains the callbackId, it can find the stored callback function in the map object, and then call the callback function, pass the parameters returned by the cross-platform native end into the callback function, and delete the callback function stored in the map object, thereby realizing the call to the native interface. Accordingly, the calling interface can set a corresponding internal function interface, bridge.invokeHandler (command, params, callbackId), so that the calling interface can be adapted to the cross-platform native end according to the preset communication rules, thereby smoothing out the differences between different native platforms. It should be noted that the above-mentioned function interfaces invoke, invokeHandler and the parameter names of the functions are customized, and different function names and parameter names can be used according to different tasks and naming preferences, as long as the functions of the calling interface in this embodiment can be implemented.
[0062] In an optional embodiment, the notification service event includes notification event content and notification parameters, and in response to the service end calling the notification interface, a cross-platform communication connection is established between the service end and the corresponding cross-platform native end to execute the service end notifying the cross-platform native end of the service event, including:
[0063] In response to the service end calling the notification interface, a cross-platform communication connection is established between the service end and the corresponding cross-platform native end, and notification event content and notification parameters are passed to the cross-platform native end to notify the cross-platform native end of the service event.
[0064] In this optional embodiment, the notification from the business end to the cross-platform native end usually includes the transmission of the notification event content and the configuration of the notification parameters. The notification interface can receive the notification event content and the notification parameters as output. When the business end calls the notification interface, the interface will send this information to the cross-platform native end. After receiving this information, the native end will parse and process this information, thereby making some adaptive adjustments based on the notification content. In addition, after processing the notification event, the cross-platform native end can selectively send a feedback message to the business end to inform the business end of the processing of the notification event. This feedback message can be a simple success or failure status code, or it can be a structure containing more detailed information, so that the business end can also understand the processing progress and results of the notification event in real time, so as to make corresponding adjustments and optimizations.
[0065] In this optional embodiment, specifically, the notification interface can be a specific function interface bridge.publish(event,params), through which an event notification can be made to the cross-platform native end. Among them, event is the content of the notification event, params is the notification parameter, and when the notification interface is called, a communication connection between the business end and the cross-platform native end will be established inside it, and the notification event content and notification parameters will be directly sent to the cross-platform native end. After receiving this information, the cross-platform native end will perform corresponding processing according to the content of the notification event, and at the same time, make adaptive adjustments in combination with the notification parameters to meet the needs of the business end. Accordingly, the notification interface can set a corresponding internal function interface, bridge.publishHandler(event,params), so that the notification interface can be adapted to the cross-platform native end according to the preset communication rules, thereby smoothing the differences between different native platforms. It should be noted that the above-mentioned function interfaces publish, publishHandler and the parameter names of the function are customized, and different function names and parameter names can be used according to different tasks and naming preferences, as long as the function of the notification interface in this embodiment can be realized.
[0066] In an optional embodiment, subscribing to a native event includes monitoring a native event and receiving a native notification, monitoring a native event includes monitoring an event and a monitoring callback function, and receiving a native notification event includes feedback notification events and feedback notification data. In response to the service end calling a subscription interface, a cross-platform communication connection is established between the service end and the corresponding cross-platform native end to execute the service end to subscribe to the native event from the cross-platform native end, including:
[0067] (1) In response to the service end calling the subscription interface, a cross-platform communication connection is established between the service end and the corresponding cross-platform native end, and a monitoring event and a monitoring callback function are transmitted to the cross-platform native end to monitor the native event of the cross-platform native end;
[0068] (2) Receive feedback notification events and feedback notification data from the cross-platform native end based on the global object.
[0069] In this optional embodiment, subscribing to native events includes actively monitoring native events and passively receiving native notifications. Among them, when monitoring native events, the monitoring event and the monitoring callback function are used as input. When the business end calls the subscription interface, the interface will send this information to the cross-platform native end, and the cross-platform native end will perform relevant settings for event monitoring after receiving this information. Once the native end detects that an event matching the monitoring event occurs, the monitoring callback function will be triggered immediately, and the event-related data will be passed as a parameter to the callback function. The business end can obtain the data of these events through the callback function and perform corresponding processing. When a specific event occurs at the native end, it can also actively send notifications to the business end. These notifications usually include feedback notification events and feedback notification data. In order to receive these notifications, the business end needs to accept them based on a global object. When the native end sends a notification, these data will be sent to the global object. The business end can obtain these notification data in real time by receiving the global object, and perform corresponding processing according to the notification content.
[0070] In this optional embodiment, specifically, the native event can be monitored through window.hybridBridge.subscribe(event, callback), where event is the monitored event and callback is the monitored callback function. When the subscription interface is called, a communication connection between the business end and the cross-platform native end will be established inside it, and the monitored event and the monitored callback function will be sent to the cross-platform native end. After receiving this information, the native end will perform relevant settings for event monitoring and trigger the monitoring callback function when the event occurs. In order to receive native notifications, the business end can define a global object as a container for notification reception, for example, it can be performed through window.hybridBridge.subscribeHandler(event, data) to operate the global object of js context, where event is the feedback notification event and data is the feedback notification data. When sending a notification, the cross-platform native end will send the notification data to the global object. By obtaining the changes of the global object, the business end can obtain the notification data of the cross-platform native end in real time, and realize the native event subscription of the business end to the cross-platform native end.
[0071] This embodiment can smooth out the communication differences between the business-side web pages and different native-side platforms. Cross-platform communication with different platforms can be achieved by calling the communication interface according to the target interaction task, so that the business side can call the functions of the cross-platform native side, and realize the target interaction tasks such as notification and subscription of the business side to the cross-platform native side. It is suitable for different native-side platforms and can maintain a consistent functional experience on different native-side platforms, which greatly improves the portability of the project, effectively improves the efficiency and stability of cross-platform communication, and reduces the difficulty and cost of business-side development and maintenance.
[0072] In order to execute the corresponding steps in the above method embodiment and each possible implementation method, the following provides an implementation method of a cross-platform communication device. Figure 4 , Figure 4 This is a functional module diagram of an embodiment of a cross-platform communication device in an embodiment of the present invention. In this embodiment, the cross-platform communication device is applied to an electronic device, and the cross-platform communication device includes:
[0073] A determination module 201, used to determine a target interaction task of a service end and a communication interface corresponding to the target interaction task;
[0074] An adaptation module 202, configured to determine the cross-platform native end corresponding to the target interactive task based on a preset communication rule, and adapt the communication interface to the cross-platform native end;
[0075] The communication module 203 is used to establish a cross-platform communication connection between the service end and the corresponding cross-platform native end in response to the service end calling the communication interface to execute the target interaction task.
[0076] In an optional embodiment, the target interaction task specifically includes: at least one of calling a native interface, notifying a business event, and subscribing to a native event, and the communication interface specifically includes: at least one of a calling interface, a notification interface, and a subscription interface.
[0077] In an optional embodiment, the adaptation module 202 is further specifically used to: serialize the parameters of the target interaction task to convert the parameters into JSON format; determine the cross-platform native end corresponding to the target interaction task based on environment variables and / or user agents; determine the module name for the communication interface to interact with the cross-platform native end, wherein the module name is provided by the cross-platform native end; determine a preset communication mechanism corresponding to the cross-platform native end, and adapt the communication interface to the cross-platform native end based on the preset communication mechanism.
[0078] In an optional embodiment, the adaptation module 202 is specifically used for: if the cross-platform native end is an iOS end, the communication method of the web page and the host application is adopted to adapt the communication interface to the cross-platform native end; if the cross-platform native end is a Windows end, the communication method of the system intercepting custom requests is adopted to adapt the communication interface to the cross-platform native end; if the cross-platform native end is an Electron end or an Android end, the communication method of injecting global variables is adopted to adapt the communication interface to the cross-platform native end; if the cross-platform native end is a Web end, the communication method of accessing the parent window global object is adopted to adapt the communication interface to the cross-platform native end.
[0079] In an optional embodiment, the communication module 203 is specifically used to: in response to the business end calling a calling interface, establish a cross-platform communication connection between the business end and the corresponding cross-platform native end, so as to execute the business end to call a native interface to the cross-platform native end; and / or in response to the business end calling a notification interface, establish a cross-platform communication connection between the business end and the corresponding cross-platform native end, so as to execute the business end to notify the cross-platform native end of a business event; and / or in response to the business end calling a subscription interface, establish a cross-platform communication connection between the business end and the corresponding cross-platform native end, so as to execute the business end to subscribe to a native event from the cross-platform native end.
[0080] In an optional embodiment, calling the native interface includes calling parameters, calling targets and calling callback functions, and the communication module 203 is specifically used to: in response to the service end calling the calling interface, establish a cross-platform communication connection between the service end and the cross-platform native end, generate a callback identifier and send the callback identifier to the cross-platform native end, so that the cross-platform native end associates the calling callback function. In response to the cross-platform native end feeding back feedback information with the callback identifier, the calling parameters and the calling target are passed to the cross-platform native end to call the native interface to the cross-platform native end.
[0081] In an optional embodiment, the notification business event includes notification event content and notification parameters, and the communication module 203 is specifically used to: in response to the business end calling the notification interface, establish a cross-platform communication connection between the business end and the corresponding cross-platform native end, and transmit the notification event content and notification parameters to the cross-platform native end to notify the cross-platform native end of the business event.
[0082] In one embodiment, subscribing to native events includes monitoring native events and receiving native notifications. Monitoring native events includes monitoring events and monitoring callback functions. Receiving native notification events includes feedback notification events and feedback notification data. The communication module 203 is also specifically used to: respond to the business end calling the subscription interface, establish a cross-platform communication connection between the business end and the corresponding cross-platform native end, and pass the monitoring events and monitoring callback functions to the cross-platform native end to monitor native events to the cross-platform native end; receive feedback notification events and feedback notification data fed back by the cross-platform native end based on the global object.
[0083] Since the embodiments of the module part correspond to the embodiments of the above-mentioned method, please refer to the above-mentioned method embodiments for the introduction of the cross-platform communication device provided by the embodiments of the present invention. The embodiments of the present invention will not be repeated here, and it has the same beneficial effects as the above-mentioned cross-platform communication method.
[0084] The present invention also provides a computer device, which includes a memory and a processor. The memory stores computer-readable instructions. When the computer-readable instructions are executed by the processor, the processor executes the cross-platform communication method as described above. Figure 5 3 is a functional module diagram of a computer device provided by an embodiment of the present invention. The computer device 300 may have relatively large differences due to different configurations or performances, and may include one or more processors (central processing units, CPU) 310 (for example, one or more processors) and a memory 320, and one or more storage media 330 (for example, one or more mass storage devices) storing application programs 333 or data 332. Among them, the memory 320 and the storage medium 330 can be temporary storage or permanent storage. The program stored in the storage medium 330 may include one or more modules (not shown in the figure), and each module may include a series of instruction operations in the computer device 300. Furthermore, the processor 310 can be configured to communicate with the storage medium 330 to execute a series of instruction operations in the storage medium 330 on the computer device 300.
[0085] The computer device 300 may also include one or more power supplies 340, one or more wired or wireless network interfaces 350, one or more input and output interfaces 360, and / or one or more operating systems 331, such as Windows Server, Mac OS X, Unix, Linux, FreeBSD, etc. It will be appreciated by those skilled in the art that Figure 5 The illustrated computer device structure does not constitute a limitation on the computer device, and may include more or fewer components than illustrated, or combine certain components, or arrange the components differently.
[0086] The present invention also provides a computer-readable storage medium, which may be a non-volatile computer-readable storage medium or a volatile computer-readable storage medium. Instructions are stored in the computer-readable storage medium. When the instructions are executed on a computer, the computer executes the cross-platform communication method as described above.
[0087] Those skilled in the art can clearly understand that, for the convenience and simplicity of description, the specific working process of the device described above can refer to the corresponding process in the aforementioned method embodiment, and will not be repeated here. If the integrated module or unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on such an understanding, the technical solution of the present invention is essentially or part of the contribution to the prior art or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM), random access memory (RAM), disk or optical disk and other media that can store program code.
[0088] As described above, the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit the same. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features thereof may be replaced by equivalents. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A cross-platform communication method, characterized in that: The cross-platform communication method comprises: Determine a target interaction task of a service end and a communication interface corresponding to the target interaction task; Based on preset communication rules, determining the cross-platform native end corresponding to the target interaction task, and adapting the communication interface to the cross-platform native end; In response to the service end calling the communication interface, a cross-platform communication connection is established between the service end and the corresponding cross-platform native end to execute the target interaction task.
2. The cross-platform communication method according to claim 1, characterized in that: The determining, based on a preset communication rule, a cross-platform native terminal corresponding to the target interaction task, and adapting the communication interface to the cross-platform native terminal includes: Serializing the parameters of the target interactive task to convert the parameters into JSON format; Determining the cross-platform native end corresponding to the target interaction task based on environment variables and / or user agents; Determine a module name for interaction between the communication interface and the cross-platform native end, wherein the module name is provided by the cross-platform native end; A preset communication mechanism corresponding to the cross-platform native terminal is determined, and the communication interface is adapted to the cross-platform native terminal based on the preset communication mechanism.
3. The cross-platform communication method according to claim 2, characterized in that: The determining a preset communication mechanism corresponding to the cross-platform native terminal, and adapting the communication interface to the cross-platform native terminal based on the preset communication mechanism, includes: If the cross-platform native terminal is an iOS terminal, the communication interface is adapted to the cross-platform native terminal by using a communication method between a web page and a host application; If the cross-platform native end is a Windows end, the communication interface is adapted to the cross-platform native end by adopting a communication method in which the system intercepts the custom request; If the cross-platform native end is an Electron end or an Android end, a communication method of injecting global variables is adopted to adapt the communication interface to the cross-platform native end; If the cross-platform native end is a Web end, the communication interface is adapted to the cross-platform native end by using a communication method of accessing the parent window global object.
4. The cross-platform communication method according to claim 1, characterized in that: The target interaction task includes at least one of calling a native interface, notifying a business event, and subscribing to a native event, and the communication interface includes at least one of a calling interface, a notification interface, and a subscription interface; The step of establishing a cross-platform communication connection between the service end and the corresponding cross-platform native end in response to the service end calling the communication interface to execute the target interaction task includes: In response to the service end calling the calling interface, establishing a cross-platform communication connection between the service end and the corresponding cross-platform native end, so as to execute the service end calling the native interface to the cross-platform native end; and / or, in response to the service end calling the notification interface, establishing a cross-platform communication connection between the service end and the corresponding cross-platform native end, so as to execute the service end notifying the cross-platform native end of a service event; And / or, in response to the service end calling the subscription interface, establishing a cross-platform communication connection between the service end and the corresponding cross-platform native end to execute the service end to subscribe to native events from the cross-platform native end.
5. The cross-platform communication method according to claim 4, characterized in that: The calling native interface includes calling parameters, calling targets and calling callback functions, and the response to the service end calling the calling interface, establishing a cross-platform communication connection between the service end and the corresponding cross-platform native end to execute the service end calling the native interface to the cross-platform native end, including: In response to the service end calling the calling interface, establishing a cross-platform communication connection between the service end and the cross-platform native end, generating a callback identifier and sending the callback identifier to the cross-platform native end, so that the cross-platform native end associates the calling callback function; In response to the cross-platform native end feeding back feedback information with the callback identifier, the calling parameter and the calling target are passed to the cross-platform native end to call a native interface to the cross-platform native end.
6. The cross-platform communication method according to claim 4, characterized in that: The notification service event includes notification event content and notification parameters, and the response to the service end calling the notification interface, establishing a cross-platform communication connection between the service end and the corresponding cross-platform native end to execute the service end notifying the cross-platform native end of the service event includes: In response to the service end calling the notification interface, a cross-platform communication connection is established between the service end and the corresponding cross-platform native end, and the notification event content and the notification parameters are transmitted to the cross-platform native end to notify the cross-platform native end of the service event.
7. The cross-platform communication method according to claim 4, characterized in that: The subscribing to native events includes monitoring native events and receiving native notifications, wherein the monitoring of native events includes monitoring events and monitoring callback functions, and the receiving of native notification events includes feedback notification events and feedback notification data. In response to the service end calling the subscription interface, a cross-platform communication connection is established between the service end and the corresponding cross-platform native end to execute the service end to subscribe to native events from the cross-platform native end, including: In response to the service end calling the subscription interface, a cross-platform communication connection is established between the service end and the corresponding cross-platform native end, and the monitoring event and the monitoring callback function are transmitted to the cross-platform native end to monitor the native event of the cross-platform native end; The feedback notification event and the feedback notification data fed back by the cross-platform native terminal are received based on the global object.
8. A cross-platform communication device, characterized in that: The cross-platform communication device comprises: A determination module, used to determine a target interaction task of a service end and a communication interface corresponding to the target interaction task; An adaptation module, configured to determine the cross-platform native end corresponding to the target interaction task based on a preset communication rule, and adapt the communication interface to the cross-platform native end; The communication module is used to establish a cross-platform communication connection between the service end and the corresponding cross-platform native end in response to the service end calling the communication interface to execute the target interaction task.
9. A computer device, characterized in that: The computer device comprises: a memory and at least one processor, wherein instructions are stored in the memory; The at least one processor calls the instructions in the memory to enable the computer device to execute the cross-platform communication method according to any one of claims 1 to 7.
10. A computer-readable storage medium having instructions stored thereon, characterized in that: When the instructions are executed by a processor, the cross-platform communication method according to any one of claims 1 to 7 is implemented.
Citation Information
Patent Citations
Cross-platform calling method and system for application programs
CN107870821A
Method and device for calling cross-platform native function of mobile terminal
CN110908704A
Cross-platform communication method, system, device, equipment and medium
CN115827285A
Web end and native end interaction method and terminal equipment
CN117041234A