How to send multiple sms from Kotlin
The simplest way to send SMS from Kotlin is to use the built in HTTP/Rest SMS api of Ozeki SMS Gateway. When you use this API, you will send SMS messages by issuing a HTTP Post request to the SMS gateway. The HTTP Post request will contain a message formatted in json format. The SMS gateway will send this SMS to the recipient phone, and it will return a HTTP 200 OK response to your request. (Figure 1)
Kotlin code to send multiple sms to mobile
The Kotlin sms code sample below demonstrates how you can send multiple SMS using the http rest sms api of Ozeki SMS Gateway using the Kotlin Ozeki.Libs.Rest library. This library is provided to you free of charge, and you may use it and modify it in any of your projects.
MainActivity.kt
package send.multiple.sms import Ozeki.Libs.Rest.Configuration import Ozeki.Libs.Rest.Message import Ozeki.Libs.Rest.MessageApi import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.ArrayAdapter import androidx.appcompat.app.AppCompatActivity import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val inputToAddress:android.widget.EditText = findViewById(R.id.inputToAddress) val inputMessage:android.widget.EditText = findViewById(R.id.inputMessage) val btnAddMessage:android.widget.Button = findViewById(R.id.btnAddMessage) val btnSendRequest:android.widget.Button = findViewById(R.id.btnSendRequest) val listOfMessages:android.widget.ListView = findViewById(R.id.listView) val logBox:android.widget.TextView = findViewById(R.id.logBox) logBox.movementMethod = ScrollingMovementMethod() val configuration = Configuration( username = "http_user", password = "qwe123", apiurl = "http://10.0.2.2:9509/api" ) val api = MessageApi(configuration) val messages : ArrayList<Message> = arrayListOf() val messages_label : ArrayList<String> = arrayListOf() val arrayAdapter = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, messages_label) listOfMessages.adapter = arrayAdapter btnAddMessage.setOnClickListener { if (inputToAddress.text.toString() != "" && inputMessage.text.toString() != "" ) { val msg = Message() msg.ToAddress = inputToAddress.text.toString() msg.Text = inputMessage.text.toString() inputToAddress.text.clear() inputMessage.text.clear() messages_label.add(msg.toString()) arrayAdapter.notifyDataSetChanged() messages.add(msg) } else { logBox.text = String.format("%s\nYou have to fill all the fields!", logBox.text) } } btnSendRequest.setOnClickListener { messages_label.clear() arrayAdapter.notifyDataSetChanged() GlobalScope.launch(Dispatchers.IO) { val response = api.Send(messages) messages.clear() logBox.text = String.format("%s\n%s", logBox.text, response.toString()) } } } }
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/textToAddress" android:layout_width="320dp" android:layout_height="wrap_content" android:layout_marginTop="40dp" android:text="To address:" android:textSize="20sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.498" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textMsg" android:layout_width="320dp" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="Message:" android:textSize="20sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.505" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/inputToAddress" /> <EditText android:id="@+id/inputToAddress" android:layout_width="320dp" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:ems="10" android:inputType="textPersonName" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.505" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textToAddress" /> <EditText android:id="@+id/inputMessage" android:layout_width="320dp" android:layout_height="80dp" android:layout_marginTop="20dp" android:ems="10" android:gravity="start|top" android:inputType="textMultiLine" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.494" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textMsg" /> <Button android:id="@+id/btnSendRequest" android:layout_width="320dp" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="Send" android:backgroundTint="#FF3F3F" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/listView" /> <Button android:id="@+id/btnAddMessage" android:layout_width="320dp" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:backgroundTint="#FF3F3F" android:text="Add" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.505" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/inputMessage" /> <ListView android:id="@+id/listView" android:layout_width="320dp" android:layout_height="120dp" android:layout_marginTop="20dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.494" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/btnAddMessage" /> <TextView android:id="@+id/logBox" android:layout_width="320dp" android:layout_height="80dp" android:layout_marginTop="20dp" android:nestedScrollingEnabled="false" android:scrollbars="vertical" android:text="Logs:" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.494" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/btnSendRequest" /> </androidx.constraintlayout.widget.ConstraintLayout>
How to use the Kotlin sms example:
You can use the Message class to create the SMS and you can use the MessageApi class to send the SMS messages to the SMS gateway. The SMS gateway will forward your message to the mobile network either through a wireless connection or through the Internet.
Download SendMultipleSms.kt
The source code explained in this article can be downloaded and used and modified free of charge.
Download: SendMultipleSms.kt.zip (150Kb)
What is in the SendMultipleSms.kt.zip file?
The SendMultipleSms.kt.zip file contains the an example project, which has the Ozeki.Libs.Rest library in it. With this library you can send, delete, mark and receive sms messages by creating a MessageApi and using the Send(), Delete(), Mark(), and Receive() methods. (Figure 2)
How to send multiple SMS from Kotlin (Quick steps)
To send multiple sms from Kotlin:
- Install Ozeki SMS Gateway
- Connect Ozeki SMS Gateway to the mobile network
- Send a test sms from Ozeki GUI
- Create a HTTP sms api user
- Android Studio
- Download the example project above
- Create the SMS by creating a new Message object
- Create an api to send your messages
- Use the Send method to send your messages
- Read the response message on the console
- Check the logs in the SMS gateway
Install Ozeki SMS Gateway and create an HTTP API user
To be able to send SMS from Kotlin, first you need to install Ozeki SMS Gateway. The SMS gateway can be installed on the same computer, where you develop your Kotlin code in Android Studio. After installation, the next step is to connect Ozeki SMS Gateway to the mobile network. You can send a test sms from the Ozeki GUI to verify, that your mobile network connection works. The final step to prepare your environment is to create a HTTP sms api user. Create a user with a username of "http_user", and with a password of "qwe123" to make the example work without modification.
After the environment is setup, you can run your Kotlin code.
HTTP API url to use send sms from Kotlin
To send multiple SMS from Kotlin, your Kotlin will have to issue an HTTP request to the SMS gateway. The API url is shown below. Note that the IP address (127.0.0.1) should be replaced to the IP address of your SMS gateway. If Ozeki SMS Gateway is installed on the same computer where the JavaScript sms application is running, this can be 127.0.0.1. If it is installed on a different computer, it should be the IP address of that computer.
http://127.0.0.1:9509/api?action=rest
HTTP authentication to use send multiple sms from Kotlin
To authenticate the Kotlin sms client, you need to send the username and password in a base64 encoded string to the server in a HTTP request. The format used is: base64(username+":"+password). In Kotlin you can use the following code to do this encoding:
var usernamePassword = "%s:%s".format(username, password) return "Basic %s".format(Base64.getEncoder().encodeToString(usernamePassword.toByteArray()))
For example, if you encode the username 'http_user' and the password 'qwe123', you will get the following base64 encoded string: aHR0cF91c2VyOnF3ZTEyMw==. To send
HTTP request header to send SMS from Kotlin
To send the SMS messages, you need to include the following lines as headers in the HTTP request. Note that we include a content type and an Authorization header.
Content-Type: application/json Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
HTTP request to send SMS from Kotlin
To submit the SMS, your Kotlin application will send an HTTP request similar to the one below. Note, that this request contains a HTTP header part and a http body part. The HTTP body is a JSON encoded data string. It contains the recipient's number and the message's text.
POST /api?action=sendmsg HTTP/1.1 Connection: Keep-Alive Content-Length: 944 Content-Type: application/json; charset=utf-8 Accept-Encoding: gzip Authorization: Basic aHR0cF91c2VyOnF3ZTEyMw== Host: 10.0.2.2:9509 User-Agent: okhttp/4.2.2 { "messages": [ { "message_id": "50fae4db-be52-4a79-905b-d51d1c83351b", "to_address": "+36201111111", "text": "Hello world 3", "create_date": "2021-06-17T13:59:40", "valid_until": "2021-06-24T13:59:40", "time_to_send": "-999999999-01-01T00:00", "submit_report_requested": true, "delivery_report_requested": true, "view_report_requested": true }, { "message_id": "6be6e16f-783e-4a88-ba7c-5f37553ee430", "to_address": "+36202222222", "text": "Hello world 2", "create_date": "2021-06-17T13:59:58", "valid_until": "2021-06-24T13:59:58", "time_to_send": "-999999999-01-01T00:00", "submit_report_requested": true, "delivery_report_requested": true, "view_report_requested": true }, { "message_id": "128495ae-7175-4219-bed5-5da3161e7c1a", "to_address": "+36203333333", "text": "Hello world 1", "create_date": "2021-06-17T14:00:15", "valid_until": "2021-06-24T14:00:15", "time_to_send": "-999999999-01-01T00:00", "submit_report_requested": true, "delivery_report_requested": true, "view_report_requested": true } ] }
HTTP response received by the Kotlin sms example
Once the SMS gateway receives this request, it will generate a HTTP response. The HTTP response will contain a status code, to indicate whether the SMS submit request was successful or not. It will also return a JSON encoded structure to provide you useful details about the message's submission.
HTTP/1.1 200 OK User-Agent: OZEKI 10.3.120 (www.myozeki.com) Content-Type: application/json; charset=utf8 Last-Modified: Thu, 17 Jun 2021 13:48:15 GMT Server: 10/10.3.120 Transfer-Encoding: chunked { "http_code": 200, "response_code": "SUCCESS", "response_msg": "Messages queued for delivery.", "data": { "total_count": 3, "success_count": 3, "failed_count": 0, "messages": [ { "message_id": "50fae4db-be52-4a79-905b-d51d1c83351b", "from_station": "%", "to_address": "+36201111111", "to_station": "%", "text": "Hello world 3", "create_date": "2021-06-17 13:59:40", "valid_until": "2021-06-24 13:59:40", "time_to_send": "2021-06-17 13:59:40", "submit_report_requested": true, "delivery_report_requested": true, "view_report_requested": false, "tags": [ { "name": "Type", "value": "SMS:TEXT" } ], "status": "SUCCESS" }, { "message_id": "6be6e16f-783e-4a88-ba7c-5f37553ee430", "from_station": "%", "to_address": "+36202222222", "to_station": "%", "text": "Hello world 2", "create_date": "2021-06-17 13:59:58", "valid_until": "2021-06-24 13:59:58", "time_to_send": "2021-06-17 13:59:58", "submit_report_requested": true, "delivery_report_requested": true, "view_report_requested": false, "tags": [ { "name": "Type", "value": "SMS:TEXT" } ], "status": "SUCCESS" }, { "message_id": "128495ae-7175-4219-bed5-5da3161e7c1a", "from_station": "%", "to_address": "+36203333333", "to_station": "%", "text": "Hello world 1", "create_date": "2021-06-17 14:00:15", "valid_until": "2021-06-24 14:00:15", "time_to_send": "2021-06-17 14:00:15", "submit_report_requested": true, "delivery_report_requested": true, "view_report_requested": false, "tags": [ { "name": "Type", "value": "SMS:TEXT" } ], "status": "SUCCESS" } ] } }
How to send multiple SMS from Kotlin using the sms api and the example project above (Video tutorial)
This video shows you how to download and use the SendMultipleSms.kt project. Once you opened the example project, you might notice that a there is a package called Ozeki.Libs.Rest This is the package that contains the MessageApi and all the stuffs you need to send multiple SMS using Kotlin.
How to check that the SMS has been accepted by the HTTP user
After the SMS has been submitted, it is a good idea to check your SMS gateway, to see what it has received. You can check the log by opening the HTTP user's details from the Ozeki SMS Gateway management console. At the end of the video above you can see how to check if the request has been received by the http_user. (Figure 3)
How using the app on a virtual phone look like
As you can see on Figure 4, you can send multiple SMS messages at the same time, using the app. After you have sent the messages, you can see a log after the text of the message. There, you can get more information about the sent messages. It will notify you that out of all the messages sent, how many was sent successfully and how many failed. (Figure 4)
How to add Ozeki.Libs.Rest to your own project
The Ozeki.Libs.Rest library can be downloaded and used and modified free of charge.
Download: Ozeki.Libs.Rest.kt.zip (7.66Kb)
If you decide to create your application on your own only with the Ozeki.Libs.Rest
library, there are few things you should change in your base application.
In order to use the Ozeki.Libs.Rest library you have to put it into the java folder
of the main directory
In the following video I will show you how to download and add the Ozeki.Libs.Rest
library to your own project.
Dependencies
It is important to mention, that the Ozeki.Libs.Rest library has some dependencies. In order to use it you have to add these dependencies into the Gradle Scripts.
implementation "com.squareup.okhttp3:okhttp:4.2.2" implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0'
Adding dependencies (Video tutorial)
In the following video, you will learn how to add the previously mentioned dependencies to your project. It will start with copying the code and will take you all the way to the working dependencies. The video is only 53 seconds long, but it features all the needed steps to complete the process. You can follow this tutorial without effort
Internet access
In order to make it possible for your application to send
an HTTP request, you have to enable your application
to connect to the internet.
In the following video I'll show you how to enable internet access
for your Kotlin application.
<uses-permission android:name="android.permission.INTERNET" />
android:usesCleartextTraffic="true"
Adding internet connection to the project (Video tutorial)
You need to add both of these lines to the AndroidManifest.xml file. In the following video I'll show you where you should put the codes above. You need to add both of these lines to the AndroidManifest.xml file. The video will start with copying the code and will take you to a working internet connection. This video is very easy to follow but it features all the steps you need to take in a detailed way.
To sum it up
This article helps you to set up an HTTP SMS API in the Ozeki SMS Gateway and to use the given Kotin code for sending multiple SMS messages. This solution is perfect if you need to forward an important message to multiple clients as quickly as possible. It is beneficial to use this solution on your Kotlin app in order to have a high-performance information-sharing system.
More information can be found about this topic on the Ozeki webpage. Continue reading with the article titled How to schedule an SMS in Kotlin.
Now download the Ozeki SMS Gateway and start to develop your business!
More information
- Kotlin send SMS with the HTTP rest API (code sample)
- Kotlin send multiple SMS with the HTTP rest API (code sample)
- Kotlin schedule SMS with the HTTP rest API (code sample)
- Kotlin receive SMS with the HTTP rest API (code sample)
- Kotlin delete SMS with the HTTP rest API (code sample)
- Github: Kotlin SMS API