增加公众号推送功能代码!

This commit is contained in:
2022-04-18 16:25:20 +08:00
parent d23826e056
commit cb8563ce5c
5 changed files with 305 additions and 47 deletions

View File

@@ -32,5 +32,15 @@
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" scope="TEST">
<library name="JUnit4">
<CLASSES>
<root url="jar://$MODULE_DIR$/lib/junit-4.13.1.jar!/" />
<root url="jar://$MODULE_DIR$/lib/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>

View File

@@ -21,6 +21,8 @@
</bytecodeTargetLevel>
</component>
<component name="Encoding" defaultCharsetForPropertiesFiles="UTF-8">
<file url="file://$PROJECT_DIR$/src/main/java/test/com/cpic/xim/wechat/officalAccount/PushMessageTest.java" charset="GBK" />
<file url="file://$PROJECT_DIR$/src/test/java/com/cpic/xim/wechat/officalAccount/PushMessageTest.java" charset="UTF-8" />
<file url="PROJECT" charset="UTF-8" />
</component>
<component name="ExportToHTMLSettings">

View File

@@ -1,10 +1,18 @@
package com.cpic.xim.wechat.officalAccount;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.HttpURLConnection;
public class PushMessage
{
@@ -22,24 +30,97 @@ public class PushMessage
String notifyMessage )
{
//设置推送内容
WechatOfficalAccountMessageParameter param = new WechatOfficalAccountMessageParameter();
//param.setUrl( url );
param.setFirst( title );
param.setKeyword1( notifyType );
param.setKeyword1Color( "#ff000000" );
param.setKeyword2( notifyMessage );
ObjectMapper mapper = new ObjectMapper();
//转换成json
ObjectMapper mapper = null;
String json = null;
try
{
String json = mapper.writeValueAsString( param );
mapper = new ObjectMapper();
mapper.setSerializationInclusion( JsonInclude.Include.NON_NULL );
json = mapper.writeValueAsString( param );
System.out.println( json );
}
catch ( JsonProcessingException error )
{
error.printStackTrace();
}
//推送数据
URL url = null;
HttpURLConnection connection = null;
StringBuffer result = new StringBuffer();
OutputStreamWriter out = null;
BufferedReader in = null;
try
{
url = new URL( wechatOfficalAccountURL );
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod( "POST" );
connection.setRequestProperty( "accept", "*/*" );
connection.setRequestProperty( "Connection", "Keep-Alive" );
connection.setRequestProperty( "Content-Type", "application/json" );
connection.setDoInput( true );
connection.setDoOutput( true );
connection.connect();
out = new OutputStreamWriter( connection.getOutputStream(), "UTF-8" );
out.write( json );
out.flush();
in = new BufferedReader( new InputStreamReader( connection.getInputStream() ) );
String line = in.readLine();
while ( line != null )
{
result.append( line );
line = in.readLine();
}
}
catch ( Exception error )
{
error.printStackTrace();
}
finally
{
try
{
if ( in != null )
{
in.close();
}
if ( out != null )
{
out.close();
}
if ( connection != null )
{
connection.disconnect();
}
}
catch ( Exception error )
{
error.printStackTrace();
}
}
}
}

View File

@@ -0,0 +1,24 @@
package com.cpic.xim.wechat.officalAccount;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.Test;
import static org.junit.Assert.*;
public class PushMessageTest
{
@Test
public void pushNotifyMessage()
{
String url = "https://cxxmwx.cpic.com.cn/app/index.php?i=2&c=entry&do=send_group_tpl_api&m=ok_tplmessage";
try
{
PushMessage.PushNotifyMessage( url, "警报", "警报标题", "警报内容!" );
}
catch ( Exception error )
{
fail("测试失败!");
}
}
}