Android Smack MessageEventListener

2020-03-25 08:12发布

I´m trying to use XMPP´s message event interface. As far as I understand you can mark a message you send with a 'delivery notification requested' flag and the recipient is than responsible to send you this notification. Has anyone succeeded in implementing this? Can someone send me some sample code? My code doesn´t work. The callbacks of my listeners (MessageEventNotificationListener, MessageEventRequestListener) are never called:

@Override
public void onCreate( Bundle savedInstanceState )
{
    super.onCreate( savedInstanceState );
    setContentView( R.layout.chat );

    PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
    VajasKifli.connection.addPacketListener( this, filter );

    tv = ( TextView ) findViewById( R.id.textView1 );
    tvState = ( TextView ) findViewById( R.id.textView2 );
    et = ( EditText ) findViewById( R.id.editText1 );
    et.addTextChangedListener( this );

    mem = new MessageEventManager( VajasKifli.connection );
    mem.addMessageEventNotificationListener( this );
    mem.addMessageEventRequestListener( this );

    sdm = new ServiceDiscoveryManager( VajasKifli.connection );
    VajasKifli.log( "sdm: " + sdm );

    stateManager = ChatStateManager.getInstance( VajasKifli.connection );

    recipient = getIntent().getStringExtra( "recipient" );
    chat = VajasKifli.connection.getChatManager().createChat( recipient, "chat-" + recipient, this );
    VajasKifli.log( "chat created: " + chat );

    VajasKifli.connection.getChatManager().addChatListener( this );

    sv = ( ScrollView ) findViewById( R.id.scrollView1 );

    handler = new ChatHandler();
}

public void onClickSend( View view )
{
    String text = et.getText().toString();
    if( text.length() > 0 )
    {
        VajasKifli.log( "sending text [" + text + "] to [" + recipient + "]" );

        try
        {
            Message message = new Message();
            message.setBody( text );
            MessageEventManager.addNotificationsRequests( message, false, true, false, false );
            chat.sendMessage( message );

            stateManager.setCurrentState( ChatState.active, chat );
            lastState = ChatState.active;
            tv.append( "\n" + VajasKifli.connection.getUser().replaceFirst( "@.*", "" ) + ": " + text );
            sv.fullScroll( ScrollView.FOCUS_DOWN );
        }
        catch( XMPPException e )
        {
            VajasKifli.logError( e.toString() );
        }

        //showToast( "sent: " + text );
    }
}

2条回答
Luminary・发光体
2楼-- · 2020-03-25 08:38

That´s all too confusing. Now I use DefaultPacketExtension and send myself the events I need. That´s far more simple, easy to understand and it works.

查看更多
贪生不怕死
3楼-- · 2020-03-25 08:58

You should get a packet-trace of the XMPP connection, either via wireshark or via the smack debug option, to assure that the delivery notifications are really send by the other end of the connection. If not, this would explain why the listeners aren't called.

Message events in SMACK are done via the now obsolete XEP-22. There is a good chance that the other side does not implement this out-dated mechanism.

查看更多
登录 后发表回答