How to Send SMS via WordPress in Your Android Application

If you’re looking to send SMS messages from your WordPress site using an Android application, you’re in luck. You can easily set this up with the WP SMS plugin. In this guide, we’ll show you how to configure your WordPress site and Android device to send SMS messages using REST API and Application Passwords for authentication. Utilizing WP SMS has the added benefit of managing gateways and outgoing SMS, leveraging over 250 supported SMS gateways worldwide.

Prerequisites

  1. WordPress Site with WP SMS Plugin installed.
  2. Android Phone with an appropriate SMS gateway app installed.
  3. Application Passwords are enabled on your WordPress site for secure API access.

Step-by-Step Guide

1. Generate an Application Password in WordPress

First, you need to generate an application password for your WordPress user account. This will be used to authenticate API requests.

  1. Go to your WordPress admin dashboard.
  2. Navigate to Users → Edit User.
  3. Scroll down to the Application Passwords section.
  4. Enter a name for your application (e.g., “Android SMS Gateway”) and click Add New Application Password.
  5. Copy the generated password and store it securely.
2. Set Up REST API Endpoint

The WP SMS plugin provides a REST API endpoint for sending SMS. Here’s an example cURL request to send an SMS:

curl --user "USERNAME:APPLICATION_PASSWORD" \
  'https://yourwordpresssite.com/wp-json/wpsms/v1/send' \
  -H 'accept: application/json, text/javascript, */*; q=0.01' \
  --data-raw '{"sender":"SenderID","recipients":"numbers","message":"Hello world!","numbers":["+11111111"]}'

Replace USERNAME with your WordPress username and APPLICATION_PASSWORD with the application password you generated earlier.

3. Configure the Android Application

You will need an app on your Android device that can handle SMS sending via HTTP requests. One such app is SMS Gateway API, available on the Google Play Store.

  1. Download and install the SMS Gateway API app on your Android device.
  2. Open the app and configure it with your WordPress REST API endpoint and the required parameters.
Example Android Code

Here is a basic example of how you can configure your Android app to send an SMS via the WP SMS plugin’s REST API:

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;

public class SmsSender {
    private static final String API_URL = "https://yourwordpresssite.com/wp-json/wpsms/v1/send";
    private static final String USERNAME = "your_username";
    private static final String PASSWORD = "application_password";

    public static void main(String[] args) {
        try {
            URL url = new URL(API_URL);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            String auth = USERNAME + ":" + PASSWORD;
            String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes());
            conn.setRequestProperty("Authorization", "Basic " + encodedAuth);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json; utf-8");
            conn.setRequestProperty("Accept", "application/json");
            conn.setDoOutput(true);

            String jsonInputString = "{\"sender\":\"SenderID\",\"recipients\":\"numbers\",\"message\":\"Hello world!\",\"numbers\":[\"+11111111\"]}";

            try(OutputStream os = conn.getOutputStream()) {
                byte[] input = jsonInputString.getBytes("utf-8");
                os.write(input, 0, input.length);           
            }

            int code = conn.getResponseCode();
            System.out.println("Response Code : " + code);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Benefits of Using the WP SMS Plugin

Using the WP SMS plugin offers significant advantages:

  1. Centralized Management: Manage all your SMS gateways and outgoing messages directly from WordPress.
  2. Wide Gateway Support: WP SMS supports more than 250 global gateways, ensuring broad compatibility and flexibility.
  3. Middleware Functionality: The plugin acts as a middleware or wrapper, streamlining the process and providing additional features and capabilities not available with direct SMS gateway integration.
Conclusion

Following these steps, you can set up your WordPress site to send SMS messages using an Android application. This setup leverages the WP SMS plugin and the flexibility of application passwords for secure and seamless SMS integration. For more detailed information about the REST API endpoints provided by WP SMS, refer to their API documentation.

If you have any questions or run into issues, feel free to reach out to our support team for assistance. Happy texting!