image.png
介紹
XMPP協(xié)議的推送實(shí)現(xiàn)采用的smack庫(kù)艰管,可以與不同的java框架整合使用,這次我使用的springboot框架實(shí)現(xiàn)。
這篇文章主要寫代碼的實(shí)現(xiàn)娘赴,對(duì)XMPP不清楚的,可以看前幾篇文章:
搭建springboot項(xiàng)目
這里寫圖片描述
pom.xml文件,加入jar包
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--xmpp smack-->
<dependency>
<groupId>org.igniterealtime.smack</groupId>
<artifactId>smack-core</artifactId>
<version>4.1.8</version>
</dependency>
<dependency>
<groupId>org.igniterealtime.smack</groupId>
<artifactId>smack-tcp</artifactId>
<version>4.1.8</version>
</dependency>
<dependency>
<groupId>org.igniterealtime.smack</groupId>
<artifactId>smack-java7</artifactId>
<version>4.1.8</version>
</dependency>
<dependency>
<groupId>org.igniterealtime.smack</groupId>
<artifactId>smack-extensions</artifactId>
<version>4.1.8</version>
</dependency>
<dependency>
<groupId>org.igniterealtime.smack</groupId>
<artifactId>smack-sasl-provided</artifactId>
<version>4.1.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/libs-snapshot</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/libs-snapshot</url>
</pluginRepository>
</pluginRepositories>
</project>
與smack結(jié)合主要使用的是上面的5個(gè)jar包跟啤,smack的功能還有很多诽表,需要的可以去看看官方的API文檔
1、smack-core.jar XMPP RFC規(guī)范定義的XMPP核心功能隅肥。
2竿奏、smack-extensions.jar XMPP Standards Foundation定義的擴(kuò)展(XEP)功能。 包括群聊腥放、文件傳輸泛啸、用戶搜索等等。
官方文檔地址:
http://download.igniterealtime.org/smack/docs/latest/javadoc/
配置openfire地址
#openfire服務(wù)器地址
openfire.server=127.0.0.1
發(fā)送XMPP消息
連接XMPP服務(wù)器
XMPPTCPConnectionConfiguration.Builder config=XMPPTCPConnectionConfiguration.builder();
config.setServiceName(openfireServer);
config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
config.setSendPresence(true);
config.setCompressionEnabled(false);
XMPPTCPConnection con = new XMPPTCPConnection(config.build());
SASLAuthentication.blacklistSASLMechanism("SCRAM-SHA-1");
SASLAuthentication.blacklistSASLMechanism("DIGEST-MD5");
SASLAuthentication.blacklistSASLMechanism("CRAM-MD5");
return con;
實(shí)現(xiàn)代碼
package com.example.demo;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.sasl.provided.SASLPlainMechanism;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import org.jivesoftware.smackx.search.ReportedData;
import org.jivesoftware.smackx.search.UserSearchManager;
import org.jivesoftware.smackx.xdata.Form;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* info:
* Created by shang on 2017/8/1.
*/
@RestController
public class SmackController {
@Value("${openfire.server}")
protected String openfireServer;
@RequestMapping("")
public Object index(String mobile, String body) {
return sendSmackMessage(mobile, body, false);
}
public boolean sendSmackMessage(String mobile, String body,boolean deliveryReceiptRequest) {
try {
if (StringUtils.isEmpty(mobile)) {
return false;
}
XMPPTCPConnection con = getXmpptcpConnection();
con.connect();
if (con.isConnected()) {
SASLAuthentication.registerSASLMechanism(new SASLPlainMechanism());
con.loginAnonymously();//匿名登錄
UserSearchManager userSearchManager=new UserSearchManager(con);
Form searchForm=userSearchManager.getSearchForm("search."+con.getServiceName());
Form answerForm = searchForm.createAnswerForm();
answerForm.setAnswer("Username", true);
answerForm.setAnswer("search", mobile);
ReportedData data=userSearchManager.getSearchResults(answerForm,"search."+con.getServiceName());
List<ReportedData.Row> list = data.getRows();
for (ReportedData.Row row : list) {
for (String jid : row.getValues("jid")) {
Message m = new Message();
m.setBody(body);//設(shè)置消息秃症。
m.setTo(jid);//設(shè)置發(fā)送目標(biāo)
if(deliveryReceiptRequest){
m.setType(Message.Type.normal);
}else{
m.setType(Message.Type.headline);
}
m.setSubject(body);
con.sendStanza(m);
}
}
}
con.disconnect();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
private XMPPTCPConnection getXmpptcpConnection() {
XMPPTCPConnectionConfiguration.Builder config=XMPPTCPConnectionConfiguration.builder();
config.setServiceName(openfireServer);
config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
config.setSendPresence(true);
config.setCompressionEnabled(false);
XMPPTCPConnection con = new XMPPTCPConnection(config.build());
SASLAuthentication.blacklistSASLMechanism("SCRAM-SHA-1");
SASLAuthentication.blacklistSASLMechanism("DIGEST-MD5");
SASLAuthentication.blacklistSASLMechanism("CRAM-MD5");
return con;
}
}
代碼解釋
- getXmpptcpConnection方法為連接XMPP
- sendSmackMessage為發(fā)送消息的方法候址,mobile為接收人,body為發(fā)送的內(nèi)容伍纫,deliveryReceiptRequest為是否是離線消息
- 代碼中采用的是匿名發(fā)送宗雇,也可以指定一個(gè)賬號(hào)作為發(fā)送方
- 這里我根據(jù)需要還用了模糊匹配賬號(hào),給匹配的賬號(hào)都發(fā)送了消息
- Message.Type.normal與Message.Type.headline是是否離線消息
測(cè)試
啟動(dòng)項(xiàng)目后,瀏覽器輸入:http://localhost:8080/?mobile=18758120932&body=測(cè)試測(cè)試
登錄spray客戶端莹规,就會(huì)接收到消息
這里寫圖片描述
結(jié)束語(yǔ)
以上代碼使用就是我在項(xiàng)目中對(duì)推送服務(wù)的使用赔蒲,如果在APP內(nèi)用這樣的推送業(yè)務(wù),可以選擇XMPP服務(wù)作為你的選擇良漱。
更多的smack的使用可以查看官方文檔舞虱,聊天,分組母市,添加好友等功能矾兜,完全可以滿足你的各種需求。
有問(wèn)題歡迎給我留言~