Before talking about message push styles, let's talk about message push first.
What is news push?
In short, your app actively pushes messages to users. In the increasingly competitive application market, flexible use of message push has a great effect on user growth, promotion and retention.
Message push has different values in different push types.
product function push
System message notification: follow, praise and comment in social app with interactive message push; logistics information notification in shopping app, etc...
Information activity notifications: time-sensitive information pushes of news media apps, holiday activities of apps such as games and knowledge, and welfare promotion information pushes, etc...
user operation push
Wake up by silent users: wake up silent users with push notifications that actively touch users.
Increase user activity: combine the push of welfare activities and other information to increase user activity.
The realization of the expected effect of message push requires a perfect combination of push content, push style, and time rhythm. Today we will explain in detail the 8 message push styles and implementation methods provided by Huawei Push Service to help your app users grow and be active.
Huawei Push Kit (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, messages can be pushed to user terminals in real time, building a good user relationship, and improving user perception and activity.
Push Kit currently supports multiple text styles, Inbox styles, button styles, and custom icons. You can define highly personalized messages to attract users. This article uses a simple code and effect display to help you quickly understand and get started with Huawei Push news.
message style introduction
First, introduce the structure of the notification bar message with the help of the example given in the official development document.
As can be seen from the above figure, the notification bar message includes small message icons, application name, message summary, message arrival time, title, content, etc. from top to bottom. Among them, in addition to the application name, other elements together constitute the custom style of the notification bar message.
As a comparison of various styles, first I will show you the most basic notification bar message style:
{
"validate_only": false,
"message": {
"android": {
"notification": {
"body": "简要描述消息的内容,自由发挥啦",
"click_action": {
"type": 3
},
"title": "这里是消息标题"
}
},
"token": ["xxx"]
}
}
Note: A notification bar message must contain at least the above fields, otherwise it cannot be sent.
Below, I will explain how to customize each style.
1) Customize the small message icon
Push Kit provides two methods for setting small icons in the notification bar:
Send a downlink message through the server API to carry the "icon" field. The icon file must be stored in the application's /res/raw path, such as "res/raw/ic_launcher", which corresponds to the local application "/res/raw/ic_launcher.xxx"
文件。
{
"android": {
"notification": {
"body": "没错,消息小图标可以自定义哦",
"click_action": {
"type": 3
},
"icon": "/raw/custom_notification_icon",
"title": "快看左上角小图标"
}
},
"token": ["xxx"]
}
This is achieved by adding meta-data metadata to the "AndroidManifest.xml" file of the application. The reference code is as follows:
<meta-data
android:name="com.huawei.messaging.default_notification_icon"
android:resource="@drawable/ic_push_notification" />
Among them, the meta-data metadata "name" cannot be changed, and the resource icon specified by "resource" is specified by you and needs to be in the "res/drawable" directory of the application.
Analysis: Comparing the two methods, method 1 is more flexible. You only need to preset the small icons on the client in advance, and the server can use different small icons as needed.
2) Custom message summary
The message summary is displayed on the right side of the application name and is used to briefly describe the content of the message. Set through the "notify_summary" field in the server API.
{
"validate_only": false,
"message": {
"android": {
"notification": {
"body": "简要描述消息的内容,自由发挥啦",
"click_action": {
"type": 3
},
"notify_summary": "这里是摘要部分……",
"title": "这里是消息标题",
}
},
"token": ["xxx"]
}
}
3) Custom message arrival time (for display only)
Once the Huawei Push server receives the developer's message push request, it will immediately process and send it to the user. Therefore, the time when the message actually arrives on the user's mobile phone cannot be customized. However, the server-side API provides a custom time field "when" for displaying and sorting messages in the notification bar. Once the developer specifies this parameter, the messages in the user notification bar will be displayed and sorted according to this time.
In the test above, both messages were sent and received around 10 o'clock, and the message above was sent earlier than the message below. If the "when" field is not set, the picture message should be displayed below. But because the "when" field is used to specify the display time as "2021-04-29T01:26:23.045123456Z" when the message below is sent. Therefore, when actually sorting and displaying on the user's mobile phone, the message below shows the time set in the "when" field.
{
"validate_only": false,
"message": {
"android": {
"notification": {
"body": "简要描述消息的内容,自由发挥啦",
"click_action": {
"type": 3
},
"title": "这里是消息标题",
"when": "2021-04-29T01:26:23.045123456Z"
}
},
"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 adding multiple buttons, and clicking the button can trigger the corresponding action.
{
"validate_only": false,
"message": {
"android": {
"notification": {
"body": "推送消息底部可添加多个按钮,点击按钮可以出发相应的动作",
"buttons": [{
"action_type": 0,
"name": "了解更多"
}, {
"action_type": 3,
"name": "任性忽视"
}],
"click_action": {
"type": 3
},
"title": "震惊!这个推送样式你必须了解"
}
},
"token": ["xxx"]
}
}
Description: Button action type: 0: Open the application homepage, 1: Open the application custom page, 2: Open the specified web page, 3: Clear notification, 4: Huawei share function;
If the value of the "name" field is in English, the button name will be displayed in all capital letters when the notification message is displayed.
For the above message, the main changes are the local style, and the title and body fields of the message are not involved. These styles can be used in random combinations without affecting each other.
The three styles introduced below have some influence on each other because they involve the title and body fields. It is not recommended to use them at the same time when calling the server-side API.
5) Large text style
The early version of the push service only supports single-line text in the default style. The single-line text supports too few words, which may cause incomplete expression. The large text style supports single-line title and multi-line content text (the current EMUI 9 system limits the display of up to 12 lines of Chinese or 14 lines of English, and the EMUI 10&11 system limits the display of up to 11 lines of Chinese or 13 lines of English). The effect of expanding the large text is as follows:
{
"validate_only": false,
"message": {
"android": {
"notification": {
"big_body": "大文本样式支持标题单行,内容文字多行。当前EMUI 9系统限制最多显示12行中文或者14行英文,EMUI 10&11系统限制最多显示11行中文或者13行英文。",
"big_title": "这里是大文本消息标题",
"body": "简要描述消息的内容,自由发挥啦",
"click_action": {
"type": 3
},
"style":1,
"title": "这里是消息标题"
}
},
"token": ["xxx"]
}
}
Description
EMUI 9: The title and content displayed before the large text is expanded are taken from the "title" and "body" fields, not the content of the "big_title" and "big_body" fields.
EMUI 10: The title displayed before the large text is expanded 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 different from the large text style, the Inbox style treats each line of content as an independent single line of text to display. The text content can display up to 5 lines, and "..." is automatically added after each line of content cannot be displayed.
{
"validate_only": false,
"message": {
"android": {
"notification": {
"body": "简要描述消息的内容,自由发挥啦",
"click_action": {
"type": 3
},
"inbox_content": ["1.首先你需要找到一头大象","2.好吃好喝把大象骗到冰箱旁","3.打开冰箱门","4.把大象塞进冰箱","5.关上冰箱门"],
"style": 3,
"title": "这里是消息标题"
}
},
"token": ["xxx"]
}
}
to sum up:
7) Message localization display
Notification message localization can also be understood as internationalized multilingual display, which means that the notification message can display the title and content of the corresponding language according to the local language of the mobile phone, thereby covering the general title and content of the message.
Push Kit provides two methods for localized display of messages:
Completely implemented 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",
"zh": "来自%s的好友请求"
},
"body_key": {
"en": "My name is %s.",
"zh": "我叫%s。"
}
},
"title": "ttt",
"title_loc_args": ["Shanghai"],
"title_loc_key": "title_key"
}
},
"token": ["xxx"]
}
}
Description:
1. The "title_loc_key" and "body_loc_key" fields correspond to the names of the relevant fields in "multi_lang_key";
2. The values of the "title_loc_args" and "body_loc_args" fields are both a variable string array, which is used to fill the placeholder %s in the value of the corresponding field;
3. The "multi_lang_key" field currently supports up to three languages.
The server-side REST API is implemented in cooperation with the local string resource file "strings.xml" of the application.
{
"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 string resources in the Android resource file "/res/values/strings.xml".
Supports placeholders,% is the position of the placeholder, starting from 1, and $ is the type of data to be filled.
Support multiple languages, such as "/res/values-en/strings.xml" for English localization adaptation.
<string name="title_key">New Friend Request</string>
<string name="body_key">My name is %1$s, I am from %2$s.</string>
Add code to the App, dynamically obtain the variable string, format the string of the resource node, and fill the content into the placeholder.
public class DemoHmsMessageService extends HmsMessageService {
@Override
public void onMessageReceived(RemoteMessage message) {
String[] bodyArrays = message.getNotification().getBodyLocalizationArgs();
// 获取内容并进行格式化
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 to use and does not require modification of the client code. However, Method 2 can support more languages at the same time and is suitable for applications with a higher degree of globalization.
Visit Huawei Developer Alliance official website to learn more about it
Get development guide document
Huawei Mobile Services open source warehouse address: GitHub , Gitee
Original link: https://developer.huawei.com/consumer/cn/forum/topic/0202557858360000310?fid=18
Original Author: Pepper
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。