Skip to content

Android Notification Buttons

A button for Android push notifications includes the following properties:

Button title - this is the text displayed on the button.

Activity class name - an activity class in your Android application which will be used to handle the button tap event. Must be a fully qualified class name, which is a complete name of the Java class including the name of the Java package. For example, com.acmeapp.acivities.MyButtonHandlingActivity. where MyButtonHandlingActivity is the name of the activity class, while com.acmeapp.acivities is the name of the package.

Inline reply indicator - Instructs Backendless that the button should support an inline reply - a default action of handling user's input when the button is tapped.

button-template-main-screen.zoom80

Button Handling in Code

When a user taps a button in a push notification, an Android intent is sent to the activity class specified for the button. The intent contains the button title and all the information about the push notification including message body, title, subtitle and the headers. The content of a push notification comes from the push template from which a push notification is created. All listed elements of a push notification (message body, title, subtitle and the headers) can be defined on the "WHAT" section in Push Composer.  The sample code below demonstrates the API used to obtain these values:

public class ButtonHandlerActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = this.getIntent();

        // get button title
        final String buttonTitle = intent.getAction();

        // get message body
        final String messageTag = intent.getStringExtra( PublishOptions.MESSAGE_TAG );

        // get message title
        final String contentTitleTag = intent.getStringExtra( PublishOptions.ANDROID_CONTENT_TITLE_TAG );

        // get message subtext
        final String summarySubtext = intent.getStringExtra( PublishOptions.ANDROID_SUMMARY_SUBTEXT_TAG );

        // get custom header value - in this example, the name of the header is "sample_header_name"
        String headerValue = intent.getStringExtra( "sample_header_name" );
 }
}

Handling Inline Reply

Buttons in a Button Options Configuration with the selected "Inline reply" checkbox have special handling on the device. When the user taps such button, they will be prompted to enter a reply right in the push notification area. The reply is then submitted into the activity class assigned to the button. At that point it is the responsibility of the code in the activity class to process the user's response. The code below demonstrates the API for processing user's response:

public class ButtonHandlerActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = this.getIntent();

        // GET USER'S REPLY:
        // retrieve the value the user typed in
        Bundle remoteInput = RemoteInput.getResultsFromIntent(getIntent());
        String reply = (String) remoteInput.getCharSequence( PublishOptions.INLINE_REPLY );

        // PROCESS USER'S REPLY HERE
 }
}

Dismissing Notification with Code

In cases when the notification needs to be dismissed programmatically, your app can use the following code:

public class ButtonHandlerActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = this.getIntent();

        // apply your logic here


        // TO REMOVE THE NOTIFICATION:
        // get template name - it will be used later to remove the notification
        final String templateName = intent.getStringExtra(PublishOptions.TEMPLATE_NAME);

        // get internal ID of the notification
        final int notificationId = intent.getIntExtra(PublishOptions.NOTIFICATION_ID, 0);

        // get the Android notification manager
        final NotificationManagerCompat notificationManager;
        notificationManager = NotificationManagerCompat.from( getApplicationContext() );

        // cancel the notification - remove from the notification deck 
        notificationManager.cancel( templateName, notificationId );
 }
}

Button Configuration in a Template

When composing a push template, the OPTIONS section for both Android and iOS options contains a dropdown where a button configuration can be selected:

selecting-button-template