android代码:
OpenfireManager.getInstance().init(RegisterActivity.this, userName, pwd);
try {
OpenfireManager.getInstance().connect();
OpenfireManager.getInstance().login();
//这里一定要设置为在线,否则收不到消息
OpenfireManager.getInstance().setOnLine();
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e(TAG, "登录openfire失败");
//Toast.makeText(RegisterActivity.this, "登录失败",
//Toast.LENGTH_SHORT).show();
}
接收消息处理,需要实现PacketListener接口,实现processPacket方法
@Override
public void processPacket(Packet packet) {
org.jivesoftware.smack.packet.Message message = (org.jivesoftware.smack.packet.Message) packet;
handleMessage(message);
}
/**
* 处理接收到的消息
*/
private void handleMessage(org.jivesoftware.smack.packet.Message message) {
NotificationUtils notifyUtils = new NotificationUtils(getBaseContext());
int noticeId = new Random().nextInt();
// Intent intent = new Intent(getBaseContext(),NoticeInfo.class);
// String msgid = (String)message.getProperty("MSGID");
// intent.putExtra("MSGID", msgid);
regCode = message.getBody();
Log.d(TAG, "regCode:" + regCode);
mHandler.sendEmptyMessage(GET_CODE);
mHandler.sendEmptyMessageDelayed(CLEAR_CODE, 180000);
notifyUtils.notify(message.getBody(),
getBaseContext().getString(R.string.app_name),
R.drawable.ic_launcher, null, noticeId);
}