-
Notifications
You must be signed in to change notification settings - Fork 223
/
MessageExample.java
548 lines (489 loc) · 22.2 KB
/
MessageExample.java
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
package io.rong.example.message;
import io.rong.RongCloud;
import io.rong.messages.*;
import io.rong.methods.message._private.Private;
import io.rong.methods.message.chatroom.Chatroom;
import io.rong.methods.message.discussion.Discussion;
import io.rong.methods.message.group.Group;
import io.rong.methods.message.history.History;
import io.rong.methods.message.system.MsgSystem;
import io.rong.methods.message.ultragroup.UltraGroup;
import io.rong.models.message.*;
import io.rong.models.push.PlatformNotification;
import io.rong.models.response.HistoryMessageResult;
import io.rong.models.response.ResponseResult;
import io.rong.util.CodeUtil;
import io.rong.util.GsonUtil;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* 消息发送示例
*
* @author RongCloud
* @version 3.0.0
*/
public class MessageExample {
/**
* 此处替换成您的appKey
*/
private static final String appKey = "appKey";
/**
* 此处替换成您的appSecret
*/
private static final String appSecret = "appSecret";
private static final UserInfo userinfo = new UserInfo("rc1", "rc_user1",
"http://www.rongcloud.cn/images/logo.png", "");
private static final TxtMessage txtMessage = new TxtMessage("helloAAAA", "helloExtra");
private static final VoiceMessage voiceMessage = new VoiceMessage("hello", "helloExtra", 20L);
/**
* 自定义api地址
*/
// private static final String api = "http://api-bj.ronghub.com";
public static void main(String[] args) throws Exception {
RongCloud rongCloud = RongCloud.getInstance(appKey, appSecret);
// RongCloudConfig config = RongCloudConfig.DefaultConfig;
// config.connectionKeepAlive = false;
// RongCloud rongCloud = RongCloud.getInstance(appKey, appSecret, config);
//自定义 api 地址方式
// RongCloud rongCloud = RongCloud.getInstance("appkey", "appSecret", api);
// RongCloud rongCloud2 = RongCloud.getInstance("appKey", "appSecret", new RongCloudConfig("api"));
Private Private = rongCloud.message.msgPrivate;
MsgSystem system = rongCloud.message.system;
Group group = rongCloud.message.group;
Chatroom chatroom = rongCloud.message.chatroom;
Discussion discussion = rongCloud.message.discussion;
History history = rongCloud.message.history;
UltraGroup ultraGroup = rongCloud.message.ultraGroup;
PushExt.HW hw = new PushExt.HW("channelId", "NORMAL");
// 扩展参数
hw.addParamIfNotBlank("image", "xxx");
PushExt pe = PushExt.build("testTitle", 1,
hw,
new PushExt.VIVO("1"),
new PushExt.HONOR("importance", "image"),
new PushExt.APNs("234353efsfwd", "232"),
new PushExt.OPPO("134324"),
new PushExt.FCM("134324"),
new PushExt.OHOS("category", "image"),
new PushExt.MI("12345", "icon_test")
);
/**
* API 文档: https://doc.rongcloud.cn/imserver/server/v1/im-server-api-list-v1
*
* 发送系统消息
*
*/
String[] targetIds = {"2651280140445094444"};
SystemMessage systemMessage = new SystemMessage()
.setSenderId("usetId")
.setTargetId(targetIds)
.setObjectName(txtMessage.getType())
.setContent(txtMessage)
.setPushContent("this is a push")
.setPushData("{'pushData':'hello'}")
.setPushExt(pe)
.setIsPersisted(0)
.setIsCounted(0)
.setMsgRandom(System.currentTimeMillis())
.setContentAvailable(0);
ResponseResult result = system.send(systemMessage);
System.out.println("send system message: " + result.toString());
/**
* API 文档: https://doc.rongcloud.cn/imserver/server/v1/im-server-api-list-v1
*
* 发送系统模板消息方法
*
*/
Reader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(MessageExample.class.getClassLoader().getResourceAsStream("jsonsource/message/TemplateMessage.json")));
TemplateMessage template = (TemplateMessage) GsonUtil.fromJson(reader, TemplateMessage.class);
ResponseResult systemTemplateResult = system.sendTemplate(template);
System.out.println("send system template message: " + systemTemplateResult.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != reader) {
reader.close();
}
}
/**
* API 文档: https://doc.rongcloud.cn/imserver/server/v1/im-server-api-list-v1
*
* 发送系统模板消息方法
*
*/
BroadcastMessage message = new BroadcastMessage()
.setSenderId("Hji8yh76")
.setObjectName(txtMessage.getType())
.setContent(txtMessage)
.setPushContent("this is a push")
.setPushData("{'pushData':'hello'}")
.setOs("iOS");
ResponseResult broadcastResult = rongCloud.message.system.broadcast(message);
System.out.println("send broadcast: " + broadcastResult.toString());
/**
* API 文档: https://doc.rongcloud.cn/imserver/server/v1/im-server-api-list-v1
*
* 不落地通知
*
*/
List<String> users = new ArrayList<>();
users.add("user1");
PushUserMessage.Notification notification = new PushUserMessage.Notification()
.setTitle("testtitle")
.setPushContent("testcontent");
PlatformNotification notification1 = new PlatformNotification();
notification1.setHw("testhw");
notification1.setTitle("hwtitle");
notification.setAndroid(notification1);
PushUserMessage pushUserMessage = new PushUserMessage()
.setUserIds(users)
.setNotification(notification);
ResponseResult sendUser = rongCloud.message.system.sendUser(pushUserMessage);
System.out.println("sendUser: " + sendUser.toString());
/**
* API 文档: https://doc.rongcloud.cn/imserver/server/v1/im-server-api-list-v1
*
* 发送单聊消息<文本, 语音, 文件类型 等消息类型>
*/
PrivateMessage privateMessage = new PrivateMessage()
.setSenderId("2609751433442958892")
.setTargetId(targetIds)
.setObjectName(voiceMessage.getType())
.setContent(voiceMessage)
.setPushContent("")
.setPushData("{\"pushData\":\"hello\"}")
.setPushExt("{\"title\":\"\",\"forceShowPushContent\":0,\"pushConfigs\":{\"HW\":{\"channelId\":\"\"},\"MI\":{\"channelId\":\"\"},\"OPPO\":{\"channelId\":\"\"}}}")
.setCount("4")
.setVerifyBlacklist(0)
.setIsPersisted(0)
.setIsCounted(0)
.setIsIncludeSender(0);
ResponseResult privateResult = Private.send(privateMessage);
System.out.println("send private getReqBody: " + privateResult.getReqBody());
System.out.println("send private message: " + privateResult.toString());
/**
* API 文档: https://doc.rongcloud.cn/imserver/server/v1/im-server-api-list-v1
*
* 发送单聊模板消息方法
*/
try {
reader = new BufferedReader(new InputStreamReader(MessageExample.class.getClassLoader().getResourceAsStream("jsonsource/message/TemplateMessage.json")));
TemplateMessage template = (TemplateMessage) GsonUtil.fromJson(reader, TemplateMessage.class);
ResponseResult messagePublishTemplateResult = Private.sendTemplate(template);
System.out.println("send privateTemplate message: " + messagePublishTemplateResult.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != reader) {
reader.close();
}
}
/**
* API 文档: https://doc.rongcloud.cn/imserver/server/v1/im-server-api-list-v1
*
* 撤回单聊消息
* */
RecallMessage recallMessage = new RecallMessage()
.setSenderId("2609751433442958892")
.setTargetId("2651280140445094444")
.setuId("5H6P-CGC6-44QR-VB3R")
.setSentTime("1519444243981");
ResponseResult recallPrivateResult = (ResponseResult) Private.recall(recallMessage);
System.out.println("recall private: " + recallPrivateResult.toString());
/**
* API 文档: https://doc.rongcloud.cn/imserver/server/v1/im-server-api-list-v1
*
* 发送单聊状态消息
*/
PrivateStatusMessage statusMessage = new PrivateStatusMessage()
.setSenderId("IotBnm9K4")
.setTargetId(new String[]{"jf8yVWgZO"})
.setObjectName(txtMessage.getType())
.setContent(txtMessage);
ResponseResult statusMessageResult = Private.sendStatusMessage(statusMessage);
System.out.println("private status message result: " + statusMessageResult.toString());
/**
* API 文档: https://doc.rongcloud.cn/imserver/server/v1/im-server-api-list-v1
*
* 单聊-发送正在输入状态消息
*
* 正在输入状态消息只支持单聊文本消息,不支持其他消息类型(包括自定义消息)
*/
TypingStatusMessage typpingStatusMessage = new TypingStatusMessage();
PrivateMessage privateMsg = new PrivateMessage()
.setSenderId("BzUPcKM2B")
.setTargetId(new String[]{"jf8yVWgZO"})
.setObjectName(typpingStatusMessage.getType())
.setContent(typpingStatusMessage);
ResponseResult statusResult = Private.sendTypingStatusMessage(privateMsg);
System.out.println("send private message: " + statusResult.toString());
/**
* API 文档: https://doc.rongcloud.cn/imserver/server/v1/im-server-api-list-v1
* 发送单聊小灰条消息
*/
InfoNtfMessage infoNotify = new InfoNtfMessage("小灰条消息内容", "helloExtra");
PrivateMessage p = new PrivateMessage()
.setSenderId("BzUPcKM2B")
.setTargetId(new String[]{"jf8yVWgZO"})
.setObjectName(infoNotify.getType())
.setContent(infoNotify);
ResponseResult infoNotifyResult = Private.send(p);
System.out.println("send private infoNotify message: " + infoNotifyResult.toString());
/**
* 发送单聊已读回执消息(会话类型可设置)
*/
ReadReceiptMessage receiptMessage = new ReadReceiptMessage("1589425641984", "BI24-J9K0-0007-VD72", CodeUtil.ConversationType.PRIVATE.getIntValue());
PrivateMessage privateReceipt = new PrivateMessage()
.setSenderId("BzUPcKM2B")
.setTargetId(new String[]{"jf8yVWgZO"})
.setObjectName(receiptMessage.getType())
.setContent(receiptMessage);
ResponseResult privateReceiptResult = Private.send(privateReceipt);
System.out.println("send private ReceiptResult message: " + privateReceiptResult.toString());
/**
* API 文档: https://doc.rongcloud.cn/imserver/server/v1/im-server-api-list-v1
*
* 群组消息
* */
GroupMessage groupMessage = new GroupMessage()
.setSenderId("userId")
.setTargetId(targetIds)
.setObjectName(txtMessage.getType())
.setContent(txtMessage)
.setPushContent("this is a push")
.setPushData("{\"pushData\":\"hello\"}")
.setPushExt(pe)
.setIsPersisted(0)
.setIsIncludeSender(0)
.setContentAvailable(0);
ResponseResult groupResult = group.send(groupMessage);
System.out.println("send Group message: " + groupResult.toString());
/**
* API 文档: https://doc.rongcloud.cn/imserver/server/v1/im-server-api-list-v1
*
* 群组定向消息
* */
String[] targetId = {"2651280140445094444"};
String[] toUserIds = {"2651280140445094444"};
GroupMessage groupDirectionMessage = new GroupMessage()
.setSenderId("userId")
.setTargetId(targetId)
.setToUserId(toUserIds)
.setObjectName(txtMessage.getType())
.setContent(txtMessage)
.setPushContent("this is a push")
.setPushData("{\"pushData\":\"hello\"}")
.setPushExt(pe)
.setIsPersisted(0)
.setIsIncludeSender(0)
.setContentAvailable(0);
ResponseResult groupDirectionResult = group.sendDirection(groupDirectionMessage);
System.out.println("send group direction message: " + groupDirectionResult.toString());
/**
* API 文档: https://doc.rongcloud.cn/imserver/server/v1/im-server-api-list-v1
*
* 群组撤回消息
* */
recallMessage = new RecallMessage()
.setSenderId("sea9901")
.setTargetId("markoiwm")
.setuId("5GSB-RPM1-KP8H-9JHF")
.setSentTime("1522242030641");
ResponseResult recallMessageResult = (ResponseResult) group.recall(recallMessage);
System.out.println("recall group message: " + recallMessageResult.toString());
/**
* API 文档: https://doc.rongcloud.cn/imserver/server/v1/im-server-api-list-v1
*
* 群组@消息
* */
//要@的人
String[] mentionIds = {"jf8yVWgZO"};
// String[] targetIds = { "ckHduTB4f" };
MentionedInfo mentionedInfo = new MentionedInfo(1, mentionIds, "");
//@内容
MentionMessageContent content = new MentionMessageContent(txtMessage, mentionedInfo);
MentionMessage mentionMessage = new MentionMessage()
.setSenderId("BzUPcKM2B")
.setTargetId(targetIds)
.setObjectName(txtMessage.getType())
.setContent(content)
.setPushContent("this is a push")
.setPushData("{\"pushData\":\"hello\"}")
.setPushExt(GsonUtil.toJson(pe, PushExt.class))
.setIsPersisted(0)
.setIsCounted(0)
.setIsIncludeSender(0)
.setContentAvailable(0);
ResponseResult mentionResult = rongCloud.message.group.sendMention(mentionMessage);
System.out.println("group mention result: " + mentionResult.toString());
/**
* 发送群组状态消息
*
* API 文档: https://doc.rongcloud.cn/imserver/server/v1/im-server-api-list-v1
*/
GroupStatusMessage groupStatusMessage = new GroupStatusMessage();
groupStatusMessage.setSenderId("BzUPcKM2B")
.setGroupId(new String[]{"ckHduTB4f"})
.setContent(txtMessage)
.setObjectName(txtMessage.getType())
.setVerifyBlacklist(0)
.setIsIncludeSender(1);
ResponseResult groupStatusResult = group.sendStatusMessage(groupStatusMessage);
System.out.println("group status message result: " + groupStatusResult.toString());
/**
* 发送群聊小灰条消息(所有人)
*/
GroupMessage groupMessage2 = new GroupMessage()
.setSenderId("BzUPcKM2B")
.setTargetId(targetIds)
.setObjectName(infoNotify.getType())
.setContent(infoNotify);
ResponseResult groupinfoNotifyResult = group.send(groupMessage2);
System.out.println("group info Notify message result: " + groupinfoNotifyResult.toString());
/**
* 发送群聊小灰条消息-定向用户(单次请求最多 1000 人)
*/
String[] userIds = {"2651280140445094444", "2651280140445094445"};
GroupMessage groupMessage3 = new GroupMessage()
.setSenderId("BzUPcKM2B")
.setTargetId(targetIds)// 群Id
.setToUserId(userIds)// 群里的用户Id
.setObjectName(infoNotify.getType())
.setContent(infoNotify);
group.sendDirection(groupMessage3);
/**
* API 文档: https://doc.rongcloud.cn/imserver/server/v1/im-server-api-list-v1
*
* 发送讨论组消息
* */
String[] discussionIds = {"lijhGk87", "lijhGk88"};
DiscussionMessage discussionMessage = new DiscussionMessage()
.setSenderId("JuikH78ko")
.setTargetId(discussionIds)
.setObjectName(txtMessage.getType())
.setContent(txtMessage)
.setPushContent("this is a push")
.setPushData("{\"pushData\":\"hello\"}")
.setIsPersisted(0)
.setIsCounted(0)
.setIsIncludeSender(0)
.setContentAvailable(0);
ResponseResult discussionResult = discussion.send(discussionMessage);
System.out.println("send discussion message: " + discussionResult.toString());
/**
* API 文档: https://doc.rongcloud.cn/imserver/server/v1/im-server-api-list-v1
*
* 撤回讨论组消息
* */
recallMessage = new RecallMessage()
.setSenderId("sea9901")
.setTargetId("IXQhMs3ny")
.setuId("5GSB-RPM1-KP8H-9JHF")
.setSentTime("1519444243981");
ResponseResult recallDiscussionResult = (ResponseResult) discussion.recall(recallMessage);
System.out.println("recall discussion message: " + recallDiscussionResult.toString());
/**
* API 文档: https://doc.rongcloud.cn/imserver/server/v1/im-server-api-list-v1
*
* 聊天室消息
* */
String[] chatroomIds = {"15222258878654823358"};
CustomTxtMessage ctm = new CustomTxtMessage("hello");
ChatroomMessage chatroomMessage = new ChatroomMessage()
.setSenderId("1")
.setTargetId(chatroomIds)
.setContent(ctm)
.setIsIncludeSender(1)
.setPriority(1)
.setObjectName(ctm.getType());
ResponseResult chatroomResult = chatroom.send(chatroomMessage);
System.out.println("send chatroom message: " + chatroomResult.toString());
/**
* API 文档: https://doc.rongcloud.cn/imserver/server/v1/im-server-api-list-v1
*
* 聊天室撤回消息
* */
recallMessage = new RecallMessage()
.setSenderId("1")
.setTargetId("15222258878654823358")
.setuId("5GSB-RPM1-KP8H-9JHF")
.setSentTime("1522242030641");
ResponseResult recallChatroomResult = (ResponseResult) chatroom.recall(recallMessage);
System.out.println("recall chatroom message: " + recallChatroomResult.toString());
/**
* 超级群消息撤回
*/
recallMessage = new RecallMessage()
.setSenderId("1")
.setTargetId("15222258878654823358")
.setuId("5GSB-RPM1-KP8H-9JHF")
.setSentTime("1522242030641");
ResponseResult recallUltraGroupResult = (ResponseResult) ultraGroup.recall(recallMessage);
System.out.println("recall ultragroup message: " + recallUltraGroupResult.toString());
/**
*
* API 文档: https://doc.rongcloud.cn/imserver/server/v1/im-server-api-list-v1
*
* 聊天室广播消息
*
* 此功能需开通专有服务: http://www.rongcloud.cn/deployment#overseas-cloud
*
* */
chatroomMessage = new ChatroomMessage()
.setSenderId("bN6oQi8T5")
.setContent(txtMessage)
.setObjectName(txtMessage.getType());
ResponseResult chatroomBroadcastresult = chatroom.broadcast(chatroomMessage);
System.out.println("send chatroom broadcast message: " + chatroomBroadcastresult.toString());
/**
*
* API 文档: https://doc.rongcloud.cn/imserver/server/v1/im-server-api-list-v1
*
* 获取历史消息日志文件
*
* */
HistoryMessageResult historyMessageResult = (HistoryMessageResult) history.get("2018032810");
System.out.println("get history message: " + historyMessageResult.toString());
/**
*
* API 文档: https://doc.rongcloud.cn/imserver/server/v1/im-server-api-list-v1
*
* 删除历史消息日志文件
*
* */
ResponseResult removeHistoryMessageResult = history.remove("2018030210");
System.out.println("remove history message: " + removeHistoryMessageResult.toString());
/**
* 清除历史消息
*
* API 文档: https://docs.rongcloud.cn/im/server/message_clean/
*/
ResponseResult cleanResult = history.clean("1", "jf8yVWgZO", "IotBnm9K4", null);
System.out.println("clean history message: " + cleanResult.toString());
PushExt pushExt = new PushExt();
pushExt.setTitle("aaa");
pushExt.setTemplateId("22");
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("a", "a");
UltraGroupMessage ultraGroupMessage = new UltraGroupMessage()
.setSenderId("bN6oQi8T5")
.setContent(txtMessage)
.setTargetId(targetIds)
.setPushExt(pushExt)
.setExpansion(true)
.setExtraContent(hashMap)
.setObjectName(txtMessage.getType())
.setIsCounted(1);
/**
* 发送超级群消息
* */
ResponseResult send = ultraGroup.send(ultraGroupMessage);
System.out.println("send ultragroup message: " + send.toString());
}
}