我使用2.1.1 Android SDK并希望通知用户应用程序收到的新传入消息。
这是问题所在。我想在启动应用程序时通知用户,因此我想在“用户登录”阶段添加监听器,如下所示:
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才能发起。但是我想听听所有的消息,不仅仅是在特定的聊天中。
长话短说,如何实现一个消息监听器来捕获发送给登录用户的所有消息?
答案 0 :(得分:2)
@Naveen Kumar
在我的开始活动中,我启动侦听器以捕获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触发的连接,然后将数据包侦听器附加到它。