Skip to content

Basic Publish

The following example publishes a message to the default channel:

// do not forget to call Backendless.initApp in the app initialization logic

Backendless.Messaging.publish( "hello world!", new AsyncCallback<MessageStatus>()
{
  @Override
  public void handleResponse( MessageStatus response )
  {
    Log.i( "MYAPP", "message has  been published" );
  }

  @Override
  public void handleFault( BackendlessFault fault )
  {
    Log.e( "MYAPP", "server reported an error " + fault );
  }
} );

Codeless Reference

pubsub_api_basic_publish_1

where:

Argument                Description
channel name Name of the channel where a message must be published to.
subtopic name The subtopic of the message allows developers to designate and filter messages by subtopic name. For instance, if a messaging application has a group and a few subgroups, all messages sent to the main group can be marked with this subtopic name which can be used to route messages to the appropriate subgroups depending on the context.
message The contents of the message that must be published to a channel.
headers Message headers is a collection of name/value pairs. A subscriber can set a filter expressed as an SQL "where clause" query (called selector) which Backendless uses to determine whether a message should be delivered to the subscriber. When the query matches the published data in message headers, message is delivered to the corresponding subscriber. For more information see the Conditional Delivery section of this guide.
publish at Must be a Unix Timestamp, which is the number of milliseconds since the Epoch (January 1st, 1970 at UTC).  Note that if you want to specify the number of seconds instead of milliseconds, you must multiply the number by 1000 or add three trailing zeroes to your number (e.g. (1681324179 * 1000) or 1681324179000.
return result When this box is checked, the operation returns an object containing the status of the message delivery and a unique message identifier.

Returns an object containing the status of the message delivery and a unique message identifier:

// Sample object  

{  
    "errorMessage":null,  
    "messageId":"message:FCBD8BF1-A45D-4564-B449-3C91D4896987",  
    "status":"published"  
}

The example below publishes the message to the "School" channel with the subtopic name "Middle School".

pubsub_api_basic_publish_2