package com.zhujizheng.IHome.util.alipush; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; import com.aliyuncs.push.model.v20160801.PushRequest; import com.aliyuncs.push.model.v20160801.PushResponse; import com.zhujizheng.IHome.everymomentmsg.vo.PYEveryMomentMsgVO; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; /** * Created with IntelliJ IDEA *

* Author: yons * Date: 2019/04/25 * Time: 01:10 * Description: * Copyright © 2019年 com.zhujizheng. All rights reserved. */ @Service @Transactional(rollbackFor = RuntimeException.class) public class AliPushServiceImpl implements AliPushService { private static final String accessKeyId = "LTAIPLS9lp16D10G"; private static final String accessKeySecret = "iL0XBB4V22PEBTZ1FQlrqnoG5xbGUH"; private static final long appKey = 26012507; @Override public void pushRemoteNotify(PYEveryMomentMsgVO systemMessageVO, int loverId, int badge) { this.pushWithAccount( String.valueOf(loverId), systemMessageVO.getTitle(), systemMessageVO.getContent(), badge, "", "remindBody", "{\"emId\":" + systemMessageVO.getEmId().toString() + "}"); } @Override public void pushRemoteNotify(String title, String content, String receiveId, String sendId, int badge) { this.pushWithAccount( receiveId, title, content, badge, "", "remindBody", "{\"sendId\":" + sendId + "}" ); } @Override public void pushRemoteNotify(String title, String content, String receiveId, int badge) { this.pushWithAccount( receiveId, title, content, badge, "", "remindBody", "" ); } public void pushWithAccount( String account, String title, String body, int badge, String subTitle, String remindBody, String iOSExtParams) { this.pushWithTarget( "ACCOUNT", account, title, body, badge, subTitle, remindBody, iOSExtParams); } public void pushWithDeviceId(String deviceId, String title, String body, int badge, String subTitle, String remindBody, String iOSExtParams) { this.pushWithTarget("DEVICE", deviceId, title, body, badge, subTitle, remindBody, iOSExtParams); } public void pushWithTarget( String target, String value, String title, String body, int badge, String subTitle, String remindBody, String iOSExtParams) { IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret); DefaultAcsClient client = new DefaultAcsClient(profile); PushRequest pushRequest = new PushRequest(); // 推送目标 pushRequest.setAppKey(appKey); //推送目标: DEVICE:按设备推送 ALIAS : 按别名推送 ACCOUNT:按帐号推送 TAG:按标签推送; ALL: 广播推送 pushRequest.setTarget(target); //根据Target来设定,如Target=DEVICE, 则对应的值为 设备id1,设备id2. 多个值使用逗号分隔.(帐号与设备有一次最多100个的限制) pushRequest.setTargetValue(value); //推送目标: DEVICE:推送给设备; ACCOUNT:推送给指定帐号,TAG:推送给自定义标签; ALL: 推送给全部 // pushRequest.setTarget("ALL"); //根据Target来设定,如Target=DEVICE, 则对应的值为 设备id1,设备id2. 多个值使用逗号分隔.(帐号与设备有一次最多100个的限制) // pushRequest.setTargetValue("ALL"); // 消息类型 MESSAGE NOTICE pushRequest.setPushType("NOTICE"); // 设备类型 ANDROID iOS ALL. pushRequest.setDeviceType("ALL"); // 消息的标题 pushRequest.setTitle(title); // 消息的内容 pushRequest.setBody(body); // iOS应用图标右上角角标 pushRequest.setIOSBadge(badge); // iOS通知声音 pushRequest.setIOSMusic("default"); // iOS10通知副标题的内容 pushRequest.setIOSSubtitle(subTitle); // 指定iOS10通知Category pushRequest.setIOSNotificationCategory("category"); // 是否允许扩展iOS通知内容 pushRequest.setIOSMutableContent(true); // iOS的通知是通过APNs中心来发送的,需要填写对应的环境信息。"DEV" : 表示开发环境 "PRODUCT" : 表示生产环境 pushRequest.setIOSApnsEnv("PRODUCT"); // 消息推送时设备不在线(既与移动推送的服务端的长连接通道不通),则这条推送会做为通知,通过苹果的APNs通道送达一次。注意:离线消息转通知仅适用于生产环境 pushRequest.setIOSRemind(true); // iOS消息转通知时使用的iOS通知内容,仅当iOSApnsEnv=PRODUCT && iOSRemind为true时有效 pushRequest.setIOSRemindBody(body); // 通知的扩展属性(注意 : 该参数要以json map的格式传入,否则会解析出错) // pushRequest.setIOSExtParameters("{\"_ENV_\":\"DEV\",\"k2\":\"v2\"}"); // 通知的扩展属性(注意 : 该参数要以json map的格式传入,否则会解析出错) pushRequest.setIOSExtParameters(iOSExtParams); // 推送控制 // Date pushDate = new Date(System.currentTimeMillis()) ; // 30秒之间的时间点, 也可以设置成你指定固定时间 // String pushTime = ParameterHelper.getISO8601Time(pushDate); // pushRequest.setPushTime(pushTime); // 延后推送。可选,如果不设置表示立即推送 // String expireTime = ParameterHelper.getISO8601Time(new Date(System.currentTimeMillis() + 12 * 3600 * 1000)); // 12小时后消息失效, 不会再发送 // pushRequest.setExpireTime(expireTime); // 离线消息是否保存,若保存, 在推送时候,用户即使不在线,下一次上线则会收到 pushRequest.setStoreOffline(true); try { PushResponse pushResponse = client.getAcsResponse(pushRequest); if (pushResponse != null) { System.out.printf("RequestId: %s, MessageID: %s\n", pushResponse.getRequestId(), pushResponse.getMessageId()); } } catch (ClientException e) { e.printStackTrace(); } } }