Sharing of research on Android third-party application access to wechat platform (2)

After the opening of the wechat platform, it is very popular. Many third-party applications want to try. After all, it is good to use the relationship chain established by wechat to expand their own applications, which can save a lot of social expenses. Recently, I have been studying this thing due to my internship needs, but I found that there are still very few relevant information on the Internet, Here I give out my whole research situation, hoping to learn together.

Access of third-party applications to wechat platform (1)

2、 Timing diagram of third-party application and wechat communication

2. Receive wechat request information

The first four steps are the same as the previous "1. Send a message to wechat". There is no need to repeat them. They are given here only for

Integrity of the process. After we register, the application icon will appear in the wechat chat list. Click the plus sign to add,

After adding, if you click the application icon during chat, the third-party application will be recalled, as shown in the above sequence diagram. Here's another point,

When the media type we send messages to wechat is appdata type, that is, wxappextendobject object, friends are in

When you click the message when you receive the message, a similar callback process will appear. What needs to be modified is the type when onreq is called

Is constantsapi.command_ SHOWMESSAGE_ FROM_ Wx instead of constantsapi.command_ GETMESSAGE_ FROM_ WX 。

When we receive a constantsapi. Command in the onreq method_ GETMESSAGE_ FROM_ WX

If you want to send the response back to wechat when sending a message of type, you can use the following code snippet to implement it

It can be found that the message object of the response is the same as that of the request. Let's take a look at the effect diagram

This is the chat window, provided that you have clicked the plus sign to add the application to the list below

At this time, if you click the third-party application, it will call back our set interface

This interface is self-defined. You can customize the response interface after receiving the wechat request.

After talking for a long time, I haven't talked about the origin of onreq method or onresp method mentioned earlier,

In other words, if we want to process the feedback after sending a request to wechat or respond to the request of wechat

What should we do? We need the following three steps: A. you can create a wxapi directory under the corresponding directory of your package name, and add a wxentryactivity class under the wxapi directory. This class inherits from the activity (of course, the path is user-defined, whatever you want) and add the exported attribute in the manifest file and set it to true, for example:

Finally, these two magic methods appear. You can see their general usage

Finally, some points for attention are easily confused. I have written a demo and verified it. If you think there is something wrong, you are welcome to point it out and discuss it

1. Registration is local and only needs to be registered once, but the wechat server will detect the legitimacy of third-party applications when sending

2. To put it bluntly, the content to be sent by the local third-party application is sent to the wechat process first, and then the wechat process sends the message to its wechat friends after receiving it

3. After uninstallation or de registration, the third-party application will not appear in the list of applications that can be added on wechat

4. Among the media objects sent by wechat open platform, web pages, audio and video can only be network URLs. Pictures can send local path, local byte data, network URL, and files can send local byte data and local path. The byte array attached to the media object will be clicked by default to give the download interface, The path stored after downloading is in the / sdcard / Tecent / micromsg / local wechat account (usually a string of alphanumeric combinations) / attachment folder. This path does not need to be recorded by yourself. Wechat will record it in the filepath field of the media object. At this time, it must be noted that when you respond to the show request of wechat, the filedata field in the media object is empty, Even if you attach this field when sending, as mentioned earlier, you can only use the field filepath.

If you want to process only appdata data, the reasons are given below:

The only special point of appdata type objects is that they tell wechat that a third-party application needs to be called when the message is clicked, while the default page of wechat is displayed when other types of media objects are clicked

5. Wechat determines the performance of the message in the chat box and the response after clicking according to the transaction field. Only when appdata is clicked can the process of the third-party application be called. The triggered method is the onreq method of the class that implements the iwxapieventhandler interface, and the type is constantsapi.command_ SHOWMESSAGE_ FROM_ Wx while chatting with friends on wechat, the method triggered by clicking the third-party application icon is the onreq method of the class that implements the iwxapieventhandler interface, and the type is constantsapi.command_ GETMESSAGE_ FROM_ WX

6. Wechat will give the application installation information according to whether the third-party application is registered. For example, when an audio message is sent to a wechat friend, when the friend clicks the message, wechat will load the URL in the audio message with a WebView. However, previously, some wechat defined information will be attached to the URL, One field is isinstalled. We can disconnect the network and click it again. Then the classic interface will appear naturally, and you can see the true face of the URL. Because the demo I wrote myself was lost after the internship, I gave it to an official demo to download, If there is something unclear, you can communicate

Thank you for your cooperation and hope to learn together.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>