java批量发送短信_JAVA实现多线程处理批量发送短信、APP推送

packagecom.bankhui.center.jpush;importjava.util.HashMap;importjava.util.Map;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;/*** The entrance of JPush API library.

**/

public class JPushClient extendsBaseClient {private static Logger logger = LoggerFactory.getLogger(JPushClient.class);//在极光注册上传应用的 appKey 和 masterSecret

private static final String appKeyStr ="******************";必填,

private static final String masterSecretStr = "******************";//必填,每个应用都对应一个masterSecret

private static JPushClient jpush = null;/** 保存离线的时长。秒为单位。最多支持10天(864000秒)。

* 0 表示该消息不保存离线。即:用户在线马上发出,当前不在线用户将不会收到此消息。

* 此参数不设置则表示默认,默认为保存1天的离线消息(86400秒*/

private static long timeToLive = 60 * 60 * 24;protected static HttpPostClient httpClient = newHttpPostClient();/*** 给指定用户推送消息

*@parammsgTitle 标题

*@parammsgContent 内容

*@paramjgAlias 极光通讯id

*@paramsysMsgId 系统保存的消息id

*@paramtype 跳转类型0不带链接跳转,1带链接跳转 2 站内信

*@paramurl 跳转url

*@authorwxz

* @date 2017年2月27日*/

public static voidappSend(String msgTitle,String msgContent,String jgAlias,String sysMsgId,String type,String url) {try{

Map extra1 =new HashMap();

extra1.put("sysMsgId", sysMsgId);

extra1.put("type", type);//0不带链接跳转,1带链接跳转

extra1.put("url", url);if(null ==jpush){

jpush= newJPushClient(masterSecretStr, appKeyStr, timeToLive);

}

MessageResult msgResult= jpush.sendNotificationWithAlias(getRandomSendNo(), jgAlias, msgTitle, msgContent, 0, extra1);if (null !=msgResult) {

logger.info("服务器返回数据: " +msgResult.toString());if (msgResult.getErrcode() ==ErrorCodeEnum.NOERROR.value()) {

logger.info("发送成功, sendNo=" +msgResult.getSendno());

}else{

logger.error("发送失败, 错误代码=" + msgResult.getErrcode() + ", 错误消息=" +msgResult.getErrmsg());

}

}else{

logger.error("无法获取数据");

}

}catch(Exception e) {

logger.error("发送失败,error msg is :"+e);

}

}/*** 给所有用户推送消息

*@parammsgTitle 标题

*@parammsgContent 内容

*@paramsysMsgId 消息id

*@paramtype 跳转类型0不带链接跳转,1带链接跳转

*@paramurl 跳转url

*@authorwxz

* @date 2017年2月27日*/

public static voidappSendAll(String msgTitle,String msgContent,String sysMsgId,String type,String url) {/** IOS设备扩展参数,

* 设置badge,设置声音*/Map extra1 =new HashMap();

extra1.put("sysMsgId", sysMsgId);

extra1.put("type", type);//0不带链接跳转,1带链接跳转

extra1.put("url", url);if(null ==jpush){

jpush= newJPushClient(masterSecretStr, appKeyStr, timeToLive);

}

MessageResult msgResult= jpush.sendNotificationWithAppKey(getRandomSendNo(), msgTitle, msgContent, 0, extra1);if (null !=msgResult) {

logger.info("服务器返回数据: " +msgResult.toString());if (msgResult.getErrcode() ==ErrorCodeEnum.NOERROR.value()) {

logger.info("发送成功, sendNo=" +msgResult.getSendno());

}else{

logger.error("发送失败, 错误代码=" + msgResult.getErrcode() + ", 错误消息=" +msgResult.getErrmsg());

}

}else{

logger.error("无法获取数据");

}

}publicJPushClient(String masterSecret, String appKey) {this.masterSecret =masterSecret;this.appKey =appKey;

}public JPushClient(String masterSecret, String appKey, longtimeToLive) {this.masterSecret =masterSecret;this.appKey =appKey;this.timeToLive =timeToLive;

}publicJPushClient(String masterSecret, String appKey, DeviceEnum device) {this.masterSecret =masterSecret;this.appKey =appKey;

devices.add(device);

}public JPushClient(String masterSecret, String appKey, longtimeToLive, DeviceEnum device) {this.masterSecret =masterSecret;this.appKey =appKey;this.timeToLive =timeToLive;

devices.add(device);

}/** @description 发送带IMEI的通知

* @return MessageResult*/

publicMessageResult sendNotificationWithImei(String sendNo, String imei, String msgTitle, String msgContent) {

NotifyMessageParams p= newNotifyMessageParams();

p.setReceiverType(ReceiverTypeEnum.IMEI);

p.setReceiverValue(imei);return sendNotification(p, sendNo, msgTitle, msgContent, 0, null);

}/** @params builderId通知栏样式

* @description 发送带IMEI的通知

* @return MessageResult*/

public MessageResult sendNotificationWithImei(String sendNo, String imei, String msgTitle, String msgContent, int builderId, Mapextra) {

NotifyMessageParams p= newNotifyMessageParams();

p.setReceiverType(ReceiverTypeEnum.IMEI);

p.setReceiverValue(imei);returnsendNotification(p, sendNo, msgTitle, msgContent, builderId, extra);

}/** @description 发送带IMEI的自定义消息

* @return MessageResult*/

publicMessageResult sendCustomMessageWithImei(String sendNo, String imei, String msgTitle, String msgContent) {

CustomMessageParams p= newCustomMessageParams();

p.setReceiverType(ReceiverTypeEnum.IMEI);

p.setReceiverValue(imei);return sendCustomMessage(p, sendNo, msgTitle, msgContent, null, null);

}/** @params msgContentType消息的类型,extra附属JSON信息

* @description 发送带IMEI的自定义消息

* @return MessageResult*/

public MessageResult sendCustomMessageWithImei(String sendNo, String imei, String msgTitle, String msgContent, String msgContentType, Mapextra) {

CustomMessageParams p= newCustomMessageParams();

p.setReceiverType(ReceiverTypeEnum.IMEI);

p.setReceiverValue(imei);returnsendCustomMessage(p, sendNo, msgTitle, msgContent, msgContentType, extra);

}/** @description 发送带TAG的通知

* @return MessageResult*/

publicMessageResult sendNotificationWithTag(String sendNo, String tag, String msgTitle, String msgContent) {

NotifyMessageParams p= newNotifyMessageParams();

p.setReceiverType(ReceiverTypeEnum.TAG);

p.setReceiverValue(tag);return sendNotification(p, sendNo, msgTitle, msgContent, 0, null);

}/** @params builderId通知栏样式

* @description 发送带TAG的通知

* @return MessageResult*/

public MessageResult sendNotificationWithTag(String sendNo, String tag, String msgTitle, String msgContent, int builderId, Mapextra) {

NotifyMessageParams p= newNotifyMessageParams();

p.setReceiverType(ReceiverTypeEnum.TAG);

p.setReceiverValue(tag);returnsendNotification(p, sendNo, msgTitle, msgContent, builderId, extra);

}/** @description 发送带TAG的自定义消息

* @return MessageResult*/

publicMessageResult sendCustomMessageWithTag(String sendNo, String tag, String msgTitle, String msgContent) {

CustomMessageParams p= newCustomMessageParams();

p.setReceiverType(ReceiverTypeEnum.TAG);

p.setReceiverValue(tag);return sendCustomMessage(p, sendNo, msgTitle, msgContent, null, null);

}/** @params msgContentType消息的类型,extra附属JSON信息

* @description 发送带TAG的自定义消息

* @return MessageResult*/

public MessageResult sendCustomMessageWithTag(String sendNo, String tag, String msgTitle, String msgContent, String msgContentType, Mapextra) {

CustomMessageParams p= newCustomMessageParams();

p.setReceiverType(ReceiverTypeEnum.TAG);

p.setReceiverValue(tag);returnsendCustomMessage(p, sendNo, msgTitle, msgContent, msgContentType, extra);

}/** @description 发送带ALIAS的通知

* @return MessageResult*/

publicMessageResult sendNotificationWithAlias(String sendNo, String alias, String msgTitle, String msgContent) {

NotifyMessageParams p= newNotifyMessageParams();

p.setReceiverType(ReceiverTypeEnum.ALIAS);

p.setReceiverValue(alias);return sendNotification(p, sendNo, msgTitle, msgContent, 0, null);

}/** @params builderId通知栏样式

* @description 发送带ALIAS的通知

* @return MessageResult*/

public MessageResult sendNotificationWithAlias(String sendNo, String alias, String msgTitle, String msgContent, int builderId, Mapextra) {

NotifyMessageParams p= newNotifyMessageParams();

p.setReceiverType(ReceiverTypeEnum.ALIAS);

p.setReceiverValue(alias);returnsendNotification(p, sendNo, msgTitle, msgContent, builderId, extra);

}/** @description 发送带ALIAS的自定义消息

* @return MessageResult*/

publicMessageResult sendCustomMessageWithAlias(String sendNo, String alias, String msgTitle, String msgContent) {

CustomMessageParams p= newCustomMessageParams();

p.setReceiverType(ReceiverTypeEnum.ALIAS);

p.setReceiverValue(alias);return sendCustomMessage(p, sendNo, msgTitle, msgContent, null, null);

}/** @params msgContentType消息的类型,extra附属JSON信息

* @description 发送带ALIAS的自定义消息

* @return MessageResult*/

public MessageResult sendCustomMessageWithAlias(String sendNo, String alias, String msgTitle, String msgContent, String msgContentType, Mapextra) {

CustomMessageParams p= newCustomMessageParams();

p.setReceiverType(ReceiverTypeEnum.ALIAS);

p.setReceiverValue(alias);returnsendCustomMessage(p, sendNo, msgTitle, msgContent, msgContentType, extra);

}/** @description 发送带AppKey的通知

* @return MessageResult*/

publicMessageResult sendNotificationWithAppKey(String sendNo, String msgTitle, String msgContent) {

NotifyMessageParams p= newNotifyMessageParams();

p.setReceiverType(ReceiverTypeEnum.APPKEYS);return sendNotification(p, sendNo, msgTitle, msgContent, 0, null);

}/** @params builderId通知栏样式

* @description 发送带AppKey的通知

* @return MessageResult*/

public MessageResult sendNotificationWithAppKey(String sendNo, String msgTitle, String msgContent, int builderId, Mapextra) {

NotifyMessageParams p= newNotifyMessageParams();

p.setReceiverType(ReceiverTypeEnum.APPKEYS);returnsendNotification(p, sendNo, msgTitle, msgContent, builderId, extra);

}/** @description 发送带AppKey的自定义消息

* @return MessageResult*/

publicMessageResult sendCustomMessageWithAppKey(String sendNo, String msgTitle, String msgContent) {

CustomMessageParams p= newCustomMessageParams();

p.setReceiverType(ReceiverTypeEnum.APPKEYS);return sendCustomMessage(p, sendNo, msgTitle, msgContent, null, null);

}/** @params msgContentType消息的类型,extra附属JSON信息

* @description 发送带AppKey的自定义消息

* @return MessageResult*/

public MessageResult sendCustomMessageWithAppKey(String sendNo, String msgTitle, String msgContent, String msgContentType, Mapextra) {

CustomMessageParams p= newCustomMessageParams();

p.setReceiverType(ReceiverTypeEnum.APPKEYS);returnsendCustomMessage(p, sendNo, msgTitle, msgContent, msgContentType, extra);

}protected MessageResult sendCustomMessage(CustomMessageParams p, String sendNo, String msgTitle, String msgContent, String msgContentType, Mapextra) {if (null !=msgContentType) {

p.getMsgContent().setContentType(msgContentType);

}if (null !=extra) {

p.getMsgContent().setExtra(extra);

}returnsendMessage(p, sendNo, msgTitle, msgContent);

}protected MessageResult sendNotification(NotifyMessageParams p, String sendNo, String msgTitle, String msgContent, int builderId, Mapextra) {

p.getMsgContent().setBuilderId(builderId);if (null !=extra) {

p.getMsgContent().setExtra(extra);

}returnsendMessage(p, sendNo, msgTitle, msgContent);

}protectedMessageResult sendMessage(MessageParams p,String sendNo, String msgTitle, String msgContent) {

p.setSendNo(sendNo);

p.setAppKey(this.getAppKey());

p.setMasterSecret(this.masterSecret);

p.setTimeToLive(this.timeToLive);

p.setSendDescription(this.getSendDescription());for (DeviceEnum device : this.getDevices()) {

p.addPlatform(device);

}if (null !=msgTitle) {

p.getMsgContent().setTitle(msgTitle);

}

p.getMsgContent().setMessage(msgContent);returnsendMessage(p);

}protectedMessageResult sendMessage(MessageParams params) {return httpClient.post(BaseURL.ALL_PATH, this.enableSSL, params);

}public static final int MAX =Integer.MAX_VALUE;public static final int MIN = (int) MAX/2;/*** 保持 sendNo 的唯一性是有必要的

* It is very important to keep sendNo unique.

*@returnsendNo*/

public staticString getRandomSendNo() {return String.valueOf((int) (MIN + Math.random() * (MAX -MIN)));

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值