Token是推送令牌,每个设备上的每个应用的Token都是唯一存在的,客户端调用getToken方法向Push服务端请求应用的Token,您可以根据Push服务端返回的Token向应用推送消息。当getToken方法返回为空时,可通过onNewToken方法获取Token值。
建议将Push Token上报到您自己的应用服务器,并定时更新Token列表。您可以调用下行消息API,根据这些Token批量推送消息。
public class TokenAbilitySlice extends AbilitySlice {
private static final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, 0xD001234,
"TokenAbilitySlice");
private void getToken() {
// 创建新线程
new Thread("getToken") {
@Override
public void run() {
try {
// 从agconnect-services.json文件中读取“client_id”
String appId = "your client_id";
// 输入token标识"HCM"
String tokenScope = "HCM";
// 获取Push Token
String token = HmsInstanceId.getInstance(getAbility().getAbilityPackage(), TokenAbilitySlice.this).getToken(appId, tokenScope);
} catch (ApiException e) {
// 获取Push Token失败时,打印错误码
HiLog.error(LABEL_LOG, "get token failed, the error code is %{public}d", e.getStatusCode());
}
}
}.start();
}
} public class DemoHmsMessageServiceAbility extends HmsMessageService {
private static final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, 0xD001234, "DemoHmsMessageServiceAbility");
@Override
// 获取Token
public void onNewToken(String token) {
HiLog.info(LABEL_LOG, "onNewToken called, get token");
}
@Override
// 获取Token失败,打印错误码
public void onTokenError(Exception exception) {
HiLog.error(LABEL_LOG, "get onNewToken error, error code is %{public}d", ((ZBaseException)exception).getErrorCode());
}
} 在您的service(已继承HmsMessageService)中,覆写onMessageReceived方法,只要您发送透传消息至终端设备,都会获得透传消息的内容。
public class DemoHmsMessageServiceAbility extends HmsMessageService {
private static final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, 0xD001234,
"DemoHmsMessageServiceAbility");
@Override
public void onMessageReceived(ZRemoteMessage message) {
// 打印消息的内容字段
HiLog.info(LABEL_LOG, "get data, %{public}s", message.getData());
ZRemoteMessage.Notification notification = message.getNotification();
if (notification != null) {
HiLog.info(LABEL_LOG, "get title, %{public}s", notification.getTitle());
HiLog.info(LABEL_LOG, "get body, %{public}s", notification.getBody());
}
}
}
HTTPS POST URL:
POST https://push-api.cloud.huawei.com/v1/[clientid]/messages:send
调用API推送消息时,接口URL版本为V1(https://push-api.cloud.huawei.com/v1/[clientid]/messages:send)和V2(https://push-api.cloud.huawei.com/v2/[projectid]/messages:send)时,仅支持给HarmonyOS 3.x/4.x的系统版本推送通知。如果您想给HarmonyOS Next/5.x及之后的系统版本推送通知,请使用V3 版本URL(https://push-api.cloud.huawei.com/v3/[projectId]/messages:send)。
请求消息头示例:
Content-Type: application/json; charset=UTF-8 Authorization: Bearer CF3Xl2XV6jMK************************DgAPuzvNm3WccUIaDg==
{
"validate_only": false,
"message": {
"android": {
"notification": {
"title": "test title",
"body": "test body",
"click_action": {
"type": 3
}
}
},
"token": ["pushtoken1"]
}
} {
"validate_only": false,
"message": {
"android": {
"notification": {
"title": "test title",
"body": "test body",
"click_action": {
"type": 3
}
}
},
"token": ["pushtoken1"]
}
} {
"validate_only": false,
"message": {
"android": {
"notification": {
"title": "test title",
"body": "test body",
"click_action": {
"type": 2,
"url":"https://www.huawei.com"
}
}
},
"token": ["pushtoken1"]
}
} {
"orientation": "unspecified",
"name": "com.test.java.MyActionAbility",
"icon": "$media:icon",
"description": "$string:myactionability_description",
"label": "$string:entry_MyActionAbility",
"type": "page",
"launchType": "standard",
"skills": [
{
"entities": ["entity.system.default"],
"actions": ["com.test.myaction"]
}
]
} 您也可以通过服务端API推送消息。消息体中携带click_action和action字段,type取值为1表示点击消息后打开应用自定义页面,action取值为上一步中定义的actions的值。
{
"validate_only": false,
"message": {
"android": {
"notification": {
"title": "test title",
"body": "test body",
"click_action": {
"type": 1,
"action":"com.test.myaction"
}
}
},
"token": ["pushtoken1"]
}
} {
"orientation": "unspecified",
"name": "com.test.java.MyActionAbility",
"icon": "$media:icon",
"description": "$string:myactionability_description",
"label": "$string:entry_MyActionAbility",
"type": "page",
"launchType": "standard",
"skills": [
{
"uris": [
{
"scheme": "pushscheme",
"host": "pushscheme.host",
"path": "/deeplink"
}
]
}
]
} 您也可以通过服务端API推送消息。消息体中携带click_action和intent字段,type取值为1表示点击消息后打开应用自定义页面,intent取值为上一步中定义的uris的值拼接而成。
{
"validate_only": false,
"message": {
"android": {
"notification": {
"title": "test title",
"body": "test body",
"click_action": {
"type": 1,
"intent":"intent://pushscheme.host/deeplink?#Intent;scheme=pushscheme;end"
}
}
},
"token": ["pushtoken1"]
}
} 您推送消息时可携带data字段,当用户点击消息时通过下述方法将data中的数据传递至客户端应用。
{
"validate_only": false,
"message": {
"android": {
"notification": {
"title": "test title",
"body": "test body",
"click_action": {
"type": 1,
"action":"com.test.myaction"
}
},
"data": "{'key_data':'value_data'}"
},
"token": ["pushtoken1"]
}
}public class MainAbilitySlice extends AbilitySlice { private static final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, 0xD001234, "myDemo"); @Override public void onStart(Intent intent) { HiLog.info(LABEL_LOG, "MainAbilitySlice get started..."); super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); // 调用解析方法 parseIntent(intent); } private void parseIntent(Intent intent){ if (intent == null){return;} IntentParams intentParams = intent.getParams(); if (intentParams == null) {return;} // 获取data字段中的键值对 String key = "key_data"; Object obj = intentParams.getParam(key); try{ // 打印data字段中的键值对 HiLog.info(LABEL_LOG, "my key: %{public}s, my value: %{public}s", key, obj); }catch (Exception e){ HiLog.info(LABEL_LOG, "catch exception : " + e.getMessage()); } } }