Weixin Sharing and Favorites allow third-party apps to enable users to share any text, image, music, video, webpage, and Mini Program in Weixin friend chats and on Moments or add them to Favorites from apps.

The Weixin Sharing and Favorites features are open to all developers (overseas apps support sharing webpages and Mini Programs). Developers can obtain the Weixin Sharing and Favorites permissions after applying for a mobile app and getting the approval under the Weixin Open Platform account.

After integrating the Weixin SDK into an app, developers may call APIs to implement this feature. The following are examples of sharing text, image, music, video, webpage, and Mini Program.

WXMediaMessage Description

Field Type Description Note
sdkVer int SDK version number
title String Message title The maximum length is 512 bytes.
description String Message description The maximum length is 1 KB.
thumbData byte[] Binary data of thumbnail The maximum size is 32 KB.
mediaObject WXMediaMessage.IMediaObject Message object Describes an API of a media object. Media objects include
WXTextObject, WXImageObject, WXMusicObject, WXVideoObject、WXWebpageObject, WXFileObject, WXAppExtendObject, and WXMiniProgramObject.

**SendMessageToWX.Req (request class of SendMessageToWX)**

The target scene shared or added to favorites is implemented by modifying the scene field of SendMessageToWX.Req.

Field Type Description Note
message WXMediaMessage
scene int Target scene to be shared Share to Chat:
SendMessageToWX.Req.WXSceneSession
Share on Moments:
SendMessageToWX.Req.WXSceneTimeline
Share to Favorites:
SendMessageToWX.Req.WXSceneFavorite
transaction String Transaction ID of the request initiated via Req. Enter the transaction ID in the response returned via Resp.

1. Example of Text Sharing

WXTextObject (derived class of WXMediaMessage.IMediaObject, used to describe a text object)

Field Type Description Note
text String Text data The length must be greater than 0 and less than or equal to 10 KB.

Demo of text sharing
// Initialize a WXTextObject object and fill in the text content to be shared
WXTextObject textObj = new WXTextObject();
textObj.text = text;

// Use the WXTextObject object to initialize a WXMediaMessage object
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = textObj;
msg.description = text;

SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("text"); 
req.message = msg;
req.scene = mTargetScene; 
// Call the API and send data to Weixin
api.sendReq(req);


2. Example of Image Sharing

WXImageObject (derived class of WXMediaMessage.IMediaObject, used to describe an image object)

Field Type Description Note
imageData byte[] Binary data of image The maximum size is 10 MB.
imagePath String Local path of image The maximum size is 10 MB.

Demo of image sharing
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.send_img);

// Initialize WXImageObject and WXMediaMessage objects
WXImageObject imgObj = new WXImageObject(bmp);
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = imgObj;

// Set a thumbnail
Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true);
bmp.recycle();
msg.thumbData = Util.bmpToByteArray(thumbBmp, true);

// Build a Req
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("img");
req.message = msg;
req.scene = mTargetScene;
req.userOpenId = getOpenId();
// Call the API and send data to Weixin
api.sendReq(req);

3. Example of Music Sharing

WXMusicObject (derived class of WXMediaMessage.IMediaObject, used to describe an audio object)

Field Type Description Note
musicUrl String URL address of audio webpage The maximum length is 10 KB.
musicLowBandUrl String URL address of audio webpage in the low bandwidth environment The maximum length is 10 KB.
musicDataUrl String URL address of audio data The maximum length is 10 KB.
musicLowBandDataUrl String URL address of audio data in the low bandwidth environment The maximum length is 10 KB.

Note: Either musicUrl or musicLowBandUrl should be specified.

Demo of music sharing

// Initialize a WXMusicObject and enter an URL
WXMusicObject music = new WXMusicObject();
music.musicUrl="Music url";

// Initialize a WXMediaMessage object using a WXMusicObject object
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = music;
msg.title = "Music title";
msg.description = "Music description"
Bitmap thumbBmp = BitmapFactory.decodeResource(getResources(), R.drawable.send_music_thumb);
// Set a music thumbnail
msg.thumbData = Util.bmpToByteArray(thumbBmp, true);

// Build a Req
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("music");
req.message = msg;
req.scene = mTargetScene;
req.userOpenId = getOpenId();
// Call the API and send data to Weixin
api.sendReq(req);

Note: For music shared to Weixin, directly tap the shared content in a friend chat or Moments to redirect to a third-party app. Click the music sharing content at the top of the chat list to redirect to Weixin's native music player to play the music.


4. Example of Video Sharing

WXVideoObject (derived class of WXMediaMessage.IMediaObject, used to describe a video object)

Field Type Description Note
videoUrl String Video URL The maximum length is 10 KB.
videoLowBandUrl String Video URL of audio data in the low bandwidth environment The maximum length is 10 KB.

Note: Either videoUrl or videoLowBandUrl should be specified.

Demo of video sharing:

// Initialize a WXVideoObject and enter an URL
WXVideoObject video = new WXVideoObject();
video.videoUrl ="Video url";

// Initialize a WXMediaMessage object using a WXVideoObject object
WXMediaMessage msg = new WXMediaMessage(video);
msg.title ="Video title";
msg.description ="Video description";
Bitmap thumbBmp = BitmapFactory.decodeResource(getResources(), R.drawable.send_music_thumb);
msg.thumbData =Util.bmpToByteArray(thumbBmp,true);

// Build a Req
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("video");
req.message =msg;
req.scene = mTargetScene;
req.userOpenId = getOpenId();

// Call the API and send data to Weixin
api.sendReq(req);    

5. Example of Webpage Sharing

WXWebpageObject (derived class of WXMediaMessage.IMediaObject, used to describe a webpage object)

Field Type Description Note
webpageUrl String HTML URL The maximum length is 10 KB.

// Initialize a WXWebpageObject and enter an URL
WXWebpageObject webpage = new WXWebpageObject();
webpage.webpageUrl ="Webpage URL";

// Initialize a WXMediaMessage object using a WXWebpageObject object
WXMediaMessage msg = new WXMediaMessage(webpage);
msg.title ="Webpage title ";
msg.description ="Webpage description";
Bitmap thumbBmp = BitmapFactory.decodeResource(getResources(), R.drawable.send_music_thumb);
msg.thumbData =Util.bmpToByteArray(thumbBmp, true);

// Build a Req
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("webpage");
req.message =msg;
req.scene =mTargetScene;
req.userOpenId = getOpenId();

// Call the API and send data to Weixin
api.sendReq(req);

6. Example of Mini Program Sharing

WXMiniProgramObject (derived class of WXMediaMessage.IMediaObject, used to describe a Mini Program object)

Field Type Description Note
webpageUrl String Webpage URL compatible with older versions of Weixin The maximum length is 10 KB.
userName String Mini Program native ID This value can be obtained by logging in to the Mini Program admin console and selecting Settings > Basic Settings > Account Information.
path String Mini Program path
withShareTicket boolean Indicates whether to share with shareTicket. Generally, developers want users to get more information, such as group identifier, when they open a shared Mini Program. By setting withSharseTicket to true, when the sharing card is opened by other users in a group chat, a shareTicket can be obtained to get more shared information. For details, see How to Get More Information from a Shared Mini Program. This field is supported as of Weixin 6.5.13.
miniprogramType int Mini Program type Release version (default): WXMiniProgramObject.MINIPTOGRAM_TYPE_RELEASE;
Beta version: WXMiniProgramObject.MINIPROGRAM_TYPE_TEST;
Preview version: WXMiniProgramObject.MINIPROGRAM_TYPE_PREVIEW

WXMiniProgramObject miniProgramObj = new WXMiniProgramObject();
miniProgramObj.webpageUrl = "http://www.qq.com"; // Webpage URL compatible with older versions of Weixin
miniProgramObj.miniprogramType = WXMiniProgramObject.MINIPTOGRAM_TYPE_RELEASE;// 0: Release version; 1: Beta version; 2: Test version
miniProgramObj.userName = "gh_d43f693ca31f";     // Mini Program's original ID
miniProgramObj.path = "/pages/media";            // Mini Program's page path
WXMediaMessage msg = new WXMediaMessage(miniProgramObj);
msg.title = "Mini Program message Title";                    // Mini Program message title
msg.description = "Mini Program message Desc";               // Mini Program message desc
msg.thumbData = getThumb();                      // Cover image of the Mini Program message, which is less than 128 KB.

SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = buildTransaction("miniProgram");
req.message = msg;
req.scene = SendMessageToWX.Req.WXSceneSession;  // Only chats are supported.
api.sendReq(req);

Notes:

The app initiating a share operation and the Mini Program shall belong to the same Weixin Open Platform account. Sharing Mini Program messages in a chat is supported. Sharing on Moments is not allowed. If messages are received in the Weixin app below 6.5.6 or on iPad, Mini Program sharing will be automatically converted to webpage sharing. Developers must fill in the webpage URL field to ensure the URL can opened in an older version of Weixin.