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

Posted

技术标签:

【中文标题】如何实施延迟交付 (XEP-203) Smack【英文标题】:How to implement Delayed Delivery (XEP-203) Smack 【发布时间】:2018-03-06 19:08:13 【问题描述】:

我知道DelayInformationManager 类,并且我知道这是实现此目的的类。但是,我不知道如何使用它,如何指定jid目的地,如何实际发送等等。

谁能给我一个简短的例子来展示它的用法?

【问题讨论】:

【参考方案1】:

使用以下你可以使用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();

    

【讨论】:

以上是关于如何实施延迟交付 (XEP-203) Smack的主要内容,如果未能解决你的问题,请参考以下文章

在 smack XMPP android 中发送 Message.Type.normal

保证产品开发顺利实施的五个软件度量方法

在澳洲实施Scrum的切身感受

Firebase FCM 使用 Cloud Functions 延迟交付

DevOps正当道,但没法落地实施?

IEEP-网络实施-项目交付流程