QuickBlox 聊天消息侦听器
QuickBlox chat messages listener
我使用 2.1.1 Android SDK 并希望通知用户有关应用程序收到的新传入消息。
问题来了。我想在用户启动应用程序时通知用户,因此我想在 "user login" 阶段添加监听器,如代码段所示:
QBAuth.createSession(user, new QBEntityCallbackImpl<QBSession>() {
@Override
public void onSuccess(QBSession session, Bundle args) {
// login to Chat
chatService.login(user, new QBEntityCallbackImpl() {
@Override
public void onSuccess() {
HERE I WOULD LIKE A CODE TO START LISTEN FOR ALL INCOMING MESSAGES
根据http://sdk.quickblox.com/android/com/quickblox/chat/listeners/QBMessageListenerImpl.html
侦听器需要聊天 QBChat 才能启动。但我想收听所有消息,而不仅仅是在特定聊天中。
长话短说,如何实现消息侦听器以捕获所有发送给已登录用户的消息?
@Naveen Kumar
一开始 activity 我启动侦听器来捕获 XMPP 连接。
private void XMPPConnectionListener() {
XMPPConnection.addConnectionCreationListener(new ConnectionCreationListener() {
@Override
public void connectionCreated(XMPPConnection connection) {
GlobalVar.XMPPConn = connection;
ChatHelper.idleMessageListener(StartActivity.this);
}
});
}
然后我用GlobalVar.XMPPConn抓包解析:
public static void idleMessageListener(final Activity activity) {
PacketListener myListener = new PacketListener() {
public void processPacket(final Packet packet) {
final Integer userID = Integer.parseInt(returnIDFromPacket(packet.getFrom()));
final String body = returnBodyFromPacket(packet.toString());
if (!GlobalVar.currentOpponent.equals(userID) && !body.isEmpty()) {
activity.runOnUiThread(new Runnable() {
public void run() {
QBUsers.getUser(userID, new QBEntityCallbackImpl<QBUser>() {
@Override
public void onSuccess(final QBUser user, Bundle args) {
sendNotification(activity, user, body);
}
@Override
public void onError(List<String> errors) {
Log.d(TAG, errors.toString());
}
});
}
});
}
}
};
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
if (GlobalVar.XMPPConn != null) {
GlobalVar.XMPPConn.addPacketListener(myListener, filter);
}
}
所以逻辑是捕获由 QuickBlox 触发的连接,然后将数据包侦听器附加到它。
我使用 2.1.1 Android SDK 并希望通知用户有关应用程序收到的新传入消息。
问题来了。我想在用户启动应用程序时通知用户,因此我想在 "user login" 阶段添加监听器,如代码段所示:
QBAuth.createSession(user, new QBEntityCallbackImpl<QBSession>() {
@Override
public void onSuccess(QBSession session, Bundle args) {
// login to Chat
chatService.login(user, new QBEntityCallbackImpl() {
@Override
public void onSuccess() {
HERE I WOULD LIKE A CODE TO START LISTEN FOR ALL INCOMING MESSAGES
根据http://sdk.quickblox.com/android/com/quickblox/chat/listeners/QBMessageListenerImpl.html 侦听器需要聊天 QBChat 才能启动。但我想收听所有消息,而不仅仅是在特定聊天中。
长话短说,如何实现消息侦听器以捕获所有发送给已登录用户的消息?
@Naveen Kumar
一开始 activity 我启动侦听器来捕获 XMPP 连接。
private void XMPPConnectionListener() {
XMPPConnection.addConnectionCreationListener(new ConnectionCreationListener() {
@Override
public void connectionCreated(XMPPConnection connection) {
GlobalVar.XMPPConn = connection;
ChatHelper.idleMessageListener(StartActivity.this);
}
});
}
然后我用GlobalVar.XMPPConn抓包解析:
public static void idleMessageListener(final Activity activity) {
PacketListener myListener = new PacketListener() {
public void processPacket(final Packet packet) {
final Integer userID = Integer.parseInt(returnIDFromPacket(packet.getFrom()));
final String body = returnBodyFromPacket(packet.toString());
if (!GlobalVar.currentOpponent.equals(userID) && !body.isEmpty()) {
activity.runOnUiThread(new Runnable() {
public void run() {
QBUsers.getUser(userID, new QBEntityCallbackImpl<QBUser>() {
@Override
public void onSuccess(final QBUser user, Bundle args) {
sendNotification(activity, user, body);
}
@Override
public void onError(List<String> errors) {
Log.d(TAG, errors.toString());
}
});
}
});
}
}
};
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
if (GlobalVar.XMPPConn != null) {
GlobalVar.XMPPConn.addPacketListener(myListener, filter);
}
}
所以逻辑是捕获由 QuickBlox 触发的连接,然后将数据包侦听器附加到它。