tencent cloud

Mini Game Subscription Message Practical Tutorial
Last updated: 2025-08-11 14:30:23
Mini Game Subscription Message Practical Tutorial
Last updated: 2025-08-11 14:30:23

Introduction

Based on the superapp notification channel, we enable developers to create subscription messages, resulting in a closed loop of services and an improved user experience for superapp users.
Developers can initiate message subscriptions to users within the game. They must track whether users have successfully subscribed and the number of successful subscriptions in the backend.
After users subscribe to specified message content in the mini game, developers can send template messages to them using the send API in the backend. Once a user successfully subscribes, developers can send a template message to that user at an appropriate time within the game.
To implement the subscription message feature, follow these steps:

Process description

Prerequisites

To enable the subscription message feature for both the superapp and mini game, your superapp must have push services activated; otherwise, mini game subscription messages won't reach users through the superapp's push channel.

Add message templates

Developers need to log in to the console and go to Mini game management - Message subscription,select an appropriate message template from the public template library, configure the push fields, and add it to My templates. For detailed steps, see Mini game management - Message subscription.

Trigger subscription messages in the mini game

Step 1: Get the template ID

After the message template is added as instructed above, you will receive the configured template ID, which will be used for subsequent message subscription operations.

Step 2: Obtain sending permissions

In the mini game, you can invoke the superapp subscription message interface using wx.requestSubscribeMessage to allow users to subscribe to messages in one go. After the user interacts with the interface, the result of their subscription action will be returned. If the result is "accept," it indicates that the user has agreed to subscribe to the template message corresponding to the template ID, and the template is available.

Exmaple:

JAVASCRIPT
// tmplIds:Array of template IDs that need to be subscribed
requestSubscribeMessage(tmplIds) {
wx.requestSubscribeMessage({
tmplIds,
success: (res) => {
console.log('wx.requestSubscribeMessage success', res)
const keysWithAccept = Object.entries(res)
.filter(([key, value]) => value === "accept")
.map(([key]) => key);
if (keysWithAccept.length > 0) {
// Send subscription message
this.orderSubscribe(keysWithAccept)
} else {
wx.showModal({
title: 'No available message templates',
confirmText: 'Confirm',
showCancel: false
})
}
},
fail: (res) => {
console.log('wx.requestSubscribeMessage fail', res)
wx.showModal({
title: 'wx.requestSubscribeMessage fail',
confirmText: 'Confirm',
content: `${res.errMsg}【${res.errCode}】`,
showCancel: false
})
}
})
}

Step 3: Call the API to send the subscription message

When a user triggers an operation in the mini game that requires receiving messages, the mini game calls a custom API to communicate with its backend service, passing along the available template ID. The mini game server will then invoke the send API provided by the SAS server (OpenServer) to send the subscription message.

Send subscription messages from the mini game server

Before sending subscription messages, the mini game server must integrate with the getAccessToken API.
Typically, the mini game server needs to retrieve the subscription relationships, as shown in the example code with the this.orderSubscribe() API. The server should persist the successfully subscribed templates, subscription times, and subscriber (openid) information.
After the subscription action is completed, sending the message is usually a future action. The mini game server should provide a platform for message dispatch or use other middleware or APIs to trigger message sending.
When subscribing to messages, the input parameter tmplIds can be validated for template ID effectiveness through backend APIs.
Please refer to the following API for sending subscription messages from the backend. Pay special attention to the format of the data field, as shown in the example below.
If the subscription message template is as follows:
name: {{name01.DATA}}
amount: {{amount01.DATA}}
journey: {{thing01.DATA}}
date: {{date01.DATA}}
The data structure of the corresponding message is as follows:
{
"touser": "OPENID",
"template_id": "TEMPLATE_ID",
"page": "index",
"data": {
"name01": {
"value": "who "
},
"amount01": {
"value": "$100"
},
"thing01": {
"value": "from beijing to shanghai"
} ,
"date01": {
"value": "2018-01-01"
}
}
}

Superapp receives and displays subscription messages


In the previous step, the mini game backend pushes subscription messages to the superapp backend through the SAS platform. The superapp needs to receive the messages through an API and complete the subsequent custom business logic. Usually, the superapp backend needs to provide an interface for pulling a message list to the superapp, so that the superapp can obtain subscription message data pushed from the mini game and display it to the superapp's users.
Note:
The API for superapp to retrieve message list needs to be developed on the superapp backend.
Here are our recommendations for how the superapp can receive and display subscription messages:
If your superapp has social chat feature, we suggest placing the notification entry for subscription messages in the chat list, making it easy for users to view them in real time.

In addition, if your superapp has a system message feature, you can also use the system message entry for subscription messages.

You can also decide the entry of the subscription messages according to your own preferences. We recommend placing the entry in a more prominent position.
Once you have the entry point for subscription messages, the next step is to create a custom subscription message list page, which we will refer to as the "Message card" page. This page will be used to display subscription messages. You can refer to the image below for UI implementation.

Below is an example on how to implement the subscription message list and “Message card”.
When entering the "Message card" page, call the superapp backend to retrieve the subscription messages. Example:
// Define an array variable jsonDatas to store the retrieved data, initialized to nil
BLOCK_ARRAY jsonDatas = nil;

// Call getMessage method of the singleton SubscriptionManager to retrieve message data
// Parameters:
// - token: The token for the logged-in user
// - appId: The mini game key from the configuration file (tcmpp-ios-configurations.json or tcmpp-android-configurations.json)
// Successful callback:
// - message: The array of retrieved message data
// Failed callback:
// - error: Error information
[SubscriptionManager.sharedInstance getMessage: UserInfo.sharedInstance.token
appId: MiniAppSDKManager.sharedInstance.configAppKey
success:^(ARRAY message) {
// Check if message is of array type; if not, return directly
if (![message isArray]) {
return;
}

// Check if the message array is empty; if so, return directly
if ([message isEmpty]) {
return;
}

// Assign the retrieved message data array to the jsonDatas variable
jsonDatas = message;

// Create a mutable array arrayDatas to store the parsed data models
MUTABLE_ARRAY arrayDatas = [NSMutableArray array];

// Iterate through the jsonDatas array
for (DICTIONARY json in jsonDatas) {
// Parse each json data into a SubscribeInfoModel object
SubscribeInfoModel *model = [[SubscribeInfoModel alloc] initWithJsonDatas: json];

// Add the model object to the arrayDatas array
[arrayDatas addObject: model];
}

// Assign the arrayDatas array to the self.modelDatas property
self.modelDatas = [NSArray arrayWithArray: arrayDatas];

// Asynchronously execute the tableView's reloadData method on the main thread to refresh the table data
[self.tableView performSelectorOnMainThread: @selector(reloadData)
withObject: nil
waitUntilDone: YES];
}
failure:^(ERROR error) {
// Handle the failure to retrieve message data; provide failure notifications based on business scenarios
}];
In the example code, we use the superapp's encapsulated getMessage API, passing in the user token and mini game key parameters to retrieve the subscription card data. In the success callback, we deserialize the corresponding subscription data, set it in the view container, and then refresh the display.
Tap the card to enter the corresponding mini game page. Example:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// Get the subscription model object corresponding to the tapped card
SubscribeInfoModel *model = self.modelDatas[indexPath.row];
// Pass the parameters of the subscription model object and call the SDK's method to open the mini game
[[TMFMiniAppSDKManager sharedInstance] startUpMiniAppWithAppID:model.MnpId verType:model.vertype scene:0 firstPage:model.Page paramsStr:nil parentVC:self completion:^(NSError * _Nullable error) {
if(error) {
// Opening failed; provide a notification based on the business scenario
return;
}
// Opening succeeded; provide a notification based on the business scenario
}];
}
In the example, we retrieve the corresponding subscription object by tapping on a list element. By calling the startUpMiniAppWithAppID method, we pass in parameters such as the mini game appid, its status, and the entry page to open the mini game.

Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback