如何实现延迟交付(XEP-203)Smack

时间:2018-03-06 19:08:13

标签: android xmpp smack

我知道DelayInformationManager类,我知道这是实现这一目标的类。但是,我不知道如何使用它,如何指定Jid目标,如何实际发送它等等。

有人能为我提供一个证明其用法的简短例子吗?

1 个答案:

答案 0 :(得分:0)

使用以下命令可以使用smack lib实现Delayed Delivery

发送延迟交货收据

 public void sendReceipt(String toJID, String sender, final String stanzaID, final String id, final long millis, Message.Type msgType) {

    if(isConnected()){

        Message ack = null; //2017-11-17T15:21:50.063+00:00
        try {              

            String fromJidGroup = toJID;
            if(msgType == Message.Type.groupchat){
                fromJidGroup = ActivityHelper.createJid(sender) ;
            }else{
                fromJidGroup = toJID;
            }

           ack = new Message(JidCreate.from(fromJidGroup), Message.Type.chat); //msgType
           ack.addExtension(new DeliveryReceipt(id));


        } catch (XmppStringprepException e) {
            e.printStackTrace();
        }
        if(millis!=0) {
            DelayInformation delay = new DelayInformation(new Date(millis));
            ack.addExtension(delay);
        }
        if(stanzaID!=null){
            ack.setStanzaId(stanzaID);
        }
        try {

            if(connection.isSmEnabled() && connection!=null) {

                    //addStanzaIdAcknowledgedListener send successfully Receipt or not in server
                    connection.addStanzaIdAcknowledgedListener(ack.getStanzaId(), new StanzaListener() {
                        @Override
                        public void processPacket(Stanza stanza) throws SmackException.NotConnectedException, InterruptedException {

                            if(registerXmppListener!=null){
                                registerXmppListener.onStanzaIdAcknowledgedReceived(stanza);
                            }
                        }

                    });

            }

            connection.sendStanza(ack);

        } catch (SmackException.NotConnectedException e) {
            e.printStackTrace();
        } catch (InterruptedException | StreamManagementException.StreamManagementNotEnabledException e) {
            e.printStackTrace();
        }

    }

}

接收Delayed Delivery,首先使用xmpp连接注册ReceiptReceivedListener

 private ReceiptReceivedListener mReceiptReceivedListener;

  mReceiptReceivedListener = new ReceiptReceivedListener() {
        @Override
        public void onReceiptReceived(Jid from, Jid to, String rec_id, Stanza stanza) {
            if(registerXmppListener!=null) {

                Log.d("onReceipt","onReceipt stanza : " + stanza.toXML());
                registerXmppListener.onDeliveryStatusReceived(from.toString(),to.toString(),rec_id,stanza);
            }
        }
    };
    mDeliveryReceiptManager.addReceiptReceivedListener(mReceiptReceivedListener);

onDeliveryStatusReceived听众

public void changeMessageDeliveryStatus(String from, String to, String rec_id, Stanza stanza){
    if(stanza instanceof Message) {
        Message msg = (Message) stanza;

        String jid = "";
        if(msg.getType().equals(Message.Type.chat)){
            jid = ActivityHelper.getBareJID(from);

        }else if(msg.getType().equals(Message.Type.groupchat)){
            jid = ActivityHelper.getSenderFromGroup(from);
        }

        String sender="";
        long date = System.currentTimeMillis();

        String stanza_id=stanza.getStanzaId();
        int chat_type = 0;
        int isPrivate = ChatConstants.ISPRIVATE_NO;

        DelayInformation timestamp = (DelayInformation)msg.getExtension("delay", "urn:xmpp:delay");
        if (timestamp == null)
            timestamp = (DelayInformation)msg.getExtension("x", "jabber:x:delay");
        if (timestamp != null)
            date = timestamp.getStamp().getTime();

    }
}