mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-06-28 13:16:19 +08:00
#1075 企业微信支持推送小程序通知消息
This commit is contained in:
parent
cb4e3f3eb0
commit
a4f5aa341b
@ -98,6 +98,11 @@ public class WxConsts {
|
|||||||
* 菜单消息.
|
* 菜单消息.
|
||||||
*/
|
*/
|
||||||
public static final String MSGMENU = "msgmenu";
|
public static final String MSGMENU = "msgmenu";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 小程序通知消息.
|
||||||
|
*/
|
||||||
|
public static final String MINIPROGRAM_NOTICE = "miniprogram_notice";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -8,11 +8,15 @@ import me.chanjar.weixin.cp.bean.article.MpnewsArticle;
|
|||||||
import me.chanjar.weixin.cp.bean.article.NewArticle;
|
import me.chanjar.weixin.cp.bean.article.NewArticle;
|
||||||
import me.chanjar.weixin.cp.bean.messagebuilder.*;
|
import me.chanjar.weixin.cp.bean.messagebuilder.*;
|
||||||
import me.chanjar.weixin.cp.bean.taskcard.TaskCardButton;
|
import me.chanjar.weixin.cp.bean.taskcard.TaskCardButton;
|
||||||
|
import me.chanjar.weixin.cp.constant.WxCpConsts;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static me.chanjar.weixin.common.api.WxConsts.KefuMsgType.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 消息.
|
* 消息.
|
||||||
@ -40,9 +44,13 @@ public class WxCpMessage implements Serializable {
|
|||||||
private String btnTxt;
|
private String btnTxt;
|
||||||
private List<NewArticle> articles = new ArrayList<>();
|
private List<NewArticle> articles = new ArrayList<>();
|
||||||
private List<MpnewsArticle> mpnewsArticles = new ArrayList<>();
|
private List<MpnewsArticle> mpnewsArticles = new ArrayList<>();
|
||||||
|
private String appId;
|
||||||
|
private String page;
|
||||||
|
private Boolean emphasisFirstItem;
|
||||||
|
private Map<String, String> contentItems;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 任务卡片特有的属性
|
* 任务卡片特有的属性.
|
||||||
*/
|
*/
|
||||||
private String taskId;
|
private String taskId;
|
||||||
private List<TaskCardButton> taskButtons = new ArrayList<>();
|
private List<TaskCardButton> taskButtons = new ArrayList<>();
|
||||||
@ -117,10 +125,16 @@ public class WxCpMessage implements Serializable {
|
|||||||
return new TaskCardBuilder();
|
return new TaskCardBuilder();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获得小程序通知消息builder.
|
||||||
|
*/
|
||||||
|
public static MiniProgramNoticeMsgBuilder newMiniProgramNoticeBuilder() {
|
||||||
|
return new MiniProgramNoticeMsgBuilder();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <pre>
|
* <pre>
|
||||||
* 请使用
|
* 请使用.
|
||||||
* {@link KefuMsgType#TEXT}
|
* {@link KefuMsgType#TEXT}
|
||||||
* {@link KefuMsgType#IMAGE}
|
* {@link KefuMsgType#IMAGE}
|
||||||
* {@link KefuMsgType#VOICE}
|
* {@link KefuMsgType#VOICE}
|
||||||
@ -130,6 +144,7 @@ public class WxCpMessage implements Serializable {
|
|||||||
* {@link KefuMsgType#MPNEWS}
|
* {@link KefuMsgType#MPNEWS}
|
||||||
* {@link KefuMsgType#MARKDOWN}
|
* {@link KefuMsgType#MARKDOWN}
|
||||||
* {@link KefuMsgType#TASKCARD}
|
* {@link KefuMsgType#TASKCARD}
|
||||||
|
* {@link KefuMsgType#MINIPROGRAM_NOTICE}
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @param msgType 消息类型
|
* @param msgType 消息类型
|
||||||
@ -169,19 +184,19 @@ public class WxCpMessage implements Serializable {
|
|||||||
|
|
||||||
private void handleMsgType(JsonObject messageJson) {
|
private void handleMsgType(JsonObject messageJson) {
|
||||||
switch (this.getMsgType()) {
|
switch (this.getMsgType()) {
|
||||||
case KefuMsgType.TEXT: {
|
case TEXT: {
|
||||||
JsonObject text = new JsonObject();
|
JsonObject text = new JsonObject();
|
||||||
text.addProperty("content", this.getContent());
|
text.addProperty("content", this.getContent());
|
||||||
messageJson.add("text", text);
|
messageJson.add("text", text);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case KefuMsgType.MARKDOWN: {
|
case MARKDOWN: {
|
||||||
JsonObject text = new JsonObject();
|
JsonObject text = new JsonObject();
|
||||||
text.addProperty("content", this.getContent());
|
text.addProperty("content", this.getContent());
|
||||||
messageJson.add("markdown", text);
|
messageJson.add("markdown", text);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case KefuMsgType.TEXTCARD: {
|
case TEXTCARD: {
|
||||||
JsonObject text = new JsonObject();
|
JsonObject text = new JsonObject();
|
||||||
text.addProperty("title", this.getTitle());
|
text.addProperty("title", this.getTitle());
|
||||||
text.addProperty("description", this.getDescription());
|
text.addProperty("description", this.getDescription());
|
||||||
@ -190,25 +205,25 @@ public class WxCpMessage implements Serializable {
|
|||||||
messageJson.add("textcard", text);
|
messageJson.add("textcard", text);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case KefuMsgType.IMAGE: {
|
case IMAGE: {
|
||||||
JsonObject image = new JsonObject();
|
JsonObject image = new JsonObject();
|
||||||
image.addProperty("media_id", this.getMediaId());
|
image.addProperty("media_id", this.getMediaId());
|
||||||
messageJson.add("image", image);
|
messageJson.add("image", image);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case KefuMsgType.FILE: {
|
case FILE: {
|
||||||
JsonObject image = new JsonObject();
|
JsonObject image = new JsonObject();
|
||||||
image.addProperty("media_id", this.getMediaId());
|
image.addProperty("media_id", this.getMediaId());
|
||||||
messageJson.add("file", image);
|
messageJson.add("file", image);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case KefuMsgType.VOICE: {
|
case VOICE: {
|
||||||
JsonObject voice = new JsonObject();
|
JsonObject voice = new JsonObject();
|
||||||
voice.addProperty("media_id", this.getMediaId());
|
voice.addProperty("media_id", this.getMediaId());
|
||||||
messageJson.add("voice", voice);
|
messageJson.add("voice", voice);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case KefuMsgType.VIDEO: {
|
case VIDEO: {
|
||||||
JsonObject video = new JsonObject();
|
JsonObject video = new JsonObject();
|
||||||
video.addProperty("media_id", this.getMediaId());
|
video.addProperty("media_id", this.getMediaId());
|
||||||
video.addProperty("thumb_media_id", this.getThumbMediaId());
|
video.addProperty("thumb_media_id", this.getThumbMediaId());
|
||||||
@ -217,7 +232,7 @@ public class WxCpMessage implements Serializable {
|
|||||||
messageJson.add("video", video);
|
messageJson.add("video", video);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case KefuMsgType.NEWS: {
|
case NEWS: {
|
||||||
JsonObject newsJsonObject = new JsonObject();
|
JsonObject newsJsonObject = new JsonObject();
|
||||||
JsonArray articleJsonArray = new JsonArray();
|
JsonArray articleJsonArray = new JsonArray();
|
||||||
for (NewArticle article : this.getArticles()) {
|
for (NewArticle article : this.getArticles()) {
|
||||||
@ -232,7 +247,7 @@ public class WxCpMessage implements Serializable {
|
|||||||
messageJson.add("news", newsJsonObject);
|
messageJson.add("news", newsJsonObject);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case KefuMsgType.MPNEWS: {
|
case MPNEWS: {
|
||||||
JsonObject newsJsonObject = new JsonObject();
|
JsonObject newsJsonObject = new JsonObject();
|
||||||
if (this.getMediaId() != null) {
|
if (this.getMediaId() != null) {
|
||||||
newsJsonObject.addProperty("media_id", this.getMediaId());
|
newsJsonObject.addProperty("media_id", this.getMediaId());
|
||||||
@ -255,7 +270,7 @@ public class WxCpMessage implements Serializable {
|
|||||||
messageJson.add("mpnews", newsJsonObject);
|
messageJson.add("mpnews", newsJsonObject);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case KefuMsgType.TASKCARD: {
|
case TASKCARD: {
|
||||||
JsonObject text = new JsonObject();
|
JsonObject text = new JsonObject();
|
||||||
text.addProperty("title", this.getTitle());
|
text.addProperty("title", this.getTitle());
|
||||||
text.addProperty("description", this.getDescription());
|
text.addProperty("description", this.getDescription());
|
||||||
@ -291,6 +306,25 @@ public class WxCpMessage implements Serializable {
|
|||||||
messageJson.add("taskcard", text);
|
messageJson.add("taskcard", text);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case MINIPROGRAM_NOTICE: {
|
||||||
|
JsonObject notice = new JsonObject();
|
||||||
|
notice.addProperty("appid", this.getAppId());
|
||||||
|
notice.addProperty("page", this.getPage());
|
||||||
|
notice.addProperty("description", this.getDescription());
|
||||||
|
notice.addProperty("title", this.getTitle());
|
||||||
|
notice.addProperty("emphasis_first_item", this.getEmphasisFirstItem());
|
||||||
|
JsonArray content = new JsonArray();
|
||||||
|
for (Map.Entry<String, String> item : this.getContentItems().entrySet()) {
|
||||||
|
JsonObject articleJson = new JsonObject();
|
||||||
|
articleJson.addProperty("key", item.getKey());
|
||||||
|
articleJson.addProperty("value", item.getValue());
|
||||||
|
content.add(articleJson);
|
||||||
|
}
|
||||||
|
notice.add("content_item", content);
|
||||||
|
|
||||||
|
messageJson.add("miniprogram_notice", notice);
|
||||||
|
break;
|
||||||
|
}
|
||||||
default: {
|
default: {
|
||||||
// do nothing
|
// do nothing
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,69 @@
|
|||||||
|
package me.chanjar.weixin.cp.bean.messagebuilder;
|
||||||
|
|
||||||
|
import me.chanjar.weixin.common.api.WxConsts;
|
||||||
|
import me.chanjar.weixin.cp.bean.WxCpMessage;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <pre>
|
||||||
|
* miniprogram_notice 类型的消息 builder
|
||||||
|
* Created by Binary Wang on 2019/6/16.
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||||
|
*/
|
||||||
|
public class MiniProgramNoticeMsgBuilder extends BaseBuilder<MiniProgramNoticeMsgBuilder> {
|
||||||
|
private String title;
|
||||||
|
private String description;
|
||||||
|
private String appId;
|
||||||
|
private String page;
|
||||||
|
private Boolean emphasisFirstItem;
|
||||||
|
private Map<String, String> contentItems;
|
||||||
|
|
||||||
|
public MiniProgramNoticeMsgBuilder() {
|
||||||
|
this.msgType = WxConsts.KefuMsgType.MINIPROGRAM_NOTICE;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MiniProgramNoticeMsgBuilder appId(String appId) {
|
||||||
|
this.appId = appId;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MiniProgramNoticeMsgBuilder page(String page) {
|
||||||
|
this.page = page;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MiniProgramNoticeMsgBuilder title(String title) {
|
||||||
|
this.title = title;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MiniProgramNoticeMsgBuilder description(String description) {
|
||||||
|
this.description = description;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MiniProgramNoticeMsgBuilder contentItems(Map<String, String> contentItems) {
|
||||||
|
this.contentItems = contentItems;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MiniProgramNoticeMsgBuilder emphasisFirstItem(Boolean emphasisFirstItem) {
|
||||||
|
this.emphasisFirstItem = emphasisFirstItem;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public WxCpMessage build() {
|
||||||
|
WxCpMessage m = super.build();
|
||||||
|
m.setContentItems(this.contentItems);
|
||||||
|
m.setAppId(this.appId);
|
||||||
|
m.setDescription(this.description);
|
||||||
|
m.setTitle(this.title);
|
||||||
|
m.setEmphasisFirstItem(this.emphasisFirstItem);
|
||||||
|
m.setPage(this.page);
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
package me.chanjar.weixin.cp.api;
|
package me.chanjar.weixin.cp.api;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableMap;
|
||||||
import org.testng.annotations.*;
|
import org.testng.annotations.*;
|
||||||
|
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
@ -107,4 +108,27 @@ public class WxCpMessageAPITest {
|
|||||||
System.out.println(messageSendResult.getInvalidUserList());
|
System.out.println(messageSendResult.getInvalidUserList());
|
||||||
System.out.println(messageSendResult.getInvalidTagList());
|
System.out.println(messageSendResult.getInvalidTagList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSendMessage_miniprogram_notice() throws WxErrorException {
|
||||||
|
WxCpMessage message = WxCpMessage
|
||||||
|
.newMiniProgramNoticeBuilder()
|
||||||
|
.toUser(configStorage.getUserId())
|
||||||
|
.appId("wx123123123123123")
|
||||||
|
.page("pages/index?userid=zhangsan&orderid=123123123")
|
||||||
|
.title("会议室预订成功通知")
|
||||||
|
.description("4月27日 16:16")
|
||||||
|
.emphasisFirstItem(true)
|
||||||
|
.contentItems(ImmutableMap.of("会议室","402",
|
||||||
|
"会议地点","广州TIT-402会议室",
|
||||||
|
"会议时间","2018年8月1日 09:00-09:30"))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
WxCpMessageSendResult messageSendResult = this.wxService.messageSend(message);
|
||||||
|
assertNotNull(messageSendResult);
|
||||||
|
System.out.println(messageSendResult);
|
||||||
|
System.out.println(messageSendResult.getInvalidPartyList());
|
||||||
|
System.out.println(messageSendResult.getInvalidUserList());
|
||||||
|
System.out.println(messageSendResult.getInvalidTagList());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user