在aSmack中接收已发送消息的传递

时间:2014-06-25 17:17:06

标签: android xmpp asmack

我在聊天应用程序的最后一步,我正在尝试在发送消息时收到通知,所以我发现了这个实现:

 MessageEventRequestListener

附带4个覆盖:

 @Override
public void deliveredNotificationRequested(String from, String packetID,
    MessageEventManager messageEventManager) {
System.out.println("delievered");

}


@Override
public void displayedNotificationRequested(String from, String packetID,
    MessageEventManager messageEventManager) {
System.out.println("displaydelievered");

}


@Override
public void composingNotificationRequested(String from, String packetID,
    MessageEventManager messageEventManager) {
System.out.println("composingdelievered");

}


@Override
public void offlineNotificationRequested(String from, String packetID,
    MessageEventManager messageEventManager) {
System.out.println("offlinedelievered");

}

但是当发送消息时,什么都没发生,我想知道我错过了什么吗?这是正确的方法吗?感谢

5 个答案:

答案 0 :(得分:3)

正如@Flow所说,XEP-22:消息事件已被弃用。您可以使用DeliveryReceiptManager。

在接收方收到消息时调用侦听器

  DeliveryReceiptManager.getInstanceFor(connection).addReceiptReceivedListener(new ReceiptReceivedListener()
     {
 @Override
        public void onReceiptReceived(String arg0, String arg1, String arg2) {
            Log.i("Message Delivered  To=", arg0 + ", From =  " + arg1 + " , ID= " + arg2);
        }
    });
}

发送消息时添加以下代码

  //add delivery receipt request

    DeliveryReceiptManager.addDeliveryReceiptRequest(Message);

在PacketListener中添加以下代码

Packet received = new Message();

received.addExtension(new DeliveryReceipt(packet.getPacketID())); 

received.setTo(packet.getFrom());

connection.sendPacket(received);

答案 1 :(得分:1)

答案 2 :(得分:1)

首先将这些添加到您的配置中 -

 String[] projection = {FoodContract.FoodList._ID,FoodContract.FoodList.ITEM_NAME};

然后在XMPPTcpConnection建立后添加这些 -

DeliveryReceiptManager.setDefaultAutoReceiptMode(DeliveryReceiptManager.AutoReceiptMode.always);
ProviderManager.addExtensionProvider(DeliveryReceipt.ELEMENT, DeliveryReceipt.NAMESPACE, new DeliveryReceipt.Provider());
ProviderManager.addExtensionProvider(DeliveryReceiptRequest.ELEMENT, DeliveryReceipt.NAMESPACE, new DeliveryReceiptRequest.Provider());

这将为所有邮件启用送达回执。

DeliveryReceiptManager deliveryReceiptManager = DeliveryReceiptManager.getInstanceFor(this.connection);
deliveryReceiptManager.setAutoReceiptMode(DeliveryReceiptManager.AutoReceiptMode.always);
deliveryReceiptManager.autoAddDeliveryReceiptRequests();

对于自动接收模式,所有消息都将与收据请求扩展一起发送。并且您将收到所有消息的送货回执。

答案 3 :(得分:0)

对于您发送的每条消息,您可以发送通知请求:

   MessageEventManager.addNotificationsRequests(message,
                                true, true, true, true);

答案 4 :(得分:0)

使用此代码获取消息包。 //检查是否已送达。

if(msg.getBody().toString().equalsIgnoreCase("RecivedByReciver")){

//do what you want after get notify. 

}else{
//do what you want if not delevery report message.
Message message=new Message(ConnectionManager.parseBareAddress(msg.getFrom()),Message.Type.chat);
message.addBody(null,"RecivedByReciver");
Connection().sendPacket(message);

}