0

这对我来说很奇怪:在我的项目中(顺便说一下,它是从 Yaxim 派生的,所以它是一个 android 的聊天应用程序),我正在尝试通过 messageeventlistener 实现写入通知。我确实收到了通知事件。为了让它出现在列表视图中(在另一个类中),我在数据库中添加了一行,就好像它是一条消息(只有文本是“...”)。我添加了一个计数器,并且该行已正确插入到数据库中(随后,当实际消息到来时,该行从数据库中删除)。notifychanges 也被正确调用。奇怪的是,尽管如此,该行并没有出现在列表视图中。如果我将程序移到下面几行,在常规消息的数据包侦听器上,它会被插入。但是我不能那样离开它,因为数据包侦听器没有 t 正确接收所有撰写通知,因此它不会一直为此目的工作。此外,当写入通知正确插入数据库时​​,如果我将程序保留在 messageEventNotificationListener() 中,则写入通知消息在实际消息到达时不会被删除(并且 iCount 不断增加);如果我将程序放入 packetListener 中(因此在消息被推送到数据库后 iCount 等于 0)

这是消息事件监听器:

private void messageEventNotificationListener(){
    // Create a MessageEventManager
    if (mMessageEventManager == null)
        mMessageEventManager = new MessageEventManager(mXMPPConnection);
      // Add the listener that will react to the event notifications
    mMessageEventManager.addMessageEventNotificationListener(new MessageEventNotificationListener() {
          public void deliveredNotification(String from, String packetID) {
              Log.d(TAG, "The message has been delivered (" + from + ", " + packetID + ")");
          }

          public void displayedNotification(String from, String packetID) {
              Log.d(TAG, "The message has been displayed (" + from + ", " + packetID + ")");
          }

          public void composingNotification(String from, String packetID) {
              Log.d(TAG, "The message's receiver is composing a reply (" + from + ", " + packetID + ")");
              //controlla se l'utente non sta già scrivendo 
              String selection = ChatConstants.JID + " = '" + from + "' AND " +
                        ChatConstants.DELIVERY_STATUS + " = " + ChatConstants.DS_WRITING;

                Cursor cursor = mContentResolver.query(ChatProvider.CONTENT_URI,
                            new String[] { "count(" + ChatConstants._ID + ")" },
                            selection, null, null);
                cursor.moveToFirst();
                int iCount = cursor.getInt(0);
                //if (iCount == 0)
                //{
                    addChatMessageToDB(ChatConstants.INCOMING, from, "...", ChatConstants.DS_WRITING, System.currentTimeMillis(), packetID);

                //}

          }

          public void offlineNotification(String from, String packetID) {
              Log.d(TAG, "The message's receiver is offline (" + from + ", " + packetID + ")");
          }

          public void cancelledNotification(String from, String packetID) {
              Log.d(TAG, "The message's receiver cancelled composing a reply (" + from + ", " + packetID + ")");


          }
      });   

}

这是数据包侦听器:

private void registerMessageListener() {
    // do not register multiple packet listeners
    if (mPacketListener != null)
        mXMPPConnection.removePacketListener(mPacketListener);

    PacketTypeFilter filter = new PacketTypeFilter(Message.class);

    mPacketListener = new PacketListener() {
        public void processPacket(Packet packet) {
            try {
            if (packet instanceof Message) {
                Message msg = (Message) packet;
                String chatMessage = msg.getBody();

                DeliveryReceipt dr = (DeliveryReceipt)msg.getExtension("received", DeliveryReceipt.NAMESPACE);
                if (dr != null) {
                    Log.d(TAG, "got delivery receipt for " + dr.getId());
                    changeMessageDeliveryStatus(dr.getId(), ChatConstants.DS_DELIVERED);
                }


                if (chatMessage == null)
                    return;

                if (msg.getType() == Message.Type.error) {
                    chatMessage = "<Error> " + chatMessage;
                }

                long ts;
                DelayInfo timestamp = (DelayInfo)msg.getExtension("delay", "urn:xmpp:delay");
                if (timestamp == null)
                    timestamp = (DelayInfo)msg.getExtension("x", "jabber:x:delay");
                if (timestamp != null)
                    ts = timestamp.getStamp().getTime();
                else
                    ts = System.currentTimeMillis();

                String fromJID = getJabberID(msg.getFrom());

                //elimina il messaggio "writing" se esiste
                     deleteWritingChatMessageFromDB(fromJID);

                    if (msg.getExtension("request", DeliveryReceipt.NAMESPACE) != null) {
                        // got XEP-0184 request, send receipt
                        sendReceipt(msg.getFrom(), msg.getPacketID());
                    }

                    addChatMessageToDB(ChatConstants.INCOMING, fromJID, chatMessage, ChatConstants.DS_NEW, ts, msg.getPacketID());
                    mServiceCallBack.newMessage(fromJID, chatMessage);
                }
            }
            } catch (Exception e) {
                // SMACK silently discards exceptions dropped from processPacket :(
                Log.e(TAG, "failed to process packet:");
                e.printStackTrace();
            }

    };

    mXMPPConnection.addPacketListener(mPacketListener, filter);
}

最后是插入和删除方法

private void addChatMessageToDB(int direction, String JID,
        String message, int delivery_status, long ts, String packetID) {
    ContentValues values = new ContentValues();

    values.put(ChatConstants.DIRECTION, direction);
    values.put(ChatConstants.JID, JID);
    values.put(ChatConstants.MESSAGE, message);
    values.put(ChatConstants.DELIVERY_STATUS, delivery_status);
    values.put(ChatConstants.DATE, ts);
    values.put(ChatConstants.PACKET_ID, packetID);

    Uri noteUri = mContentResolver.insert(ChatProvider.CONTENT_URI, values);
    //mContentResolver.notifyChange(noteUri, null);
}

private void deleteWritingChatMessageFromDB(String JID) {
    int count = mContentResolver.delete(ChatProvider.CONTENT_URI,
            ChatConstants.JID + " = ? AND " + ChatConstants.MESSAGE + " = ? AND " + ChatConstants.DELIVERY_STATUS + " = ?", new String[] { JID, "...", "3" });
    debugLog("deleteWritingChatMessageEntryFromDB: Deleted " + count + " entries");

}

这两个方法都被调用到 ChatProvider 类中并以 getContext().getContentResolver().notifyChange(url, null);

4

1 回答 1

0

原来问题是不同的:我发布这个只是为了以防其他人遇到同样的问题。消息实际上已插入到数据库中,但是虽然 packetlistener 仅返回 jid,但 messageeventlistener 也返回带有资源的 jid,在插入之前需要将其删除。完成后,消息出现在列表中。另外:我发布的代码对于处理 messageEvent 并不完全正确。侦听器需要直接从 packetListener 内部调用。

于 2012-09-13T17:51:29.117 回答