package com.ifish7.mq.push; import com.alibaba.fastjson.JSONObject; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.http.FormatType; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.push.model.v20160801.PushRequest; import com.aliyuncs.push.model.v20160801.PushResponse; import com.ifish7.mq.business.user.entity.TblPushList; import com.ifish7.mq.business.user.service.ITblPushListService; import lombok.extern.log4j.Log4j; import org.springframework.beans.factory.annotation.Autowired; import java.time.LocalDateTime; /** * @author: yan.y * @Description: * @Date: Created in 21:42 2019/4/10 */ @Log4j public class AliyunPushApi { private DefaultAcsClient client; public AliyunPushApi(String regionId,String accessKeyId,String accessKeySecret){ client = new DefaultAcsClient(DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret)); } //推送target类型 private static final String TARGET_TYPE = "DEVICE"; //推送类型 private static final String PUSH_TYPE = "MESSAGE"; @Autowired private PushRequest pushRequest; @Autowired private ITblPushListService pushListService; public void advancedPushAndSavePushList(JSONObject json){ //推送目标: //DEVICE:根据设备推送 //ACCOUNT:根据账号推送 //ALIAS:根据别名推送 //TAG:根据标签推送 //ALL:推送给全部设备 pushRequest.setTarget(TARGET_TYPE); pushRequest.setAcceptFormat(FormatType.JSON); //根据Target来设定,多个值使用逗号分隔 //此处应该查询出用户所对应的deviceId ★★★★★ pushRequest.setTargetValue(json.getString("deviceId")); //推送类型 pushRequest.setPushType(PUSH_TYPE); //推送设备类型 pushRequest.setDeviceType(json.getString("phoneType")); // 消息的标题 pushRequest.setTitle(json.getString("title")); // 消息的内容 pushRequest.setBody(json.getString("body")); // 离线消息是否保存,若保存, 在推送时候,用户即使不在线,下一次上线则会收到 pushRequest.setStoreOffline(true); try { PushResponse response = client.getAcsResponse(pushRequest); TblPushList pushList = new TblPushList(); pushList.setUserId(json.getInteger("userId")); pushList.setDeviceId(json.getInteger("deviceId")); pushList.setShowName(json.getString("showName")); pushList.setPhoneType(json.getString("phoneType")); pushList.setPushType(json.getString("pushType")); pushList.setPushTitle(json.getString("title")); pushList.setPushContext(json.getString("body")); pushList.setJpushStatus(response.getMessageId()); pushList.setNeteaseStatus("1"); pushList.setCreateTime(LocalDateTime.now()); pushList.setNumber1(json.getInteger("number1")); pushList.setNumber2(json.getInteger("number2")); pushList.setNumber3(json.getInteger("number3")); pushList.setNumber4(json.getInteger("number4")); pushList.setNumber5(json.getInteger("number5")); pushListService.save(pushList); } catch (ClientException e) { log.error(e.getMessage(),e); } } }