Skip to content

Cancel Scheduled Message

Delayed delivery as well as repeated publish messages can be canceled using the API documented below:

Blocking API

public bool Backendless.Messaging.Cancel( string messageId )

Non-Blocking API

Backendless.Messaging.Publish( string messageId, AsyncCallback<bool> cancelCallback )

where:

Argument                Description
messageId ID of the message to cancel. Message ID must be obtained from the MessageStatus object obtained as the result of the Publish methods.

Return value

Argument                Description
bool -True is the scheduled message has been successfully canceled, false otherwise.

Errors

The following errors may occur during the message cancellation API call. See the Error Handling section for details on how to retrieve the error code when the server returns an error:

Error Code
Description
5040
Message has already been canceled or does not exist.

Example

Synchronous Publish and Cancellation

using BackendlessAPI;
using BackendlessAPI.Messaging;
Backendless.InitApp( appId, secretKey, version ); // where to get the values for the InitApp call

DeliveryOptions deliveryOptions = new DeliveryOptions();
deliveryOptions.PublishAt = DateTime.Now.AddSeconds( 200 );

MessageStatus status = Backendless.Messaging.Publish( "This message was scheduled 20 seconds ago", deliveryOptions );
System.Console.WriteLine( "Message scheduled. Message ID - " + status.MessageId + ". Message Status - " + status.Status );

Backendless.Messaging.Cancel( status.MessageId );

Asynchronous Publish and Cancellation

using BackendlessAPI;
using BackendlessAPI.Messaging;
Backendless.InitApp( appId, secretKey, version ); // where to get the values for the InitApp call

AsyncCallback<MessageStatus> cancelCallback = new AsyncCallback<MessageStatus>(
        result =>
        {
          System.Console.WriteLine( "Message canceled - " + result );

        },
        fault =>
        {
          System.Console.WriteLine( "Error - " + fault );
        } );

AsyncCallback<MessageStatus> publishCallback = new AsyncCallback<MessageStatus>(
        result =>
        {
          System.Console.WriteLine( "Message scheduled. Message ID - " + result.MessageId + ". Message Status - " + result.Status );
          Backendless.Messaging.Cancel( result.MessageId, cancelCallback );
        },
        fault =>
        {
          System.Console.WriteLine( "Error - " + fault );
        } );

DeliveryOptions deliveryOptions = new DeliveryOptions();
deliveryOptions.PublishAt = DateTime.Now.AddSeconds( 200 );

Backendless.Messaging.Publish( "This message was scheduled 20 seconds ago", deliveryOptions, publishCallback );