Reel in Users with Topic-based Messaging

Photo by Yura Fresh on Unsplash

Reel in Users with Topic-based Messaging

Smartphones are everywhere these days and play an important role in our daily lives. The popularization of smartphones has led to a wave of mobile apps hitting the market. So, the homogeneous competition between apps is more fierce than ever and developers are trying their best to figure out how best to attract users to their apps. Most developers resort to message pushing, which leads to an exponential growth of pushed messages. As a result, users quickly become flooded with pushed messages and struggle to find the information they need.

The explosion of pushed messages means that crafting eye-catching messages that appeal to users has never been more crucial and challenging. Like many other developers, I also encountered this problem. I have pushed many promotional messages to users of my app, but the outcome is not particularly positive. So I wondered if it is possible to push messages only to a specific user group, for example, sending car-related product promotion messages to users with cars.

It occurred to me that I once came across a service called HMS Core Push Kit, which provides a function that allows developers to send topic-based messages. With this function, developers can customize messages by topic to match users' habits or interests and then regularly send these messages to user devices via a push channel. For example, a weather forecast app can send weather forecast messages concerning a city that users have subscribed to, or a movie ticket-booking app can send reminders to users who have followed a particular movie.

Isn't that exactly what I need? So I decided to play about with this function, and it turned out to be very effective. Below is a walkthrough of how I integrated this function into my app to send topic-based messages. I hope this will help you.

Development Procedure

Generally, three development steps are required for using the topic-based messaging function.

Step 1: Subscribe to a topic within the app.

Step 2: Send a message based on this topic.

Step 3: Verify that the message has been received.

The figure below shows the process of messaging by topic subscription on the app server.

You can manage topic subscriptions in your app or on your app server. I will detail the procedures and codes for both of these methods later.

Key Steps and Coding

Managing Topic Subscription in Your App

The following is the sample code for subscribing to a topic:

public void subtopic(View view) {
    String SUBTAG = "subtopic";
    String topic = "weather";
    try {
        // Subscribe to a topic.
    HmsMessaging.getInstance(PushClient.this).subscribe(topic).addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(Task<Void> task) {
                if (task.isSuccessful()) {
                    Log.i(SUBTAG, "subscribe topic weather successful");
                } else {
                    Log.e(SUBTAG, "subscribe topic failed,return value is" + task.getException().getMessage());
                }
            }
        });
    } catch (Exception e) {
        Log.e(SUBTAG, "subscribe faied,catch exception:" + e.getMessage());
    }
}

The figure below shows that the topic is successfully subscribed to.

The following is the sample code for unsubscribing from a topic:

public void unsubtopic(View view) {
    String SUBTAG = "unsubtopic";
    String topic = "weather";
    try {
        // Subscribe to a topic.
        HmsMessaging.getInstance(PushClient.this).unsubscribe(topic).addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(Task<Void> task) {
                if (task.isSuccessful()) {
                    Log.i(SUBTAG, "unsubscribe topic successful");
                } else {
                    Log.e(SUBTAG, "unsubscribe topic failed,return value is" + task.getException().getMessage());
                }
            }
        });
    } catch (Exception e) {
        Log.e(SUBTAG, "subscribe faied,catch exception:" + e.getMessage());
    }
}

The figure below shows that the topic is successfully unsubscribed from.

Managing Topic Subscription on Your App Server

(1) Obtain an access token.

You can call the API (https://oauth-login.cloud.huawei.com/oauth2/v3/token) of the HMS Core Account Kit server to obtain an app-level access token for authentication.

  • Request for obtaining an access token

      POST /oauth2/v3/token HTTP/1.1
      Host: oauth-login.cloud.huawei.com
      Content-Type: application/x-www-form-urlencoded
    
      grant_type=client_credentials&
      client_id=<APP ID >&
      client_secret=<APP secret >
    
  • Demonstration of obtaining an access token

(2) Subscribe to and unsubscribe from topics.

Your app server can subscribe to or unsubscribe from a topic for your app by calling the corresponding subscription and unsubscription APIs of the Push Kit server. The URLs of the subscription and unsubscription APIs differ slightly, but the header and body of the subscription request are the same as those of the unsubscription request. The details are as follows:

  • URL of the subscription API

      https://push-api.cloud.huawei.com/v1/[appid]/topic:subscribe
    
  • URL of the unsubscription API

      https://push-api.cloud.huawei.com/v1/[appid]/topic:unsubscribe
    
  • Example of the request header, where the token following Bearer is the access token obtained in the previous step

      Authorization: Bearer CV0kkX7yVJZcTi1i+uk...Kp4HGfZXJ5wSH/MwIriqHa9h2q66KSl5
      Content-Type: application/json
    
  • Example of the request body

      {
          "topic": "weather",
          "tokenArray": [
              "AOffIB70WGIqdFJWJvwG7SOB...xRVgtbqhESkoJLlW-TKeTjQvzeLm8Up1-3K7",
              "AKk3BMXyo80KlS9AgnpCkk8l...uEUQmD8s1lHQ0yx8We9C47yD58t2s8QkOgnQ"
          ]
      }
    
  • Request demonstration

Sending Messages by Topic

You can send messages based on a created topic through the HTTPS protocol. The sample code for HTTPS-based messaging is as follows:

{
    "validate_only": false,
    "message": {
        "notification": {
            "title": "message title",
            "body": "message body"
        },
        "android": {
            "notification": {
                "click_action": {
                    "type": 1,
                    "action": "com.huawei.codelabpush.intent.action.test"
                }
            }
        },
        "topic": "weather"
    }
}

The figure below shows that the message is received and displayed on the user device.

Precautions

  • ​​​​​​​Your app can subscribe to any existing topics, or create new topics. When subscribing to a topic that does not exist, your app will request Push Kit to create such a topic. Then, any other app can subscribe to this topic.

  • The Push Kit server provides basic APIs for managing topics. You can subscribe to or unsubscribe from a topic using a maximum of 1000 tokens at a time. Each app can have a maximum of 2000 different topics.

  • The subscription relationship between the topic and token takes effect one minute after the subscription is complete. After the subscription takes effect, you'll be able to specify one topic, or a set of topic matching conditions to send messages in batches.

That's all for integrating the topic-based messaging function. In addition to this function, I also found that Push Kit provides functions such as scenario-based messaging and geofence-based messaging, which I think are very useful because they allow apps to push messages that are suitable for users' scenarios to users.

For example, with the scenario-based messaging function, an app can automatically send messages to users by scenario, such as when headsets are inserted, the Bluetooth car stereo is disconnected, or the motion status changes. With the geofence-based messaging function, an app can send messages to users by location, such as when users enter a shopping mall or airport and stay there for a specified period of time.

These functions, I think, can help apps improve user experience and user engagement. If you want to try out these functions, click here to view the official website.

Conclusion

The key to a successful app that stands out from the crowd is crafting messages that immediately grasp users' attention. This requires customizing messages by topic to match users' habits or interests, then regularly sending these messages to user devices via a push channel. As I illustrated earlier in this article, my solution for doing so is to integrate the topic-based messaging function in Push Kit, and it turns out to be very effective. If you have similar demands, have a try on this function and you may be surprised.

Did I miss anything? I'm looking forward to hearing your ideas.