| | |
| | | package com.xiaopeng.pushMessage.utils; |
| | | |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.aliyun.auth.credentials.Credential; |
| | | import com.aliyun.auth.credentials.provider.StaticCredentialProvider; |
| | | import com.aliyun.sdk.service.push20160801.models.*; |
| | | import com.aliyun.sdk.service.push20160801.*; |
| | | import com.google.gson.Gson; |
| | | import darabonba.core.client.ClientOverrideConfiguration; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | |
| | | //import javax.net.ssl.KeyManager; |
| | | //import javax.net.ssl.X509TrustManager; |
| | | import java.util.concurrent.CompletableFuture; |
| | | |
| | | @Slf4j |
| | | public class AliPushUtil { |
| | | |
| | | @Value("${ali.push.accessKeyId}") |
| | | private String accessKeyId; |
| | | @Value("${ali.push.accessKeySecret}") |
| | | private String accessKeySecret; |
| | | @Value("${ali.push.appKey}") |
| | | private Long appKey; |
| | | @Value("${ali.push.regionId}") |
| | | private String regionId; |
| | | @Value("${ali.push.appKey}") |
| | | private Long appKey; |
| | | @Value("${ali.push.iosAppKey}") |
| | | private Long iosAppKey; |
| | | /** |
| | | * 消息类型 MESSAGE 消息 NOTICE 通知 |
| | | */ |
| | |
| | | * @param body 消息内容 |
| | | * @throws Exception |
| | | */ |
| | | public void push(String account, String title, String body) throws Exception { |
| | | public String pushMessage(Integer deviceType,String account, String title, String body) throws Exception { |
| | | |
| | | StaticCredentialProvider credentialProvider = StaticCredentialProvider.create( |
| | | Credential.builder() |
| | |
| | | ) |
| | | .build()) { |
| | | |
| | | // Parameter settings for API request |
| | | // 判断账号类型 |
| | | Long appKeys = appKey; |
| | | if(deviceType == 2){ |
| | | appKeys = iosAppKey; |
| | | } |
| | | |
| | | PushRequest pushRequest = PushRequest.builder() |
| | | .appKey(appKey) |
| | | .appKey(appKeys) |
| | | .pushType(PUSH_TYPE) |
| | | .deviceType(DEVICE_TYPE_ALL) |
| | | .target(TARGET) |
| | |
| | | .build(); |
| | | CompletableFuture<PushResponse> response = client.push(pushRequest); |
| | | PushResponse resp = response.get(); |
| | | System.out.println(new Gson().toJson(resp)); |
| | | String json = new Gson().toJson(resp); |
| | | log.info("运维APP-消息推送:{}",json); |
| | | |
| | | JSONObject jsonObject = JSONObject.parseObject(json); |
| | | |
| | | Integer statusCode = jsonObject.getInteger("statusCode"); |
| | | if(statusCode == 200){ |
| | | log.info("运维APP-消息推送成功"); |
| | | JSONObject resultBody = jsonObject.getJSONObject("body"); |
| | | return resultBody.getString("messageId"); |
| | | }else{ |
| | | log.info("运维APP-消息推送失败"); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | public static void main(String[] args) throws Exception { |