项目中集成Openfire ios推送插件开发--(02)

时间:2020-12-27 14:43:18
Openfire ios推送插件开发    openfire源码结构如下:                   插件目录:                                                                                                                      项目中集成Openfire ios推送插件开发--(02)项目中集成Openfire ios推送插件开发--(02)

1.在plugins目录下构建如下目录: 项目中集成Openfire ios推送插件开发--(02)
2.在java文件夹上鼠标右键执行Build path --use as source folder 3.在myeclipse 中ant编译后target--openfire---plugins 目录下会出现push.jar. 4.在mysql的ofproperty里加入三个参数值

add plugin.push.apnsPathPath ,      p12文件路径

plugin.push.apnsPathKey,                    密码

plugin.push.isProduct to ofProperty           是否正式发布

5.在后台管理页面中添加push插件后完成。

附代码:
<strong>PushInterceptor 类</strong>
package com.d3.push;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javapns.devices.Device;
import javapns.devices.implementations.basic.BasicDevice;
import javapns.notification.AppleNotificationServerBasicImpl;
import javapns.notification.PushNotificationManager;
import javapns.notification.PushNotificationPayload;
import javapns.notification.PushedNotification;

import org.apache.commons.lang.StringUtils;
import org.jivesoftware.database.DbConnectionManager;
import org.jivesoftware.openfire.PresenceManager;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.interceptor.InterceptorManager;
import org.jivesoftware.openfire.interceptor.PacketInterceptor;
import org.jivesoftware.openfire.interceptor.PacketRejectedException;
import org.jivesoftware.openfire.session.Session;
import org.jivesoftware.openfire.user.UserManager;
import org.jivesoftware.openfire.user.UserNotFoundException;
import org.jivesoftware.util.JiveGlobals;
import org.xmpp.packet.JID;
import org.xmpp.packet.Message;
import org.xmpp.packet.Packet;
import org.xmpp.packet.Presence;

/**
* <b>function:</b> send offline msg plugin
*
*/
public class PushInterceptor implements PacketInterceptor {
// Hook for intercpetorn
private InterceptorManager interceptorManager;
private UserManager userManager;
private PresenceManager presenceManager;

public PushInterceptor() {
interceptorManager = InterceptorManager.getInstance();
interceptorManager.addInterceptor(this);

XMPPServer server = XMPPServer.getInstance();
userManager = server.getUserManager();
presenceManager = server.getPresenceManager();
}

/**
* intercept message
*/
@Override
public void interceptPacket(Packet packet, Session session,
boolean incoming, boolean processed) throws PacketRejectedException {
if (processed || !(packet instanceof Message) || !incoming
|| Message.Type.chat != ((Message) packet).getType())
return;
this.doAction(packet, incoming, processed, session);

}

/**
* <b>send offline msg from this function </b>
*/
private void doAction(Packet packet, boolean incoming, boolean processed,
Session session) {
Message message = (Message) packet;
JID recipient = message.getTo();
// get message
try {
// if (recipient.getNode() == null
// ||
// !UserManager.getInstance().isRegisteredUser(recipient.getNode()))
// {
// // Sender is requesting presence information of an anonymous
// //throw new UserNotFoundException("Username is null");
// }

Presence status = presenceManager.getPresence(userManager
.getUser(recipient.getNode()));
if (status == null) { // offline
String deviceToken = getDeviceToken(recipient.getNode());
if (isApple(deviceToken))
pns(deviceToken, message.getBody());
}// end if

} catch (UserNotFoundException e) {
System.out.println("user not found");
// e.printStackTrace();
}
}

/**
* 判断是否苹果
*
* @param deviceToken
* @return
*/
private boolean isApple(String deviceToken) {
if (deviceToken != null && deviceToken.length() > 0) {
return true;
}
return false;
}

public String getDeviceToken(String userId) {
String deviceToken = "";
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con
.prepareStatement("SELECT code_ios FROM ofUser where username = ?");
pstmt.setString(1, userId);
rs = pstmt.executeQuery();
if (rs.next()) {
deviceToken = rs.getString(1);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DbConnectionManager.closeConnection(rs, pstmt, con);
}
return deviceToken;
}

public void pns(String token, String msg) {
String sound = "default";// 铃音
String certificatePath = JiveGlobals.getProperty(
"plugin.push.apnsPath", "");
String certificatePassword = JiveGlobals.getProperty(
"plugin.push.apnsKey", ""); // 此处注意导出的证书密码不能为空因为空密码会报错
boolean isProduct = JiveGlobals.getBooleanProperty(
"plugin.push.isProduct", false);
try {
PushNotificationPayload payLoad = new PushNotificationPayload();
payLoad.addAlert(msg); // 消息内容
payLoad.addBadge(1); // iphone应用图标上小红圈上的数值
if (!StringUtils.isBlank(sound)) {
payLoad.addSound(sound);// 铃音
}
PushNotificationManager pushManager = new PushNotificationManager();
// true:表示的是产品发布推送服务 false:表示的是产品测试推送服务
pushManager
.initializeConnection(new AppleNotificationServerBasicImpl(
certificatePath, certificatePassword, isProduct));
// 发送push消息
Device device = new BasicDevice();
device.setToken(token);
PushedNotification notification = pushManager.sendNotification(
device, payLoad, true);
pushManager.stopConnection();
} catch (Exception e) {
e.printStackTrace();
}
}

}

<strong>PushPlugin 类</strong>
package com.d3.push;
import java.io.File;

import org.jivesoftware.openfire.container.Plugin;
import org.jivesoftware.openfire.container.PluginManager;
import org.jivesoftware.openfire.interceptor.InterceptorManager;

import com.d3.push.PushInterceptor;

public class PushPlugin implements Plugin {

private PushInterceptor pushInterceptor = null;

@Override
public void destroyPlugin() {
if (pushInterceptor != null) {
InterceptorManager.getInstance().removeInterceptor(pushInterceptor);
}
}

@Override
public void initializePlugin(PluginManager manager, File pluginDirectory) {
pushInterceptor = new PushInterceptor();
InterceptorManager.getInstance().addInterceptor(pushInterceptor);
}

}

plugin.xml
<?xml version="1.0" encoding="UTF-8"?>  
<plugin>
<class>com.d3.push.PushPlugin</class>
<name>IOSPush</name>
<description>IOS Push</description>
<author>ccs</author>
<version>1.0.0</version>
<date>13/04/2015</date>
<minServerVersion>1.0.0</minServerVersion>
</plugin>