puzhibing
2024-01-30 80b3ea5587ff7ec20541d9ca7c6c28739e4d615b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.dsh.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("&notification=").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;
    }
 
 
 
}