uniPush.sendMessage 发送华为离线 参数配置
时间: 2024-01-09 19:05:48 浏览: 157
SendMessage参数
好的,以下是华为离线推送参数配置示例:
```java
String appId = "yourAppId"; // 应用ID
String appSecret = "yourAppSecret"; // 应用秘钥
String tokenUrl = "https://login.cloud.huawei.com/oauth2/v2/token"; // 获取认证Token的URL
String apiUrl = "https://api.push.hicloud.com/pushsend.do"; // Push消息下发API
String accessToken = null; // 认证Token
// 获取认证Token
try {
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(tokenUrl);
post.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("grant_type", "client_credentials"));
params.add(new BasicNameValuePair("client_id", appId));
params.add(new BasicNameValuePair("client_secret", appSecret));
post.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = client.execute(post);
String result = EntityUtils.toString(response.getEntity(), "utf-8");
JSONObject jsonObject = new JSONObject(result);
accessToken = jsonObject.getString("access_token");
} catch (Exception e) {
e.printStackTrace();
}
// 发送Push消息
try {
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(apiUrl);
post.setHeader("Content-Type", "application/json;charset=utf-8");
post.setHeader("Authorization", "Bearer " + accessToken);
JSONObject json = new JSONObject();
json.put("validate_only", false); // 是否只验证消息
json.put("message_type", "notify"); // 消息类型:notify-通知,message-透传消息
json.put("android", new JSONObject().put("hps", new JSONObject().put("msg", new JSONObject()
.put("type", 3) // 消息类型:1-透传异步消息,3-通知栏消息
.put("body", new JSONObject()
.put("title", "Push Title") // 通知栏消息标题
.put("content", "Push Content") // 通知栏消息正文
.put("badge", 1) // 角标
.put("sound", "default") // 声音
.put("click_action", new JSONObject().put("type", 1) // 点击通知后的行为:1-打开APP首页,2-打开自定义页面,3-打开URL
.put("intent", "#Intent;compo=com.rvr/.Activity;S.W=U;end"))
.put("extras", new JSONObject()
.put("key1", "value1") // 自定义参数
.put("key2", "value2")
.put("key3", "value3")))))));
json.put("token_list", new JSONArray().put("yourDeviceToken"));
post.setEntity(new StringEntity(json.toString(), "utf-8"));
HttpResponse response = client.execute(post);
String result = EntityUtils.toString(response.getEntity(), "utf-8");
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
```
其中,需要替换的参数有:
- `yourAppId`:应用ID
- `yourAppSecret`:应用秘钥
- `yourDeviceToken`:设备Token,可通过华为推送服务的SDK获取
另外,需要注意的是,华为推送服务的API与参数可能会有变更,以上代码仅供参考。
阅读全文