集成云信推送
This commit is contained in:
parent
345586e911
commit
55c3718489
|
|
@ -1,5 +1,6 @@
|
|||
package com.ifish7.mq.push;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.aliyuncs.DefaultAcsClient;
|
||||
import com.aliyuncs.exceptions.ClientException;
|
||||
import com.aliyuncs.http.FormatType;
|
||||
|
|
@ -13,10 +14,19 @@ import com.ifish7.mq.business.user.entity.TblPushList;
|
|||
import com.ifish7.mq.business.user.service.ITblAliyunDeviceInfoService;
|
||||
import com.ifish7.mq.business.user.service.ITblPushListService;
|
||||
import lombok.extern.log4j.Log4j;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
|
||||
import java.util.List;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author: yan.y
|
||||
|
|
@ -106,13 +116,115 @@ public class AliyunPushApi {
|
|||
try {
|
||||
PushResponse response = client.getAcsResponse(pushRequest);
|
||||
pushList.setReportId(Integer.parseInt(response.getMessageId()));
|
||||
//未读
|
||||
pushList.setIsRead(1);
|
||||
pushList.setDeviceId((pushList.getDeviceId() == null || pushList.getDeviceId() == 0)? null : pushList.getDeviceId());
|
||||
} catch (ClientException e) {
|
||||
log.error(e.getMessage(),e);
|
||||
}
|
||||
});
|
||||
//未读
|
||||
pushList.setIsRead(1);
|
||||
pushListService.save(pushList);
|
||||
|
||||
sendMsg(String.valueOf(pushList.getUserId()),pushList.getPushContext());
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送普通消息
|
||||
* @param userId
|
||||
* @param msg
|
||||
* @return
|
||||
*/
|
||||
public JSONObject sendMsg(String userId, String msg){
|
||||
String appSecret = "e62f6c247b46";
|
||||
String appKey = "87b0e3315dfc2df08060bcb54246da68";
|
||||
String from = "ifish";
|
||||
try {
|
||||
HttpClient httpClient = new DefaultHttpClient();
|
||||
String url = "https://api.netease.im/nimserver/msg/sendMsg.action";
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
String nonce = getCharAndNumr(10);
|
||||
String curTime = String.valueOf((new Date()).getTime() / 1000L);
|
||||
//计算CheckSum
|
||||
String checkSum = getCheckSum(appSecret, nonce ,curTime);
|
||||
//设置请求的header
|
||||
httpPost.addHeader("AppKey", appKey);
|
||||
httpPost.addHeader("Nonce", nonce);
|
||||
httpPost.addHeader("CurTime", curTime);
|
||||
httpPost.addHeader("CheckSum", checkSum);
|
||||
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
|
||||
//设置请求的参数
|
||||
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
||||
nvps.add(new BasicNameValuePair("from", from));
|
||||
nvps.add(new BasicNameValuePair("to", userId));
|
||||
nvps.add(new BasicNameValuePair("ope", "0"));
|
||||
nvps.add(new BasicNameValuePair("type", "0"));
|
||||
nvps.add(new BasicNameValuePair("body", "{\"msg\":\""+msg+"\"}"));
|
||||
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
|
||||
//执行请求
|
||||
HttpResponse response = httpClient.execute(httpPost);
|
||||
//执行结果
|
||||
String responseStr = EntityUtils.toString(response.getEntity(), "utf-8");
|
||||
JSONObject json = JSONObject.parseObject(responseStr);
|
||||
log.info("neteasePush resp : " + json);
|
||||
return json;
|
||||
} catch (Exception e) {
|
||||
log.error("refreshToken error message:{}",e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机生成字母与数字组合
|
||||
* @param length
|
||||
* @return
|
||||
*/
|
||||
private String getCharAndNumr(int length) {
|
||||
String val = "";
|
||||
Random random = new Random();
|
||||
for (int i = 0; i < length; i++) {
|
||||
// 输出字母还是数字
|
||||
String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
|
||||
// 字符串
|
||||
if ("char".equalsIgnoreCase(charOrNum)) {
|
||||
// 取得大写字母还是小写字母
|
||||
int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
|
||||
val += (char) (choice + random.nextInt(26));
|
||||
// 数字
|
||||
} else if ("num".equalsIgnoreCase(charOrNum)) {
|
||||
val += String.valueOf(random.nextInt(10));
|
||||
}
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
// 计算并获取CheckSum
|
||||
private String getCheckSum(String appSecret, String nonce, String curTime) {
|
||||
return encode("sha1", appSecret + nonce + curTime);
|
||||
}
|
||||
|
||||
private String encode(String algorithm, String value) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
MessageDigest messageDigest
|
||||
= MessageDigest.getInstance(algorithm);
|
||||
messageDigest.update(value.getBytes());
|
||||
return getFormattedText(messageDigest.digest());
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private String getFormattedText(byte[] bytes) {
|
||||
int len = bytes.length;
|
||||
StringBuilder buf = new StringBuilder(len * 2);
|
||||
for (int j = 0; j < len; j++) {
|
||||
buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
|
||||
buf.append(HEX_DIGITS[bytes[j] & 0x0f]);
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package com.ifish7.mq.push;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.aliyuncs.DefaultAcsClient;
|
||||
import com.aliyuncs.exceptions.ClientException;
|
||||
import com.aliyuncs.http.FormatType;
|
||||
|
|
@ -76,4 +77,11 @@ public class AliyunPushApiTest {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSendMsg() {
|
||||
AliyunPushApi pushApi = new AliyunPushApi("","","");
|
||||
JSONObject jsonObject = pushApi.sendMsg("20731", "网易云信推送测试!");
|
||||
System.out.println(jsonObject.toJSONString());
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue