在談訊息推送樣式之前,先來說說訊息推送,
什么是訊息推送?
簡而言之,就是你的app主動向用戶推送訊息,在競爭日益激烈的應用市場,靈活運用訊息推送,對用戶增長、促活和留存有很大的促進作用,

訊息推送在不同推送型別中有不同的價值體現,
產品功能類推送
- 系統訊息通知:社交屬性的app中的關注、贊評轉互動訊息推送;購物app中物流資訊通知等……
- 資訊活動通知:新聞媒體app的時效性訊息推送、游戲和知識等app的節假榷訓動、福利促銷資訊推送等……
用戶運營類推送
- 沉默用戶喚醒:用主動觸及用戶的訊息推送方式喚醒沉默用戶,
- 提高用戶活躍:結合福利活動等資訊的推送,提高用戶的活躍,
訊息推送預期效果的實作,需要推送內容、推送樣式以及時間節奏的完美結合,今天我們將詳解華為推送服務所提供的8種訊息推送樣式及其實作方法,為你的應用用戶增長和活躍助力,
華為推送服務(Push Kit)是華為提供的訊息推送平臺,建立了從云端到終端的訊息推送通道,通過集成推送服務可以實時推送訊息到用戶終端,構筑良好的用戶關系,提升用戶的感知度和活躍度,
Push Kit當前支持多種文本樣式,Inbox樣式,按鈕樣式以及自定義圖示等樣式,你可定義極具個性化的訊息來吸參考戶,本文通過簡單的代碼和效果展示,幫助你快速了解并上手華為Push訊息,
訊息樣式介紹
首先,借助官方開發檔案給出的示例,介紹通知欄訊息的結構,

從上圖可以看出,通知欄訊息從上到下依次包含訊息小圖示、應用名稱、訊息摘要、訊息到達時間、標題、內容等部分,這其中,除了應用名稱,其他要素共同構成了通知欄訊息自定義樣式,
作為各種樣式的對比,首先給大家展示最基本的通知欄訊息樣式:

{
"validate_only": false,
"message": {
"android": {
"notification": {
"body": "Timely and accurate message pushing can ensure that your app's reach extends to its target users, resulting in more user engagement, and an enhanced user experience, while maximizing business value.",
"click_action": {
"type": 3
},
"title": "Push Kit"
}
},
"token": ["xxx"]
}
}
說明:一條通知欄訊息,至少需要包含以上欄位,否則無法發送,
下面,分別給大家講解各個樣式自定義的方式,
1)自定義訊息小圖示

Push Kit提供了兩種設定通知欄訊息小圖示的方法:
- 通過服務端API發送下行訊息攜帶“icon”欄位,圖示檔案必須存放在應用的/res/raw路徑下,例如“res/raw/ic_launcher”,對應應用本地的“/res/raw/ic_launcher.xxx”檔案,
{
"android": {
"notification": {
"body": "Timely and accurate message pushing can ensure that your app's reach extends to its target users, resulting in more user engagement, and an enhanced user experience, while maximizing business value.",
"click_action": {
"type": 3
},
"icon": "/raw/custom_notification_icon",
"title": "Push Kit"
}
},
"token": ["xxx"]
}
- 通過應用的“AndroidManifest.xml”檔案添加meta-data元資料來實作,參考代碼如下:
<meta-data
android:name="com.huawei.messaging.default_notification_icon"
android:resource="@drawable/ic_push_notification" />
其中,meta-data元資料“name”不可改變,“resource”指定的資源圖示由您指定,需要在應用的“res/drawable”目錄下,
分析:對比兩種方法,方法1更加靈活,只需要提前把小圖示預置在客戶端,服務端就可以根據需要使用不同的小圖示,
2)自定義訊息摘要
訊息摘要展示在應用名右側,用于簡要描述訊息的內容,通過服務端API中的“notify_summary”欄位進行設定,

{
"validate_only": false,
"message": {
"android": {
"notification": {
"body": "Timely and accurate message pushing can ensure that your app's reach extends to its target users, resulting in more user engagement, and an enhanced user experience, while maximizing business value.",
"click_action": {
"type": 3
},
"notify_summary": "HCM",
"title": "Push Kit",
}
},
"token": ["xxx"]
}
}
3)自定義訊息到達時間(僅用于展示)
華為Push服務器一旦接收到開發者的訊息推送請求,就會立即處理并發送給用戶,因此,訊息實際到達用戶手機的時間是無法自定義的,但是,服務端API提供了用于通知欄訊息展示、排序的自定義時間欄位“when”,開發者一旦指定此引數,用戶通知欄的訊息將根據此時間進行展示和排序,

上圖的測驗中,兩條訊息都是在20點左右發送并收到的,且帶有圖片的訊息發送時間早于另一條訊息,如果沒有設定“when”欄位,圖片訊息應該展示在下面,但是由于無圖片的訊息發送時使用“when”欄位指定展示時間為"2021-04-19T07:10:08.045123456Z",因此,實際在用戶手機上排序展示時,無圖訊息展示了“when”欄位設定的時間,
{
"validate_only": false,
"message": {
"android": {
"notification": {
"body": "Timely and accurate message pushing can ensure that your app's reach extends to its target users, resulting in more user engagement, and an enhanced user experience, while maximizing business value.",
"click_action": {
"type": 3
},
"title": "Push Kit",
"when": "2021-04-19T07:10:08.045123456Z"
}
},
"token": ["xxx"]
}
}
說明:此引數使用UTC時間,且必須小于當前時間,
4)自定義訊息按鈕
通知欄訊息支持添加多個按鈕,點擊按鈕可以觸發相應的動作,

{
"validate_only": false,
"message": {
"android": {
"notification": {
"body": "Timely and accurate message pushing can ensure that your app's reach extends to its target users, resulting in more user engagement, and an enhanced user experience, while maximizing business value.",
"buttons": [{
"action_type": 0,
"name": "Learn more"
}, {
"action_type": 3,
"name": "Ignore"
}],
"click_action": {
"type": 3
},
"title": "Push Kit"
}
},
"token": ["xxx"]
}
}
說明:按鈕動作型別:0:打開應用首頁,1:打開應用自定義頁面,2:打開指定的網頁,3:清除通知,4:華為分享功能;
“name”欄位的值如果為英文,通知訊息展示時按鈕名字會以全大寫字母展示,
上述訊息,改變的主要是區域樣式,不涉及訊息的title和body欄位,這些樣式可以隨意搭配使用,相互之間不影響,
下面介紹的3中樣式,由于涉及title和body欄位,因此相互之間存在一些影響,呼叫服務端API時不建議同時使用,
1)大文本樣式
早期版本推送服務,默認樣式下僅支持單行文本,單行文本支持的字數太少,會有表達不全的缺陷,大文本樣式支持標題單行,內容文字多行(當前EMUI 9系統限制最多顯示12行中文或者14行英文,EMUI 10&11系統限制最多顯示11行中文或者13行英文),大文本展開后效果如下:

{
"validate_only": false,
"message": {
"android": {
"notification": {
"big_body": "HUAWEI Push Kit is a messaging service provided for you. It establishes a messaging channel from the cloud to devices. By integrating Push Kit, you can send messages to your apps on users' devices in real time. This helps you maintain closer ties with users and increases user awareness of and engagement with your apps. The following figure shows the process of sending messages from the cloud to devices.",
"big_title": "HUAWEI Push",
"body": "Timely and accurate message pushing can ensure that your app's reach extends to its target users, resulting in more user engagement, and an enhanced user experience, while maximizing business value.",
"click_action": {
"type": 3
},
"style":1,
"title": "Push Kit"
}
},
"token": ["xxx"]
}
}
說明
EMUI 9:大文本展開之前顯示的標題與內容取自“title”與“body”欄位,非“big_title”與“big_body”欄位的內容,
EMUI 10:大文本展開之前顯示的標題取自“title”欄位,內容取自“big_body”欄位,
2)Inbox樣式
此樣式也可以展示多行文本,但是不同于大文本樣式,Inbox樣式將每行內容都當作獨立的單行文本去展示,文本內容最多可展示5行,每行內容展示不了時后邊自動添加“...”,

{
"validate_only": false,
"message": {
"android": {
"notification": {
"body": "Timely and accurate message pushing can ensure that your app's reach extends to its target users, resulting in more user engagement, and an enhanced user experience, while maximizing business value.",
"click_action": {
"type": 3
},
"inbox_content": ["1. Added the function of displaying notification messages on the UI.", "2. Added the automatic initialization capability.", "3. Added the function of sending messages to web apps.", "4. Expanded the application scope of the some functions."],
"style": 3,
"title": "Push Kit"
}
},
"token": ["xxx"]
}
}
總結:
| 樣式 |
| title取值欄位 | body取值欄位 |
| 0:默認樣式 | 展開前 | title | big_body –>body |
|
| 展開后 | big_title->title | big_body –>body |
| 1:大文本樣式 | 展開前 | title | big_body |
|
| 展開后 | big_title | big_body |
| 3:Inbox樣式 | 展開前 | title | body |
|
| 展開后 | big_title->title | inbox_content |
3)訊息本地化展示
通知訊息本地化又可以理解為國際化多語言展示,指的是通知訊息可以根據手機本地語言變化展示對應語言的標題和內容,從而覆寫本訊息中普通的標題和內容,


Push Kit提供了兩種訊息本地化展示的方法:
- 完全通過服務端提供的REST API介面實作;
{
"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"]
}
}
說明:
1、“title_loc_key”、“body_loc_key”欄位分別對應“multi_lang_key”中相關欄位的名稱;
2、“title_loc_args”、“body_loc_args”欄位的值都是一個可變的字串陣列,用于填充對應欄位的值中的占位符%s;
3、“multi_lang_key”欄位當前最多支持配置三種語言,
- 服務端REST API與應用本地的字串資源檔案“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"]
}
}
在Android資源檔案“/res/values/strings.xml”中定義字串資源,
支持占位符,%后面是占位符的位置,從1開始,$后面是填充資料的型別,
支持多語言,比如“/res/values-en/strings.xml”進行英語本地化適配,
<string name="title_key">New Friend Request</string>
<string name="body_key">My name is %1$s, I am from %2$s.</string>
在App中添加代碼,動態獲取可變字串,并對資源節點進行字串格式化,填充內容到占位符中,
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);
}
}
分析:對比2種方法,方法1使用靈活,不需要改造客戶端代碼,但是方法2可以同時支持更多的語言,適用于全球化程度更高的應用,
>>訪問華為開發者聯盟官網,了解更多相關內容
>>獲取開發指導檔案
>>華為移動服務開源倉庫地址:GitHub、Gitee
關注我們,第一時間了解華為移動服務最新技術資訊~
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/281298.html
標籤:其他
