Cross-platform communication method, apparatus, device, and storage medium
By adapting communication interfaces and mechanisms between the business side and the cross-platform native side, the problem of poor portability of cross-platform communication is solved, and an efficient and stable cross-platform communication experience is achieved.
Patent Information
- Application Number
- CN202411086607.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-08
- Publication Date
- 2026-11-27
- Estimated Expiration
- 2044-08-08
AI Technical Summary
In existing technologies, communication methods between the business side and the native platform suffer from poor portability, high development complexity, high cost, and low efficiency when crossing multiple platforms.
By defining the target interaction tasks and corresponding communication interfaces on the business side, and adapting to the cross-platform native client based on preset communication rules, different communication mechanisms are adopted, such as web page and host application, system interception of custom requests, injection of global variables, or access to the parent window's global object, to establish cross-platform communication connections.
It achieves a consistent functional experience across different platforms, improves the efficiency and stability of cross-platform communication, and reduces the difficulty and cost of development and maintenance.
Smart Images

Figure CN119202041B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of Internet technology, and in particular to a cross-platform communication method, apparatus, device, and storage medium. Background Technology
[0002] With the development of internet technology and the widespread use of various communication devices, the demand for interaction between business-side web pages and native platforms is increasing. This necessitates an efficient and unified communication mechanism to facilitate data exchange and function calls between different native platforms. Common native platforms include Web, Windows, Electron, iOS, and Android. Through communication and interaction between the business-side and native platforms, it is possible for the business-side to call software services and hardware resources from the native platform, as well as for the business-side to send notifications and subscribe to the native platform. Furthermore, it enables application development and maintenance on the business-side, expanding its functionality and application scenarios, and enhancing the richness and convenience of user operations.
[0003] However, current methods for interaction between business-side web pages and native platforms are typically developed based on a single platform. When communicating across multiple platforms, it's necessary to consider the differences in communication between the various platforms, deeply understand the communication mechanisms, security rules, error handling methods, performance optimization methods, and compatibility differences between different browsers for each platform, and then set up corresponding functional modules separately for each platform. 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, resulting in poor portability. Maintaining a consistent user experience across different platforms during cross-platform communication increases the difficulty of code reuse and cross-platform development, raises development and maintenance complexity, and leads to high costs and low efficiency. Summary of the Invention
[0004] The main objective of this invention is to provide a cross-platform communication method, apparatus, device, and storage medium, aiming to solve the technical problem of poor portability of existing communication methods between the business end and the native end.
[0005] The first aspect of the present invention provides a cross-platform communication method, the cross-platform communication method comprising: determining a target interaction task of a business terminal and a communication interface corresponding to the target interaction task; determining a cross-platform native terminal corresponding to the target interaction task based on preset communication rules, and adapting the communication interface to the cross-platform native terminal; and, in response to the business terminal calling the communication interface, establishing a cross-platform communication connection between the business terminal and the corresponding cross-platform native terminal to execute the target interaction task.
[0006] Optionally, in a first implementation of the first aspect of the present invention, the step of determining the cross-platform native client corresponding to the target interaction task based on preset communication rules and adapting the communication interface to the cross-platform native client includes: serializing the parameters of the target interaction task to convert the parameters into JSON format; determining the cross-platform native client corresponding to the target interaction task based on environment variables and / or user agents; determining the module name for the communication interface to interact with the cross-platform native client, wherein the module name is provided by the cross-platform native client; determining the preset communication mechanism corresponding to the cross-platform native client, and adapting the communication interface to the cross-platform native client based on the preset communication mechanism.
[0007] Optionally, in a second implementation of the first aspect of the present invention, determining the preset communication mechanism corresponding to the cross-platform native client and adapting the communication interface to the cross-platform native client based on the preset communication mechanism includes: if the cross-platform native client is an iOS client, then a communication method between a webpage and a host application is used to adapt the communication interface to the cross-platform native client; if the cross-platform native client is a Windows client, then a communication method that intercepts custom requests by the system is used to adapt the communication interface to the cross-platform native client; if the cross-platform native client is an Electron client or an Android client, then a communication method that injects global variables is used to adapt the communication interface to the cross-platform native client; if the cross-platform native client is a Web client, then a communication method that accesses the global object of the parent window is used to adapt the communication interface to the cross-platform native client.
[0008] Optionally, in a third implementation 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 step of establishing a cross-platform communication connection between the business terminal and the corresponding cross-platform native terminal in response to the business terminal calling the communication interface to execute the target interaction task includes: establishing a cross-platform communication connection between the business terminal and the corresponding cross-platform native terminal in response to the business terminal calling the calling interface to execute the business terminal calling the native interface to the cross-platform native terminal; and / or, establishing a cross-platform communication connection between the business terminal and the corresponding cross-platform native terminal in response to the business terminal calling the notification interface to execute the business terminal notifying the cross-platform native terminal of a business event; and / or, establishing a cross-platform communication connection between the business terminal and the corresponding cross-platform native terminal in response to the business terminal calling the subscription interface to execute the business terminal subscribing to a native event to the cross-platform native terminal.
[0009] Optionally, in a fourth implementation of the first aspect of the present invention, the invocation of the native interface includes invocation parameters, invocation target, and invocation callback function. The step of establishing a cross-platform communication connection between the business end and the corresponding cross-platform native end in response to the business end invoking the invocation interface, to execute the business end invoking the native interface to the cross-platform native end, includes: in response to the business end invoking the invocation interface, establishing a cross-platform communication connection between the business 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 is associated with the invocation callback function; in response to the cross-platform native end providing feedback information with the callback identifier, passing the invocation parameters and the invocation target to the cross-platform native end to invoke the native interface to the cross-platform native end.
[0010] Optionally, in a fifth implementation of the first aspect of the present invention, the notification service event includes notification event content and notification parameters. The step of responding to the service terminal calling the notification interface and establishing a cross-platform communication connection between the service terminal and the corresponding cross-platform native terminal to execute the service terminal notifying the cross-platform native terminal of the service event includes: responding to the service terminal calling the notification interface, establishing a cross-platform communication connection between the service terminal and the corresponding cross-platform native terminal, and passing the notification event content and the notification parameters to the cross-platform native terminal to notify the cross-platform native terminal of the service event.
[0011] Optionally, in a sixth implementation of the first aspect of the present invention, subscribing to native events includes listening to native events and receiving native notifications. Listening to native events includes listening to an event and a listening callback function. Receiving native notification events includes feedback notification events and feedback notification data. The step of responding to the business terminal calling the subscription interface to establish a cross-platform communication connection between the business terminal and the corresponding cross-platform native terminal, so as to execute the business terminal subscribing to native events from the cross-platform native terminal, includes: responding to the business terminal calling the subscription interface to establish a cross-platform communication connection between the business terminal and the corresponding cross-platform native terminal, and passing the listening event and the listening callback function to the cross-platform native terminal to listen to native events from the cross-platform native terminal; and receiving the feedback notification event and the feedback notification data from the cross-platform native terminal based on a global object.
[0012] A second aspect of the present invention also provides a cross-platform communication device, the cross-platform communication device comprising: a determining module, configured to determine a target interaction task of a business terminal and a communication interface corresponding to the target interaction task; an adaptation module, configured to determine a cross-platform native terminal corresponding to the target interaction task based on preset communication rules, and adapt the communication interface to the cross-platform native terminal; and a communication module, configured to, in response to the business terminal calling the communication interface, establish a cross-platform communication connection between the business terminal and the corresponding cross-platform native terminal to execute the target interaction task.
[0013] A third aspect of the present invention also provides a computer device, the computer device comprising: a memory and at least one processor, the memory storing instructions; the at least one processor calling the instructions in the memory to cause the computer device to perform the cross-platform communication method as described above.
[0014] A fourth aspect of the present invention also provides a computer-readable storage medium storing instructions that, when executed by a processor, implement the cross-platform communication method as described above.
[0015] This invention provides a cross-platform communication method, apparatus, device, and storage medium. First, it determines the target interaction task and its corresponding communication interface on the business side. Then, based on preset communication rules, it determines the cross-platform native client corresponding to the target interaction task and adapts the communication interface to the cross-platform native client. This enables the business side to respond to calls to the communication interface, establishing a cross-platform communication connection between the business side and the corresponding cross-platform native client to execute the target interaction task. This cross-platform communication method eliminates communication differences between the business side's webpage and different native client platforms. Cross-platform communication with different platforms can be achieved by calling the communication interface according to the target interaction task. This allows the business side to call functions of the cross-platform native client and perform target interaction tasks such as notifications and subscriptions from the business side to the cross-platform native client. It is applicable to different native client platforms, maintaining a consistent functional experience across different platforms, greatly improving project portability, effectively enhancing the efficiency and stability of cross-platform communication, and reducing the difficulty and cost of business side development and maintenance. Attached Figure Description
[0016] Figure 1 This is a flowchart illustrating the first embodiment of the cross-platform communication method in this invention.
[0017] Figure 2 For the present invention Figure 1 A flowchart of one embodiment of Example 102;
[0018] Figure 3 For the present invention Figure 1 A flowchart of one embodiment of Example 103;
[0019] Figure 4 This is a functional module diagram of an embodiment of a cross-platform communication device according to the present invention;
[0020] Figure 5 This is a schematic diagram of the functional modules of a computer device according to one embodiment of the present invention. Detailed Implementation
[0021] The terms “first,” “second,” “third,” “fourth,” etc. (if present) in the specification, claims, and accompanying drawings of this invention are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments described herein can be implemented in orders other than those illustrated or described herein. Furthermore, the terms “comprising” or “having,” and any variations thereof, are intended to cover a non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.
[0022] For ease of understanding, the specific process of the cross-platform communication method in this embodiment of the invention is described below. The cross-platform communication method in this embodiment is applied to electronic devices, which can be terminals such as smartphones, laptops, tablets, desktop computers, etc., or development boards, chip systems, etc. Please refer to... Figure 1 , Figure 1 This is a flowchart illustrating the first embodiment of the cross-platform communication method according to the present invention. In this embodiment, the cross-platform communication method includes:
[0023] 101. Determine the target interaction task on the business side and the corresponding communication interface for the target interaction task;
[0024] In this embodiment, the business terminal is used to receive and execute the target interactive task, which is typically presented to the user as a webpage. The target interactive task is the interactive service configured on the business terminal based on the communication connection between the business terminal and the cross-platform native terminal. For example, the target interactive task typically aims to call certain functions of the native terminal through the business terminal, notify the native terminal through the business terminal, listen for events on the native terminal through the business terminal, or receive event notifications sent from the native terminal to the business terminal through the business terminal. Different communication interfaces are typically set up for different target interactive tasks. These communication interfaces are used to establish a communication connection between the business terminal and the cross-platform native terminal, and to transmit the data and instructions required by the target interactive task.
[0025] In one 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 calling an interface, notifying an interface, and subscribing to an interface.
[0026] In this optional embodiment, the target interaction task of calling a native interface typically involves functions or resources of the cross-platform native end that the business 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 business events refers to the business end needing to send a certain notification to the cross-platform native end to inform the native end of the business end's status or the occurrence of certain events. To achieve this goal, the business end will determine a notification interface and send the 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 refers to the business end wanting to listen to certain events of the cross-platform native end and execute the corresponding processing logic when these events occur. For this purpose, 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 preset communication rules, determine the cross-platform native client corresponding to the target interaction task, and adapt the communication interface to the cross-platform native client;
[0028] In this embodiment, the preset communication rules are used to determine the cross-platform native client corresponding to the target interaction task and adapt the corresponding cross-platform native client. Here, a cross-platform native client refers to one of several adaptable native client platforms, such as Web, Windows, Electron, iOS, and Android. Determining the cross-platform native client corresponding to the target interaction task means determining the native client where the target interaction task is executed. This cross-platform native client is usually the native client corresponding to the native system running on the business end, or the native client specifically designated by the target interaction task. The adaptation between the communication interface and the cross-platform native client is to enable faster communication connection with the native client after the business end calls the communication interface and passes in relevant data or instructions, and to select a communication scheme that conforms to the communication mechanism of the cross-platform native client, so as to execute the corresponding target interaction task more effectively and reliably. After adaptation based on these preset communication rules, when the business end needs to perform the target interaction task, it only needs to call the corresponding communication interface to establish a communication connection with the cross-platform native client, thereby executing the corresponding target interaction task.
[0029] In an optional embodiment, see Figure 2 Based on preset communication rules, the cross-platform native client corresponding to the target interaction task is determined, and the communication interface is adapted to the cross-platform native client, including:
[0030] 1021. Serialize the parameters of the target interaction task to convert them into JSON format;
[0031] 1022. Based on environment variables and / or user agents, determine the cross-platform native client corresponding to the target interaction task;
[0032] 1023. Determine the module name for the communication interface to interact with the cross-platform native client, whereby the module name is provided by the cross-platform native client;
[0033] 1024. Determine the pre-built communication mechanism corresponding to the cross-platform native client, and adapt the communication interface to the cross-platform native client based on the pre-built communication mechanism.
[0034] In this optional embodiment, serializing the parameters of the target interaction task can ensure the consistency and readability of the data during transmission. JSON (JavaScript Object Notation) is a lightweight data exchange format, typically consisting of serialized objects or arrays. By converting the parameters to JSON format, the parameters of the target interaction task can be parsed and used across different platforms, data structures, and language forms, making the parameters more compatible, complete, and portable, easier to understand and operate, and effectively improving the efficiency, flexibility, and reliability of cross-platform communication.
[0035] In this optional embodiment, environment variables and user agents (UAs) are common identification information in native systems. Environment variables are variables used in native systems to define the program's runtime environment; their values can be referenced during program execution and used to obtain information about the native system. The user agent, on the other hand, is a special string header sent to the server by the user's browser or other client, used to identify and distinguish different native systems. By using environment variables and / or user agents, the cross-platform native client corresponding to the business application can be determined more accurately, facilitating subsequent adaptation of the business application to that cross-platform native client.
[0036] In this optional embodiment, determining the module name for the communication interface to interact with the cross-platform native client is crucial for establishing an accurate communication connection between the business side and the native client. The module name is typically provided by the native client and identifies a specific module or component within the native client capable of communicating with the business side. By agreeing on the interaction with the native client, the communication interface can be associated with communication between different functions of the cross-platform native client. For example, for the two modules `window.webkit.messageHandlers.publishHandler` and `window.webkit.messageHandlers.invokeHandler` agreed upon on the iOS platform, it can be agreed that `publishHandler` handles event interaction and `invokeHandler` handles interface interaction. By determining the correct module name, it ensures that the communication interface can correctly call the functions of the cross-platform native client, send notifications, or subscribe to events, thereby guaranteeing the accuracy and reliability of cross-platform communication.
[0037] In this optional embodiment, determining the pre-defined communication mechanism corresponding to the cross-platform native client is to select a suitable communication scheme to ensure compatibility and stability between the communication interface and the cross-platform native client. The pre-defined communication mechanism is typically a set of communication rules and protocols pre-set based on factors such as the type of native client, operating system, and network environment. Different pre-defined communication mechanisms are required for different native client platforms. By determining the pre-defined communication mechanism corresponding to the cross-platform native client, the communication scheme that best matches the cross-platform native client can be selected, thereby improving the efficiency and stability of cross-platform communication.
[0038] In one optional embodiment, a pre-defined communication mechanism corresponding to the cross-platform native client is determined, and the communication interface is adapted to the cross-platform native client based on the pre-defined communication mechanism, specifically including:
[0039] (1) If the cross-platform native client is iOS, then the communication method between the web page and the host application is adopted, and the communication interface is adapted to the cross-platform native client.
[0040] (2) If the cross-platform native client is Windows, the system will intercept custom requests to adapt the communication interface to the cross-platform native client.
[0041] (3) If the cross-platform native client is an Electron client or an Android client, then a communication method of injecting global variables is adopted to adapt the communication interface to the cross-platform native client;
[0042] (4) If the cross-platform native client is a Web client, then the communication method of accessing the global object of the parent window is adopted to adapt the communication interface to the cross-platform native client.
[0043] In this optional embodiment, when the cross-platform native client is iOS, due to the strict limitations on the execution of web page content on the iOS client, web content typically runs within the context of a host application such as WKWebView or UIWebView. In this case, a communication method between the web page and the host application can be adopted. For example, on the iOS client, the delegate method of the WKScriptMessageHandler protocol can be used, while the window.webkit.messageHandlers module is used in the communication interface to achieve cross-platform native client and iOS client adaptation, thereby enabling communication between the business client and the iOS client through the communication interface.
[0044] In this optional embodiment, when the cross-platform native client is a Windows client, due to the openness and flexibility of the Windows client, it can run more custom operations. In the Windows environment, when the business side needs to communicate with the native client, a communication method of system interception of custom requests can be adopted. This method requires the business side to trigger the corresponding custom request. The cross-platform native client intercepts the request and parses the URL (Uniform Resource Locator) field information in the request. Then, based on the parsing protocol and URL field information agreed upon by the business side, it implements a communication interface to adapt to the Windows native client, thereby enabling the business side to communicate with the Windows client through the communication interface.
[0045] In this optional embodiment, when the cross-platform native client is either an Electron client or an Android client, since both Electron and Android clients support JavaScript runtime environments, communication can be achieved by injecting global variables. For example, in Electron, the preload capability of the platform's WebView component can be used to input global custom modules into JavaScript for communication. This communication capability is provided by the ipcRenderer module, which injects specific global variables or objects into the rendering process through the IPC (Inter-Process Communication) mechanism between the main process and the rendering process. This allows business code to access these global variables or objects through the communication interface to adapt to the native client, thereby enabling communication between the business side and the Electron client through the communication interface. Similarly, on the Android client, the addJavaScriptInterface interface provided by the WebView component can be used to expose native methods or objects to the JavaScript environment, achieving adaptation of the communication interface to the cross-platform native client, thereby enabling communication between the business side and the Android client through the communication interface.
[0046] In this optional embodiment, when the cross-platform native client is a web client, since the business application itself runs in a web environment, a communication method that accesses the global object of the parent window can be used. This method is typically applicable when the parent window page and the business application page are in the same domain, and the business application is embedded in other web pages. In this case, by accessing parent.window using the communication interface, the parent-level custom communication module can be accessed, thereby adapting to the parent window or other nested web pages, and enabling communication between the business application and the web client through the communication interface.
[0047] In this optional embodiment, it should be noted that the method of determining the pre-built communication mechanism corresponding to the cross-platform native client and adapting the communication interface to the cross-platform native client based on the pre-built communication mechanism is not limited to the applications in the aforementioned native platforms. The cross-platform native client can also be the native client of other mobile operating systems such as HarmonyOS, HyperOS, and Windows Phone, or the native client of desktop operating systems such as Linux and macOS. Further details will not be elaborated here, as long as adaptation and communication can be performed according to the development mode agreed upon by different native client platforms.
[0048] 103. In response to the business side calling the communication interface, establish a cross-platform communication connection between the business side and the corresponding cross-platform native end to execute the target interaction task.
[0049] In this embodiment, after determining the pre-built communication mechanism corresponding to the cross-platform native client and successfully adapting the communication interface to the cross-platform native client, the business end can establish a cross-platform communication connection with the corresponding cross-platform native client by calling this adapted communication interface. This enables the execution of corresponding target interaction tasks, ensuring efficient and stable exchange of information and data between the business end and the native client. During this process, the establishment of the cross-platform communication connection is automatic. Users do not need to concern themselves with the underlying communication details; they only need 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. This greatly improves development efficiency and user experience, making data sharing and function calls between the business end and the cross-platform native client simpler and more convenient. Furthermore, the cross-platform communication connection can employ appropriate encryption and authentication mechanisms, and strict access control can be implemented for the communication interface to ensure the stability and security of cross-platform communication. This embodiment does not impose specific restrictions on this security mechanism.
[0050] In an optional embodiment, see Figure 3 In response to a business application's call to the communication interface, a cross-platform communication connection is established between the business application and the corresponding cross-platform native application to execute the target interactive task, including:
[0051] 1031. In response to a business-side call to the API, establish a cross-platform communication connection between the business side and the corresponding cross-platform native client to execute the business side's call to the native API from the cross-platform native client; and / or
[0052] 1032. In response to the business side calling the notification interface, establish a cross-platform communication connection between the business side and the corresponding cross-platform native client to execute the business side's notification of business events to the cross-platform native client; and / or
[0053] 1033. In response to the business side calling the subscription interface, establish a cross-platform communication connection between the business side and the corresponding cross-platform native end, so as to execute the business side to subscribe to native events from the cross-platform native end.
[0054] In this optional embodiment, when it is necessary to call native functions provided by the cross-platform native client, the business end will call the adapted communication interface to trigger the establishment of a communication connection with the native client. After receiving the call request from the business end, the native client will execute the corresponding function according to the native interface specified in the request and return the execution result to the business end. This approach allows the business end to utilize the rich functions provided by the native client, such as file operations, system settings, and hardware control, thereby improving the functionality and user experience of the business end. Simultaneously, when certain important events occur on the business end, such as user clicks or data updates, the business end can send notifications to the native client by calling the communication interface. After receiving these notifications, the native client can perform corresponding processing according to business logic, such as updating the UI or triggering callback functions. This communication method enables real-time interaction between the business end and the native client, ensuring the correctness and integrity of the business logic. Furthermore, if it is necessary to listen for or process certain events generated by the native client, such as system notifications or hardware status changes, the business end can call the communication interface to subscribe to these events from the native client. When these events occur on the native device, it will proactively send a notification to the business device. After receiving the notification, the business device can perform corresponding processing, enabling it to perceive the state changes of the native device in real time and thus make corresponding responses and operations.
[0055] In this optional embodiment, by providing diverse communication interfaces and interaction methods, communication between the business side and the cross-platform native side becomes more flexible and efficient. Whether calling native interfaces, notifying business events, or subscribing to native events, all can be achieved through the adapted communication interfaces, thereby simplifying the development process and improving development efficiency. At the same time, this communication method also ensures the correctness and integrity of business logic, enabling cross-platform applications to run stably and efficiently on various native platforms.
[0056] In one optional embodiment, calling the native interface includes calling parameters, a calling target, and a calling callback function. In response to the business side calling the interface, a cross-platform communication connection is established between the business side and the corresponding cross-platform native end to execute the business side calling the native interface to the cross-platform native end, including:
[0057] (1) In response to the business side calling the API, establish a cross-platform communication connection between the business side 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 can associate and call the callback function.
[0058] (2) In response to feedback information with callback identifier from the cross-platform native client, pass the call parameters and call target to the cross-platform native client to call the native interface.
[0059] In this optional embodiment, calling native interfaces across platforms typically involves complex parameter passing and callback parameter processing. Therefore, for the target interactive task of calling a native interface, a configuration including call parameters, a call target, and a call callback function can be preset. The call parameters are the configuration parameters required to call the native interface, the call target is the name or identifier of the native interface or a native method to be called, and the call callback function is used to trigger a callback after the call, so that the callback carries the result of the call to understand the implementation of the native interface.
[0060] In this optional embodiment, when the business side calls the API, the API generates a callback identifier and passes it to the cross-platform native client. This callback identifier acts as a label, used to identify and associate specific callback functions during subsequent API calls on the cross-platform native client. Upon receiving this callback identifier, the cross-platform native client associates it with the corresponding callback function, ensuring that the function specified by the business side is accurately invoked after the call is completed. When the cross-platform native client is ready to receive the call parameters and target, it sends a feedback message with the callback identifier to the business side. This feedback message indicates that the business side can pass the call parameters and target to the native client. Upon receiving this feedback message, the business side sends the previously prepared call parameters and target to the cross-platform native client, thereby triggering the native API call. In this way, the business side can easily call various native APIs provided by the cross-platform native client to implement complex functions and operations. Simultaneously, due to the introduction of the callback identifier, the system can also ensure that the function specified by the business side is accurately invoked after the call is completed, thus guaranteeing the integrity and correctness of the entire call process.
[0061] In this optional embodiment, the calling interface can specifically be a specific function interface `bridge.invoke(command, params, callback)`, through which direct communication and interaction with the native interface can be achieved. Here, `command` is the call target, `params` are the call parameters, and `callback` is the call callback function. When this calling interface is invoked, it internally generates a `callbackId`, which is the callback identifier and passed to the cross-platform native end. This `callbackId` is stored in a map object, associated with the passed-in call callback function, and sent along with the `callbackId` to the cross-platform native end. After the cross-platform native end processes the native interface's call logic, it sends the corresponding result and the corresponding `callbackId` to the calling interface. Upon obtaining the `callbackId`, the calling interface can find the stored callback function in the map object, then invoke the callback function, passing the parameters returned by the cross-platform native end into the callback function, and finally deleting the stored callback function from the map object, thereby realizing the invocation of the native interface. Accordingly, the API call can be configured with a corresponding internal function interface, `bridge.invokeHandler(command, params, callbackId)`, enabling the API call to adapt to cross-platform native clients according to preset communication rules, thereby bridging the differences between different native platforms. It should be noted that the names of the aforementioned function interfaces `invoke`, `invokeHandler`, and the function parameters are customizable. Different function names and parameter names can be used depending on the task and naming preferences, as long as the functionality of the API call in this embodiment is achieved.
[0062] In one optional embodiment, the notification business event includes notification event content and notification parameters. 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 notification of the business event from the business end to the cross-platform native end, including:
[0063] In response to a business-side call to the notification interface, a cross-platform communication connection is established between the business-side and the corresponding cross-platform native client. The notification event content and notification parameters are then passed to the cross-platform native client to notify the business event.
[0064] In this optional embodiment, the notification from the business end to the cross-platform native end typically includes the transmission of notification event content and the configuration of notification parameters. The notification interface can receive the notification event content and notification parameters as output. When the business end calls the notification interface, the interface sends this information to the cross-platform native end. After receiving this information, the native end parses and processes it, thereby making adaptive adjustments based on the notification content. Furthermore, after processing the notification event, the cross-platform native end can selectively send a feedback message to the business end, informing it of the notification event processing status. This feedback message can be a simple success or failure status code, or a structure containing more detailed information, allowing the business end to understand the processing progress and results of the notification event in real time, and thus make corresponding adjustments and optimizations.
[0065] In this optional embodiment, the notification interface can specifically be a specific function interface `bridge.publish(event, params)`, through which event notifications can be sent to the cross-platform native client. Here, `event` represents the notification event content, and `params` represents the notification parameters. When this notification interface is called, it establishes a communication connection between the business end and the cross-platform native client internally, and directly sends the notification event content and notification parameters to the cross-platform native client. After receiving this information, the cross-platform native client processes it accordingly based on the notification event content and adaptively adjusts it based on the notification parameters to meet the needs of the business end. Correspondingly, the notification interface can set a corresponding internal function interface, `bridge.publishHandler(event, params)`, so that the notification interface can adapt to the cross-platform native client according to preset communication rules, thereby bridging the differences between different native platforms. It should be noted that the names of the above-mentioned function interfaces `publish` and `publishHandler`, as well as the parameter names, are customizable. Different function names and parameter names can be used depending on the task and naming preferences, as long as the functionality of the notification interface in this embodiment is achieved.
[0066] In one optional embodiment, subscribing to native events includes listening to native events and receiving native notifications. Listening to native events includes listening to events and listening callback functions. Receiving native notification events includes feedback notification events and feedback notification data. In response to the business side calling the subscription interface, a cross-platform communication connection is established between the business side and the corresponding cross-platform native client to execute the business side's subscription to native events from the cross-platform native client, including:
[0067] (1) In response to the business side calling the subscription interface, establish a cross-platform communication connection between the business side and the corresponding cross-platform native end, and pass the listening event and listening callback function to the cross-platform native end to listen for native events on the cross-platform native end;
[0068] (2) Receive feedback notification events and feedback notification data from the cross-platform native client based on the global object.
[0069] In this optional embodiment, subscribing to native events includes actively listening to native events and passively receiving native notifications. When listening to native events, the listening event and its callback function are taken as input. When the business end calls the subscription interface, the interface sends this information to the cross-platform native end. Upon receiving this information, the cross-platform native end configures the event listening settings. Once the native end detects an event matching the listening event, it immediately triggers the callback function, passing event-related data as parameters. The business end can then obtain this event data through the callback function and process it accordingly. Alternatively, when a specific event occurs on the native end, it can also actively send notifications to the business end. These notifications typically include feedback notification events and feedback notification data. To receive these notifications, the business end needs to accept them based on a global object. When the native end sends a notification, it sends this data to this global object. The business end can then receive this notification data in real time by receiving this global object and process it accordingly based on the notification content.
[0070] In this optional embodiment, specifically, native events can be listened to using `window.hybridBridge.subscribe(event, callback)`, where `event` is the event to listen for and `callback` is the callback function. When this subscription interface is called, it establishes a communication connection between the business side and the cross-platform native side, and sends the event and callback function to the cross-platform native side. After receiving this information, the native side will configure the event listening settings and trigger the callback function when the event occurs. To receive native notifications, the business side can define a global object as a container for receiving notifications. For example, this can be done by manipulating the global object of the JavaScript context using `window.hybridBridge.subscribeHandler(event, data)`, where `event` is the feedback notification event and `data` is the feedback notification data. When the cross-platform native side sends a notification, it will send the notification data to this global object. By obtaining changes to this global object, the business side can obtain the notification data from the cross-platform native side in real time, realizing the subscription of native events from the business side to the cross-platform native side.
[0071] This embodiment can eliminate communication differences between the business-side webpage and different native platforms. By calling the communication interface according to the target interaction task, cross-platform communication with different platforms can be achieved. This enables the business side to call functions of the cross-platform native platform, as well as to realize target interaction tasks such as notifications and subscriptions from the business side to the cross-platform native platform. It is applicable to different native platforms and can maintain a consistent functional experience on different native 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] To perform the corresponding steps in the above method embodiments and various possible implementations, an implementation of a cross-platform communication device is provided below. Please refer to... Figure 4 , Figure 4 This is a functional block diagram of an embodiment of a cross-platform communication device according to 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] The determination module 201 is used to determine the target interaction task of the business side and the communication interface corresponding to the target interaction task;
[0074] The adaptation module 202 is used to determine the cross-platform native terminal corresponding to the target interaction task based on preset communication rules, and adapt the communication interface to the cross-platform native terminal.
[0075] The communication module 203 is used to respond to the business terminal calling the communication interface to establish a cross-platform communication connection between the business terminal and the corresponding cross-platform native terminal in order to execute the target interaction task.
[0076] In one 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 calling an interface, notifying an interface, and subscribing to an interface.
[0077] In an optional embodiment, the adaptation module 202 is further configured to: serialize the parameters of the target interaction task to convert the parameters into JSON format; determine the cross-platform native client 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 client, wherein the module name is provided by the cross-platform native client; determine the preset communication mechanism corresponding to the cross-platform native client, and adapt the communication interface to the cross-platform native client based on the preset communication mechanism.
[0078] In an optional embodiment, the adaptation module 202 is further configured to: if the cross-platform native client is an iOS client, then use the communication method between the webpage and the host application to adapt the communication interface to the cross-platform native client; if the cross-platform native client is a Windows client, then use the communication method of the system intercepting custom requests to adapt the communication interface to the cross-platform native client; if the cross-platform native client is an Electron client or an Android client, then use the communication method of injecting global variables to adapt the communication interface to the cross-platform native client; if the cross-platform native client is a Web client, then use the communication method of accessing the global object of the parent window to adapt the communication interface to the cross-platform native client.
[0079] In an optional embodiment, the communication module 203 is further configured to: establish a cross-platform communication connection between the business terminal and the corresponding cross-platform native terminal in response to the business terminal calling the calling interface, so as to execute the business terminal calling the native interface to the cross-platform native terminal; and / or establish a cross-platform communication connection between the business terminal and the corresponding cross-platform native terminal in response to the business terminal calling the notification interface, so as to execute the business terminal notifying the cross-platform native terminal of business events; and / or establish a cross-platform communication connection between the business terminal and the corresponding cross-platform native terminal in response to the business terminal calling the subscription interface, so as to execute the business terminal subscribing to native events to the cross-platform native terminal.
[0080] In one optional embodiment, calling the native interface includes calling parameters, a calling target, and a calling callback function. The communication module 203 is further configured to: in response to the business end calling the interface, establish a cross-platform communication connection between the business 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 callback function with the call. In response to feedback information with the callback identifier from the cross-platform native end, pass the calling parameters and the calling target to the cross-platform native end to call the native interface from the cross-platform native end.
[0081] In an optional embodiment, the notification business event includes notification event content and notification parameters. The communication module 203 is further configured 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 pass 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 listening to native events and receiving native notifications. Listening to native events includes listening to events and listening callback functions. Receiving native notification events includes feedback notification events and feedback notification data. The communication module 203 is further configured 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 listening events and listening callback functions to the cross-platform native end to listen to native events from the cross-platform native end; and receive feedback notification events and feedback notification data from the cross-platform native end based on a global object.
[0083] Since the embodiments of the module part correspond to the embodiments of the above method, please refer to the above 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 described again here, but have the same beneficial effects as the above cross-platform communication method.
[0084] The present invention also provides a computer device, the computer device including a memory and a processor, the memory storing computer-readable instructions, which, when executed by the processor, cause the processor to perform the cross-platform communication method as described above. Figure 5 This is a functional block diagram of a computer device 300 provided in an embodiment of the present invention. The computer device 300 can vary significantly due to different configurations or performance characteristics. It may include one or more central processing units (CPUs) 310 (e.g., one or more processors) and a memory 320, and one or more storage media 330 (e.g., one or more mass storage devices) for storing application programs 333 or data 332. The memory 320 and storage media 330 can be temporary or persistent storage. The program stored in the storage media 330 may include one or more modules (not shown in the diagram), each module including a series of instruction operations on the computer device 300. Furthermore, the processor 310 may be configured to communicate with the storage media 330 and execute the series of instruction operations in the storage media 330 on the computer device 300.
[0085] 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 / output interfaces 360, and / or one or more operating systems 331, such as Windows Server, Mac OS X, Unix, Linux, FreeBSD, etc. Those skilled in the art will understand that... Figure 5 The computer device structure shown does not constitute a limitation on the computer device and may include more or fewer components than shown, or combine certain components, or have different component arrangements.
[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, wherein the computer-readable storage medium stores instructions that, when executed on a computer, cause the computer to perform the cross-platform communication method as described above.
[0087] Those skilled in the art will understand that, for the sake of convenience and brevity, the specific working process of the device described above can be referred to the corresponding process in the foregoing method embodiments, and will not be repeated here. If the integrated module or unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of the present invention. The aforementioned storage medium includes: USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, optical disks, and other media capable of storing program code.
[0088] The above-described embodiments are only used to illustrate the technical solutions of the present invention, and are not intended to limit it. Although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate 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 includes: Determine the target interaction task on the business side and the communication interface corresponding to the target interaction task; The parameters of the target interaction task are serialized to convert them into JSON format; Based on environment variables and / or user agents, determine the cross-platform native client corresponding to the target interaction task; Determine the module name for the communication interface to interact with the cross-platform native client, wherein the module name is provided by the cross-platform native client; Determine the pre-defined communication mechanism corresponding to the cross-platform native client, and adapt the communication interface to the cross-platform native client based on the pre-defined communication mechanism; In response to the business terminal calling the communication interface, a cross-platform communication connection is established between the business terminal and the corresponding cross-platform native terminal to execute the target interaction task.
2. The cross-platform communication method according to claim 1, characterized in that, The step of determining the pre-defined communication mechanism corresponding to the cross-platform native client, and adapting the communication interface to the cross-platform native client based on the pre-defined communication mechanism, includes: If the cross-platform native client is an iOS client, then the communication method between the web page and the host application is adopted, and the communication interface is adapted to the cross-platform native client. If the cross-platform native client is a Windows client, then a communication method that intercepts custom requests by the system is adopted to adapt the communication interface to the cross-platform native client; If the cross-platform native client is an Electron client or an Android client, a communication method that injects global variables is adopted to adapt the communication interface to the cross-platform native client; If the cross-platform native client is a web client, then the communication method of accessing the global object of the parent window is adopted to adapt the communication interface to the cross-platform native client.
3. 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 calling an interface, notifying an interface, and subscribing to an interface; The step of responding to the business terminal calling the communication interface and establishing a cross-platform communication connection between the business terminal and the corresponding cross-platform native terminal to execute the target interaction task includes: In response to the business terminal calling the calling interface, a cross-platform communication connection is established between the business terminal and the corresponding cross-platform native terminal to execute the business terminal calling the native interface to the cross-platform native terminal; And / or, in response to the business terminal calling the notification interface, a cross-platform communication connection is established between the business terminal and the corresponding cross-platform native terminal to execute the business terminal to notify the cross-platform native terminal of the business event; And / or, in response to the business terminal calling the subscription interface, a cross-platform communication connection is established between the business terminal and the corresponding cross-platform native terminal to execute the business terminal subscribing to native events from the cross-platform native terminal.
4. The cross-platform communication method according to claim 3, characterized in that, The native interface call includes call parameters, call target, and call callback function. The step of responding to the business terminal calling the interface and establishing a cross-platform communication connection between the business terminal and the corresponding cross-platform native terminal to execute the business terminal calling the native interface from the cross-platform native terminal includes: In response to the business terminal calling the calling interface, a cross-platform communication connection is established between the business terminal and the cross-platform native terminal, and a callback identifier is generated and sent to the cross-platform native terminal so that the cross-platform native terminal is associated with the calling callback function; In response to feedback information with the callback identifier from the cross-platform native client, the call parameters and the call target are passed to the cross-platform native client to call the native interface.
5. The cross-platform communication method according to claim 3, characterized in that, The notification service event includes notification event content and notification parameters. The response to the service terminal calling the notification interface and establishing a cross-platform communication connection between the service terminal and the corresponding cross-platform native terminal to execute the service terminal notifying the cross-platform native terminal of the service event includes: In response to the business terminal calling the notification interface, a cross-platform communication connection is established between the business terminal and the corresponding cross-platform native terminal, and the notification event content and the notification parameters are passed to the cross-platform native terminal to notify the cross-platform native terminal of the business event.
6. The cross-platform communication method according to claim 3, characterized in that, The subscription to native events includes listening to native events and receiving native notifications. Listening to native events includes listening for events and listening callback functions. Receiving native notification events includes receiving feedback notification events and feedback notification data. The response to the business end calling the subscription interface establishes a cross-platform communication connection between the business end and the corresponding cross-platform native end, to execute the business end subscribing to native events from the cross-platform native end, including: In response to the business terminal calling the subscription interface, a cross-platform communication connection is established between the business terminal and the corresponding cross-platform native terminal, and the listening event and the listening callback function are passed to the cross-platform native terminal to listen for native events. The feedback notification event and feedback notification data are received from the cross-platform native client based on a global object.
7. A cross-platform communication device, characterized in that, The cross-platform communication device includes: The determination module is used to determine the target interaction task of the business side and the communication interface corresponding to the target interaction task; An adaptation module is configured to: serialize the parameters of the target interaction task to convert them into JSON format; determine the cross-platform native client 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 client, wherein the module name is provided by the cross-platform native client; determine the preset communication mechanism corresponding to the cross-platform native client, and adapt the communication interface to the cross-platform native client based on the preset communication mechanism; Based on preset communication rules, the cross-platform native client corresponding to the target interaction task is determined, and the communication interface is adapted to the cross-platform native client. The communication module is used to respond to the business terminal calling the communication interface, establish a cross-platform communication connection between the business terminal and the corresponding cross-platform native terminal, and execute the target interaction task.
8. A computer device, characterized in that, The computer device includes: a memory and at least one processor, wherein the memory stores instructions; The at least one processor invokes the instructions in the memory to cause the computer device to perform the cross-platform communication method as described in any one of claims 1-6.
9. A computer-readable storage medium storing instructions thereon, characterized in that, When the instructions are executed by the processor, they implement the cross-platform communication method as described in any one of claims 1-6.
Citation Information
Patent Citations
Cross-platform calling method and system for application programs
CN107870821A
Cross-platform communication method, system, device, equipment and medium
CN115827285A
