Sergio Sergio - 28 days ago 13
Android Question

Publishing messages with more than one String, Pubnub & Android

This is how I send a message with one String through a PubNub channel:

pubnub.publish().channel("my_channel").message("hello").async(new
PNCallback<PNPublishResult>() {
// ...
});


This is how I receive the message in the clients that are connected to the channel:

@Override
public void message(PubNub pubnub, final PNMessageResult message) {
if (message.getChannel() != null) {
final JsonNode received = message.getMessage();
String received_str = received.asText();
}
// ...
}


This works fine if I am sending one String only with the message, but what I have to do if I want to send and receive more than one?

Answer

I've found a solution.

This for sending:

pubnub.publish().channel("my_channel").message(Arrays.asList
("string a", "string b", "string c", "string d")).async(new
PNCallback<PNPublishResult>() {
    // ...
});

This for receiving:

@Override
public void message(PubNub pubnub, final PNMessageResult message) {
    if (message.getChannel() != null) {
        final JsonNode received = message.getMessage();
        String string_a = received.get(0).asText();
        String string_b = received.get(1).asText();
        String string_c = received.get(2).asText();
        String string_d = received.get(3).asText();
    }
    // ...
}
Comments