項目中集成Openfire ios推送插件開發--(02)

Openfire ios推送插件開發
   openfire源碼結構如下:                   插件目錄:
                                                                                                                    


1.在plugins目錄下構建如下目錄:

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>  





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章