[Huawei Developers] Freemind R:
1. About the Function

Topic-based messaging is a function in Push Kit that seeks to boost user engagement and retention, by ensuring that users receive messages on topics that they have subscribed to, such as photography, sports, and food, providing a rich array of information on topics of interest.

2. Background

During busy shopping festivals, e-commerce platforms tend to make purchase reservations broadly accessible for a wide range of products. Thanks to push channels, merchants can send messages recommending products to users in a timely manner. However, not all users will have demonstrated interest in a product, and many will be turned off by the sheer flood of messages. Topic-based messaging, on the other hand, only reaches users who have subscribed to specific topics, helping developers and merchants avoid this pitfall.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201117112934.99018810507646679545407828591909:50511123020137:2800:9985197E12CCDD481C4869F72D58A6209D2FF8B6E4439850ACEC68A10277B483.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201117112934.99018810507646679545407828591909:50511123020137:2800:9985197E12CCDD481C4869F72D58A6209D2FF8B6E4439850ACEC68A10277B483.png[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201117112934.43511146185718917390420917163722:50511123020137:2800:9C323FCEF46407E2DA76489E098915315E19751BC54D86EE85D6FD9DC33112D8.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201117112934.43511146185718917390420917163722:50511123020137:2800:9C323FCEF46407E2DA76489E098915315E19751BC54D86EE85D6FD9DC33112D8.png[/IMG]

3. Process

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201117113237.27581770141204506551021443903468:50511123065752:2800:09373DAF7ED339C09CA94F8448AB433BA90D1629458ADD17186F49D1D4E68379.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201117113237.27581770141204506551021443903468:50511123065752:2800:09373DAF7ED339C09CA94F8448AB433BA90D1629458ADD17186F49D1D4E68379.png[/IMG]

4. Key Integration Steps and Code

(1) Integrate the HMS Core Push SDK.

For details, please refer to the Push SDK integration document at https://developer.huawei.com/consum...s-V5/service-introduction-0000001050040060-V5.

(2) Configure automatic initialization. After the configuration, a token is returned by onNewToken each time that the app is started. The topic-based messaging does not depend on a token, but the app will need to obtain the token first.

<meta-data

android
:name
="push_kit_auto_init_enabled"

android
:value
="true"
/>

(3) Set topic to a product ID. When a user taps the reservation button, the subscribe method is called to subscribe to the topic.

/**
* to subscribe to topics in asynchronous mode.
*/
private void addTopic(String topic) {
try {
HmsMessaging.getInstance(MainActivity2.this)
.subscribe(topic)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(Task<Void> task) {
if (task.isSuccessful()) {
Log.i(TAG, "subscribe Complete");
changToCancelAppointment();
isAppointment = true;

showLog("subscribe successful");
} else {
isAppointment = false;
changeToAppointment();
showLog("subscribe failed: ret=" + task.getException().getMessage());
}
}
});
} catch (Exception e) {
isAppointment = false;
changeToAppointment();
showLog("subscribe failed: exception=" + e.getMessage());
}

}

(4) Call the downlink messaging API to push messages based on topic. Use Postman to simulate sending a message. The packet is as follows:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201117113634.97451623869539013644584321390200:50511123065752:2800:D85F9C9407E35BC834316D3699C5BBCAB6D8FCF45DA065B17878CBD7118FCDCF.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201117113634.97451623869539013644584321390200:50511123065752:2800:D85F9C9407E35BC834316D3699C5BBCAB6D8FCF45DA065B17878CBD7118FCDCF.png[/IMG]

(5) When reserving a watch purchase, the user may wish to purchase other watches in the same price range and of the same model. In this case, push the pre-sales information of these watches as a group to the user by condition. In the following figure, if the user has subscribed to one of watch123456, watch32165, and watch321684, the pre-sales information about the other watches will be pushed to the user.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201117113801.71002867105498742186042235061649:50511123065752:2800:83F4E00E438949F5676BE2FC96946D7B046ABD4D241D9E213816F958AE2F596E.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201117113801.71002867105498742186042235061649:50511123065752:2800:83F4E00E438949F5676BE2FC96946D7B046ABD4D241D9E213816F958AE2F596E.png[/IMG]

(6) When the user cancels the reservation, call the unsubscribe method. After the cancellation, the user will no longer receive the pre-sales information for the watch.

/**
* to unsubscribe to topics in asynchronous mode.
*/
private void deleteTopic(String topic) {
try {
HmsMessaging.getInstance(MainActivity2.this)
.unsubscribe(topic)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(Task<Void> task) {
if (task.isSuccessful()) {
showLog("unsubscribe successful");
changeToAppointment();
isAppointment = false;
} else {
isAppointment = true;
showLog("unsubscribe failed: ret=" + task.getException().getMessage());
changToCancelAppointment();
}
}
});
} catch (Exception e) {
showLog("unsubscribe failed: exception=" + e.getMessage());
isAppointment = true;
changeToAppointment();
}

}

5. Display Effects

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201117114036.42980871870200846491076516393664:50511123065752:2800:4EA2B24EE28C5FB3140E16F79A24052887C4BD9BECA92AA109DB9B16E0BCFD9F.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201117114036.42980871870200846491076516393664:50511123065752:2800:4EA2B24EE28C5FB3140E16F79A24052887C4BD9BECA92AA109DB9B16E0BCFD9F.png[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201117114036.42221953586008586019748402977414:50511123065752:2800:C0621A0FABF7FFDBAC6B6E78FD31AEA8F62F503A23D8179D96BFBFC8D0E15350.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201117114036.42221953586008586019748402977414:50511123065752:2800:C0621A0FABF7FFDBAC6B6E78FD31AEA8F62F503A23D8179D96BFBFC8D0E15350.png[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201117114036.20341846194314374954502742392450:50511123065752:2800:17E7003042C7727ADD5EE2FE5A764480F42A8BAD4DF62191406A4A1D49C87F1C.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/560/358/2640852000006560358.20201117114036.20341846194314374954502742392450:50511123065752:2800:17E7003042C7727ADD5EE2FE5A764480F42A8BAD4DF62191406A4A1D49C87F1C.png[/IMG]

(1) Prior to reservation (2) At the time of reservation (3) Reservation canceled

6. Others

Topic-based messaging does not limit the number of subscriptions to each topic. However, Push Kit has the following restrictions:

(1) An app instance can subscribe to a maximum of 2,000 topics.

(2) For Huawei devices running EMUI 10.0 or later, the version of HMS Core (APK) must be 3.0.0 or later. For Huawei devices running EMUI earlier than 10.0, the version of HMS Core (APK) must be 4.0.3 or later. HMS Core (APK) of a later version supplements the functions that are missing in EMUI of an earlier version.

(3) The number of topics through which messages can be pushed simultaneously cannot exceed 100.