package com.stylefeng.guns.modular.system.util;
|
|
import io.rong.RongCloud;
|
import io.rong.methods.push.Push;
|
import io.rong.methods.user.User;
|
import io.rong.models.Result;
|
import io.rong.models.push.Audience;
|
import io.rong.models.push.Notification;
|
import io.rong.models.push.PlatformNotification;
|
import io.rong.models.push.PushModel;
|
import io.rong.models.response.PushResult;
|
import io.rong.models.response.TokenResult;
|
import io.rong.models.user.UserModel;
|
import io.rong.util.HostType;
|
import io.rong.util.HttpUtil;
|
|
import java.net.HttpURLConnection;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
/**
|
* 融云
|
*/
|
public class RongCloudUtil {
|
private static String appKey = "25wehl3u20ddw";
|
private static String appSecret = "gG3IjHADkAK";
|
|
private static RongCloud rongCloud = RongCloud.getInstance(appKey, appSecret);
|
/**
|
* 融云注册
|
* @param id
|
* @param name
|
* @param portrait
|
* @return
|
* @throws Exception
|
*/
|
public static TokenResult getToken(String id,String name,String portrait) throws Exception{
|
User user = rongCloud.user;
|
UserModel userModel = new UserModel()
|
.setId(id)
|
.setName(name)
|
.setPortrait(portrait);
|
TokenResult result = user.register(userModel);
|
return result;
|
}
|
/**
|
* 修改
|
* @param id
|
* @param name
|
* @param portrait
|
* @return
|
* @throws Exception
|
*/
|
public static Result update(String id,String name,String portrait) throws Exception{
|
User user = rongCloud.user;
|
UserModel userModel = new UserModel()
|
.setId(id)
|
.setName(name)
|
.setPortrait(portrait);
|
Result result = user.update(userModel);
|
return result;
|
}
|
|
/**
|
* 推送
|
* @param userId
|
* @param content
|
* @param extras
|
* @return
|
* @throws Exception
|
*/
|
public static String push( String userId, String content,String extras) throws Exception {
|
String platform="[\"ios\",\"android\"]";
|
String audience="{\"userid\":"+"[\""+userId+"\"]"+",\"is_to_all\":false}";
|
String notification="{\"alert\":\""+content+"\",\"ios\":"+extras+",\"android\":"+extras+"}";
|
String[] tag = {"10"};
|
StringBuilder sb = new StringBuilder();
|
sb.append("platform=").append(platform);
|
sb.append("&tag=").append(tag.toString());
|
sb.append("&audience=").append(audience);
|
sb.append("¬ification=").append(notification);
|
/*sb.append(",");
|
sb.append("\"extras\":").append(extras);*/
|
HostType apiHostType = new HostType("https://api-cn.ronghub.com/message/system");
|
HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(apiHostType, appKey, appSecret, "/publish.json", "application/json");
|
HttpUtil.setBodyParameter(sb, conn);
|
return HttpUtil.returnResult(conn);
|
}
|
|
/**
|
* 发送推送信息
|
*
|
* @param alert 消息内容
|
* @param fromUserId 1
|
* @param toUserId userId
|
// * @param objectName RC:TxtMsg
|
// * @param pushContent 消息标题
|
// * @param pushData 空-安卓 非空:苹果
|
* @throws Exception
|
*/
|
public static PushResult pushSystemMessage(String alert, String fromUserId, String[] toUserId,Map<String, String> extras) throws Exception {
|
|
//请求路径
|
Push push = rongCloud.push;
|
//推送条件
|
Audience audience = new Audience();
|
audience.setIs_to_all(false);
|
audience.setUserid(toUserId);
|
|
////按操作系统类型推送消息内容
|
Notification notification = new Notification();
|
notification.setAlert(alert);
|
|
//IOS内容
|
PlatformNotification ios = new PlatformNotification();
|
ios.setAlert(alert);
|
ios.setExtras(extras);
|
notification.setIos(ios);
|
|
//Android内容
|
PlatformNotification android = new PlatformNotification();
|
android.setAlert(alert);
|
android.setExtras(extras);
|
notification.setAndroid(android);
|
|
PushModel pushModel = new PushModel();
|
//目标操作系统
|
String[] platform="ios,android".split(",");
|
pushModel.setPlatform(platform);
|
pushModel.setAudience(audience);
|
pushModel.setNotification(notification);
|
//推送消息
|
PushResult result = push.push(pushModel);
|
return result;
|
}
|
|
|
public static void main(String[] args) {
|
try {
|
Map<String,String> extras = new HashMap<>();
|
extras.put("state","1");
|
PushResult pushResult = pushSystemMessage("您的账号已重置密码为123456,请修改密码后重新登录,如有问题,请联系平台客服。", "1", new String[]{"122"}, extras);
|
System.out.println(pushResult);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
}
|