Search code examples
androidxmppsmack

Send Message.Type.normal in smack XMPP android


I am creating a chat app using Smack XMPP. Everything is working good except the Read/Deliver reports.

This is how I am sending delivery report:

{
    Message message = new Message();
    message.setTo(chatMessage.getReceiver());
    message.setType(Message.Type.normal);
    message.setFrom(chatMessage.getSender());
    message.setStanzaId(chatMessage.getReceiver() + "_" + CommonMethods.getCurrentGMTTime(0));

    ChatMarker_File_XMPP_SERVER chatMarker_file_xmpp_server=new ChatMarker_File_XMPP_SERVER("received","urn:xmpp:chat-markers:0" , true);
    chatMarker_file_xmpp_server.setIdValue(chatMessage.getMsgid());
    message.addExtension(chatMarker_file_xmpp_server);
    Mychat = ChatManager.getInstanceFor(connection).createChat(
            message.getTo() + "@"
                    + context.getString(R.string.server),
            mMessageListener);
    try {
        if (connection.isAuthenticated()) {
            /********************************************************
             * ********    MESSAGE SEND     **********************
             ***/

           Mychat.sendMessage(message);

        } else {

            login();
        }
    } catch (SmackException.NotConnectedException e) {
        Log.e("xmpp.SendMessage()", "msg Not sent!-Not Connected!");

    } catch (Exception e) {
        Log.e("SendMessage()-Exception",
                "msg Not sent!" + e.getMessage());
    }

}

But the issue is Mychat.sendMessage(message); the SendMessage method is doing this

 /**
     * Sends a message to the other chat participant. The thread ID, recipient,
     * and message type of the message will automatically set to those of this chat.
     *
     * @param message the message to send.
     * @throws NotConnectedException 
     */
    public void sendMessage(Message message) throws NotConnectedException {
        // Force the recipient, message type, and thread ID since the user elected
        // to send the message through this chat object.
        message.setTo(participant);
        message.setType(Message.Type.chat);
        message.setThread(threadID);
        chatManager.sendMessage(this, message);
    }

Now although I am explicitly setting 'messageType' to 'normal' but the 'sendMessage()' is converting it again to Type.Chat.

I have checked the other possible methods and docs so that somehow I can send a Message with Type normal but couldn't find any thing.

Thanks


Solution

  • Why dont you use Delivery Receipt Manager ??

    While sending message :

    Message message = new Message();
    String deliveryReceiptId = DeliveryReceiptRequest.addTo(message);
    message.setType(Message.Type.chat);
    

    And you can receive delivery ackknowledgement like below..

    DeliveryReceiptManager.getInstanceFor(connection).addReceiptReceivedListener(new ReceiptReceivedListener() {
                        @Override
                        public void onReceiptReceived(Jid fromJid, Jid toJid, String receiptId, Stanza receipt) {
                            //here receiptId will be same as deliveryReceiptId u sent above which means msg is delivered to recipient 
                        }
                    });
                }
    

    and If you want message sent to server status u can check my answer here: https://stackoverflow.com/a/44254615/6350239

    and for implementing msg read status u need to implement your custom Read receipt like this https://stackoverflow.com/a/29001653/6350239