Android Smack 消息事件监听器

发布于 2024-12-02 02:38:06 字数 2272 浏览 1 评论 0原文

我正在尝试使用 XMPP 的消息事件接口。据我了解,您可以在发送的消息上标记“请求送达通知”标志,然后收件人负责向您发送此通知。有人成功实施过这个吗?有人可以给我发送一些示例代码吗?我的代码不起作用。我的侦听器(MessageEventNotificationListener、MessageEventRequestListener)的回调永远不会被调用:

@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 );
    }
}

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 );
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧城烟雨 2024-12-09 02:38:06

您应该通过wireshark 或通过smack 调试选项获取XMPP 连接的数据包跟踪,以确保传送通知确实是由连接的另一端发送的。如果没有,这就能解释为什么没有调用听众。

SMACK 中的消息事件是通过现已过时的 XEP-22 完成的。对方很可能没有实施这种过时的机制。

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.

他夏了夏天 2024-12-09 02:38:06

这太令人困惑了。现在我使用 DefaultPacketExtension 并向自己发送我需要的事件。这更加简单、容易理解并且有效。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文