I send messages using publish/subscribe messaging, but they get duplicated

I’m trying to make a forum using backendless for Android,and at the moment of sending and receiving them , those messages are sent three and even four times in my layout however in channles and table messaging they appear once and this is right , but why does it happen when sending messages to be shown in layout?

PLease your support.

Hello Nathan

If I understood you correctly the message you are sending is displayed in Messages tab in developers console only once, right?

Also please send us your implementation of subscriptionResponder

Regards Anton

yes , in developer console the messages I send are displayed just once, this is my subscriptionResponder.

public void subscribir()
{
//SUBSCRIBE MESSAGE
AsyncCallback<List<Message>> subscriptionResponder = new AsyncCallback<List<Message>>()
{
public void handleResponse( List<Message> messages )
{
Iterator<Message> messageIterator = messages.iterator();

        while( messageIterator.hasNext() )
        {
            message = messageIterator.next();
            Map&lt;String,String&gt; headers = message.getHeaders();
            url_foto_publicador=headers.get("foto_publicador");
            id_publicador=headers.get("id_publicador");
        }
        if (!current_user.getObjectId().equals(id_publicador))
        { showToast("TODO SALIO BIEN MESSAGE SUBSCRIBING ");
            MostrarComentarios();
            //AgregarMensajeHistorial((String) message.getData(),url_foto_publicador);
        }

    }
    public void handleFault( BackendlessFault fault )
    {
        showToast("ERROR ASYNCCALLBACK METODO SUBSCRBING:"+" " + fault.getMessage());
    }
};

SubscriptionOptions subscriptionOptions=new SubscriptionOptions();
String selector="categoria='"+nombre_categoria+"' and pregunta_id='"+id_pregunta+"'";
subscriptionOptions.setSelector(selector);

Backendless.Messaging.subscribe(new_canal,subscriptionResponder,subscriptionOptions,new AsyncCallback&lt;Subscription&gt;()
{
    @Override
    public void handleResponse(Subscription response)
    {

    }

    @Override
    public void handleFault(BackendlessFault fault)
    {
        showToast("ERROR MESSAGE SUBSCRBING :"+" " + fault.getMessage());
    }
});

}

and this is my publish method

final PublishOptions publishOptions=new PublishOptions();
publishOptions.putHeader(“categoria”,nombre_categoria);
publishOptions.putHeader(“pregunta_id”,id_pregunta);

publishOptions.putHeader(“foto_publicador”, (String) current_user.getProperty(“photo”));
publishOptions.putHeader(“id_publicador”,current_user.getObjectId());

Backendless.Messaging.publish(new_canal,comentario_user,publishOptions,new AsyncCallback<MessageStatus>()
{
@Override
public void handleResponse(MessageStatus response)
{
AgregarMensajeHistorial(comentario_user, (String) current_user.getProperty(“photo”));
showToast(“SUCCESSFUL PUBLISH”);
subscribir();
}

@Override
public void handleFault(BackendlessFault fault)
{
showToast(“Error Publish:”+" " + fault.getMessage());
}
});

and this is my publish method

final PublishOptions publishOptions=new PublishOptions();
publishOptions.putHeader(“categoria”,nombre_categoria);
publishOptions.putHeader(“pregunta_id”,id_pregunta);

publishOptions.putHeader(“foto_publicador”, (String) current_user.getProperty(“photo”));
publishOptions.putHeader(“id_publicador”,current_user.getObjectId());

Backendless.Messaging.publish(new_canal,comentario_user,publishOptions,new AsyncCallback<MessageStatus>()
{
@Override
public void handleResponse(MessageStatus response)
{
AgregarMensajeHistorial(comentario_user, (String) current_user.getProperty(“photo”));
showToast(“SUCCESSFUL PUBLISH”);
subscribir();
}

@Override
public void handleFault(BackendlessFault fault)
{
showToast(“Error Publish:”+" " + fault.getMessage());
}
});

this is the other method inside publishresponder

public void MostrarComentarios()
{
//FILTRAR POR OBJECT_ID ID DE LA PREGUNTA
StringBuilder whereClause = new StringBuilder();
whereClause.append(“Pregunta_por_categoria[comentarios]”);
whereClause.append(".objectId=’").append(id_pregunta).append("’");
BackendlessDataQuery dataQuery = new BackendlessDataQuery();
dataQuery.setWhereClause(whereClause.toString());

Backendless.Data.of(Comentarios.class).findLast(new AsyncCallback&lt;Comentarios&gt;() {
    @Override
    public void handleResponse(Comentarios response)
    {
     AgregarMensajeHistorial(response.getComentario(),url_foto_publicador);
    }
    @Override
    public void handleFault(BackendlessFault fault) {

    }
});

}

and the method inside MostrarComentarios is this:
This code just adds imageview and message publish into Scrollview

public void AgregarMensajeHistorial(String mensaje, String url_foto)
{
ImageView imv=new ImageView(getApplicationContext());
Picasso.with(getApplicationContext()).load(url_foto).transform(new CircleTransform()).resize(80,80).into(imv);
imv.setScaleType(ImageView.ScaleType.FIT_START);

layout_scroll.addView(imv);

TextView txv=new TextView(getApplicationContext());
txv.setTextColor(Color.parseColor("#030303"));
txv.setBackgroundColor(Color.parseColor("#EEF8E9"));
LinearLayout.LayoutParams llp
        = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
llp.setMargins(95,0, 0, 0);
txv.setWidth(500);
txv.setPadding(10,0,0,0);
txv.setLayoutParams(llp);
txv.setText(mensaje);
txv.setBackground(getResources().getDrawable(R.drawable.rounded_textview));

layout_scroll.addView(txv);

}

in the suscrptionResponder, the code line below means that if the current user is publishing a message ,then this message won’t be shown in current user’s scrollview as “message received” because this message will be duplicated so that this message is published by the current user because the current user is also a susbcriber in some moment of the forum… I hope you understand my idea
if (!current_user.getObjectId().equals(id_publicador))
{ showToast("TODO SALIO BIEN MESSAGE SUBSCRIBING ");
MostrarComentarios();
//AgregarMensajeHistorial((String) message.getData(),url_foto_publicador);
}

Please your support

So the problem is that you do not see toasts in you app?

Do you receive any messages from server?

Hi dear Sergey, I could see Toast in my app If I want,but this is not the case, the messages I receive from the server are displayed in my app successfully , but the issue is that these messages are displayed once ,twice and tree times in my app ,however in console and channles and Table Storage these messages are dsiplayed just once and this is right, the problem is in my app

It sounds like a problem in the way you’re using the APIs, which means the problem is in your code. We would be happy to take a look at your code and assist with fixing the problem, however, it is outside of the support we provide through the forum. If you would like to get our assistance with your project, please contact Backendless sales (sales@backendless.com) to get a quote. This is something that our Services team can help with.

Regards,
Mark

Hi dear Sergey, I could see Toast in my app If I want,but this is not the case, the messages I receive from the server are displayed in my app successfully , but the issues is that these messages are displayed once ,twice and tree times in my app ,however in console and channles and Table Storage these messages are dsiplayed just once and this is rifht, the problem is in my app

Hello please check the answer from Mark http://support.backendless.com/t/i-send-messages-using-publishsubscribe-messaging-but-they-get-duplicated#comment-24221