Before we talk about the push style, let’s talk about the push.

What is a push?

In short, your app is actively pushing messages to the user. In the increasingly competitive app market, the flexible use of messaging is a great boost to user growth, promotion and retention.

Message push has different value in different push types.

Product function category push

System message notification: interactive message push of follow, like and comment in APP with social attributes; Logistics information notification in shopping APP……

Information activity notification: Timeliness information push of news media APP, holiday activity push of games and knowledge APP, welfare promotion information push and so on…

User operation class push

Silent User Awakening: Wake up silent users by actively touching the user’s message push.

Improve user activity: improve user activity by pushing information such as welfare activities.

To achieve the expected effect of message push, it is necessary to perfect the combination of push content, push style and time rhythm. Today, we will detail the 8 kinds of message push styles provided by Huawei push service and how to achieve them to help your application user growth and activity.

Huawei Push Kit is a message Push platform provided by Huawei, which establishes a message Push channel from the cloud to the terminal. Through the integrated push service, the message can be pushed to the user terminal in real time, so as to build a good user relationship and improve the user’s awareness and activity.

Push Kit currently supports various text styles, Inbox style, button style, and custom icon styles. You can define highly personalized messages to attract users. Through simple code and effect demonstration, this article will help you quickly understand and get used to Huawei Push message.

Message Style Introduction

First, let’s look at the structure of the notification bar message with the example given in the official development documentation.

As you can see from the figure above, the notification bar message from top to bottom contains message icon, application name, message digest, message arrival time, title, content, etc. In addition to the application name, all of these elements together form the notification bar message custom style.

As a comparison, I’ll show you the basic notification message style:

{"validate_only": false, "message": {"android": {"notification": {"body": "brief description of the message", "click_action": {3} "type" :, "title" : "here is the news title"}}, "token" : [" XXX "]}}

Note: A notification bar message must contain at least the above fields, otherwise it cannot be sent.

The following, respectively to explain to you each style of the way to define.

1) Custom message small icon

Push Kit provides two ways to set the notification bar message icon:

The icon file must be stored in the application’s /res/raw path, such as “res/raw/ic_launcher”, corresponding to the application’s local “/res/raw/ic_launcher.xxx”.

File. {" Android ": {" Notification ": {" Body ": "Notification "," Click_Action ": {" Type ": 3}, "Icon ": "/ raw/custom_notification_icon", "title" : "look at the upper left corner small icon"}}, "token" : [" XXX "]}

This is done by adding meta-data to the “AndroidManifest.xml” file of the application. The code is as follows:

<meta-data
    android:name="com.huawei.messaging.default_notification_icon"
    android:resource="@drawable/ic_push_notification" />

The meta-data metadata “name” is immutable, and the resource icon specified by “resource” is specified by you. It needs to be in the “res/drawable” directory of the application.

Analysis: Compare the two methods, method 1 is more flexible, just need to preset the small icon in advance in the client, the server can use different small icon according to the need.

2) Customize the message digest

A message summary is displayed to the right of the application name to briefly describe the content of the message. Set through the “NOTIFY_SUMMARY” field in the server-side API.

{"validate_only": false, "message": {"android": {"notification": {"body": "brief description of the message", "click_action": {"type": 3}, "notify_summary": "Here is the summary section..." , "title": "Here is the message title",}}, "token": [" XXX "]}}

3) Customize message arrival time (for presentation only)

As soon as Huawei Push server receives the developer’s message Push request, it will process and send it to the user immediately. Therefore, the actual arrival time of the message on the user’s phone is not customizable. However, the server-side API provides a custom time field “when” for notification bar message presentation and sorting. Once the developer specifies this parameter, the messages in the user notification bar are displayed and sorted according to this time.

In the test above, both messages were sent and received around 10:00, and the top message was sent earlier than the bottom message. If the “when” field is not set, the picture message should be displayed below. However, the display time is specified as “2021-04-29T01:26:23.045123456Z” by using the “WHEN” field when the message is sent below. So, when the actual display is sorted on the user’s phone, the message below shows the time set by the “When” field.

{"validate_only": false, "message": {"android": {"notification": {"body": "brief description of the message", "click_action": , {3} "type" : "title" : "here is a message title", "when", "the 2021-04-29 T01: he. 045123456 z"}}, "token" : [" XXX "]}}

Note: This parameter uses UTC time and must be less than the current time.

4) Custom message button

The notification bar message supports the addition of multiple buttons, which can be clicked to trigger the corresponding action.

{"validate_only": false, "message": {" Android ": {" Notification ": {" Body ": "You can add multiple buttons to the bottom of the push message. Click on the button to trigger the corresponding action ", "buttons": [{" action_type ": 0," name ":" learn more "}, {" action_type ": 3," name ":" willful neglect "}], "click_action" : {3} "type" :, "title" : "Shocked! This push style you must understand "}}, "token": [" XXX "]}}

Button action type: 0: open the application home page, 1: open the application custom page, 2: open the specified web page, 3: clear notice, 4: Huawei sharing function;

If the value of the “Name” field is in English, the name of the button is displayed in all uppercase letters when the notification message is displayed.

The above message, the change is mainly local style, do not involve the message’s title and body fields. These styles can be used in combination without affecting each other.

The three styles described below have some impact on each other because they involve the title and body fields and are not recommended to be used together when calling the server-side API.

5) Large text style

Early version push service, the default style only supports a single line of text, a single line of text support too few words, there will be incomplete defects. The large text style supports a single line title and multiple lines of text content (the current EMUI 9 system limits the display to 12 lines of Chinese or 14 lines of English, and EMUI 10&11 system limits the display to 11 lines of Chinese or 13 lines of English). The large text expands to look like this:

{"validate_only": false, "message": {"android": {"notification": {"big_body": "Large text style supports single line title and multiple lines of content text. The current EMUI 9 system limits the display to 12 lines of Chinese or 14 lines of English, and EMUI 10&11 system limits the display to 11 lines of Chinese or 13 lines of English. , "big_title": "here is the big text message title", "body":" brief description of the message content, free play ", "click_action": {"type": 3}, "style":1, "title": "Here is the message header"}}, "token": [" XXX "]}}

instructions

EMUI 9: The title and content displayed before large text expansion are taken from the “title” and “body” fields, not from the “big_title” and “big_body” fields.

EMUI 10: The title displayed before large text expansion is taken from the “title” field and the content is taken from the “big_body” field.

6) Inbox style

This style can also display multiple lines of text, but unlike the large text style, the Inbox style presents each line as a single line of text. Text content can be displayed in up to 5 lines, and “… “will be automatically added after each line. .

{"validate_only": false, "message": {"android": {"notification": {"body": "brief description of the message", "click_action": , {3} "type" : "inbox_content" : [" 1. The first thing you need to find an elephant ", "2. Delicious good drink cheat elephants to refrigerator", "3. Open the door", "4. Put the elephant into the fridge," "5. Close the fridge door]", "style" : 3, "title": "here is message title"}}, "token": [" XXX "]}}

Conclusion:

7) Localized message display

Notification message localization can also be understood as international multi-language display, which means that the notification message can display the title and content of the corresponding language according to the local language change of the mobile phone, so as to cover the common title and content of the message.



Push Kit provides two ways to display messages locally:

Completely through the REST API interface provided by the server;

{ "validate_only": false, "message": { "android": { "notification": { "body": "bbb", "body_loc_args": ["Jack"], "body_loc_key": "body_key", "click_action": { "type": 3 }, "multi_lang_key": { "title_key": { "en": "New Friend Request From % s", "useful" : "a Friend Request From % s"}, "body_key" : {" en ":" My name is % s. ", "useful" : "My name is % s." } }, "title": "ttt", "title_loc_args": ["Shanghai"], "title_loc_key": "title_key" } }, "token": ["xxx"] } }

Description:

1. The fields “title_loc_key” and “body_loc_key” correspond to the names of relevant fields in “multi_lang_key”;

2. The values of the “title_loc_args” and “body_loc_args” fields are mutable arrays of strings that are used to fill the placeholder %s in the values of the corresponding fields.

3. The “multi_lang_key” field currently supports up to three languages.

The server-side REST API is implemented in conjunction with the application’s native string resource file “strings.xml”.

{
    "validate_only": false,
    "message": {
        "android": {
            "notification": {
                "title": "ttt",
                "body": "bbb",
                "body_loc_args": ["Jack", "Shanghai"],
                "body_loc_key": "body_key",
                "click_action": {
                    "type": 3
                },
                "title_loc_key": "title_key"
            }
        },
        "token": ["xxx"]
    }
}

Define the string resource in the Android resource file “/res/values/strings.xml”.

Support for placeholders, with % followed by the location of the placeholder starting at 1 and $followed by the type of populated data.

Support for multiple languages, such as “/res/values-en/strings.xml” for English localization.

<string name="title_key">New Friend Request</string>
<string name="body_key">My name is %1$s, I am from %2$s.</string>

Add the code in the App, dynamically get the variable string, and the resource node string format, fill the content into the placeholder.

public class DemoHmsMessageService extends HmsMessageService { @Override public void onMessageReceived(RemoteMessage message) { String[] bodyArrays = message.getNotification().getBodyLocalizationArgs(); // Get content and format it String Key = getResources().getString(R.string.body_key); String body = String.format(key, bodyArrays[0], bodyArrays[1]); Log.i(TAG, body); }}

Analysis: Compare the two methods. Method 1 is flexible and does not need to change the client code. But Approach 2 can support more languages at the same time and is suitable for more globalized applications.

Visit the Huawei Developer Alliance website to learn more about it

Get the development guidance document

Address of Huawei Mobile Service Open Source Warehouse: GitHub and Gitee

The original link: https://developer.huawei.com/… Author: Pepper