Send and receive SMS messages using your Android phone programmatically via a simple HTTP API
Add userID to debug logs
Acho Arnold2b50f4f0f1a8
diff --git a/android/app/src/main/java/com/httpsms/FirebaseMessagingService.kt b/android/app/src/main/java/com/httpsms/FirebaseMessagingService.ktindex dcfa1182..e6521349 100644--- a/android/app/src/main/java/com/httpsms/FirebaseMessagingService.kt+++ b/android/app/src/main/java/com/httpsms/FirebaseMessagingService.kt@@ -89,7 +89,10 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {if (Settings.isLoggedIn(this)) {Timber.d("updating SIM1 phone with new fcm token")- HttpSmsApiService.create(this).updatePhone(Settings.getSIM1PhoneNumber(this), token, Constants.SIM1)+ val phone = HttpSmsApiService.create(this).updatePhone(Settings.getSIM1PhoneNumber(this), token, Constants.SIM1)+ if (phone != null) {+ Settings.setUserID(this, phone.userID)+ }}if(Settings.isDualSIM(this)) {@@ -106,7 +109,7 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {if (BuildConfig.DEBUG) {Timber.plant(Timber.DebugTree())- Timber.plant(LogtailTree())+ Timber.plant(LogtailTree(this.applicationContext))}}diff --git a/android/app/src/main/java/com/httpsms/HttpSmsApiService.kt b/android/app/src/main/java/com/httpsms/HttpSmsApiService.ktindex db5166e0..babd8081 100644--- a/android/app/src/main/java/com/httpsms/HttpSmsApiService.kt+++ b/android/app/src/main/java/com/httpsms/HttpSmsApiService.kt@@ -162,7 +162,7 @@ class HttpSmsApiService(private val apiKey: String, private val baseURL: URI) {}- fun updatePhone(phoneNumber: String, fcmToken: String, sim: String): Boolean {+ fun updatePhone(phoneNumber: String, fcmToken: String, sim: String): Phone? {val body = """{"fcm_token": "$fcmToken",@@ -181,12 +181,13 @@ class HttpSmsApiService(private val apiKey: String, private val baseURL: URI) {val response = client.newCall(request).execute()if (!response.isSuccessful) {Timber.e("error response [${response.body?.string()}] with code [${response.code}] while sending fcm token [${body}]")- return false+ return null}+ val payload = ResponsePhone.fromJson(response.body!!.string())?.dataresponse.close()Timber.i("fcm token sent successfully for phone [$phoneNumber]" )- return true+ return payload}diff --git a/android/app/src/main/java/com/httpsms/LogtailTree.kt b/android/app/src/main/java/com/httpsms/LogtailTree.ktindex 703eef19..9458767a 100644--- a/android/app/src/main/java/com/httpsms/LogtailTree.kt+++ b/android/app/src/main/java/com/httpsms/LogtailTree.kt@@ -1,5 +1,6 @@package com.httpsms+import android.content.Contextimport android.os.Buildimport com.beust.klaxon.Klaxonimport okhttp3.MediaType.Companion.toMediaType@@ -12,7 +13,7 @@ import java.time.ZonedDateTimeimport java.time.format.DateTimeFormatterimport java.util.concurrent.ConcurrentLinkedQueue-class LogtailTree: Timber.DebugTree() {+class LogtailTree(val context: Context): Timber.DebugTree() {private val client = OkHttpClient()private val jsonMediaType = "application/json; charset=utf-8".toMediaType()private val queue: ConcurrentLinkedQueue<LogEntry> = ConcurrentLinkedQueue<LogEntry>()@@ -30,6 +31,7 @@ class LogtailTree: Timber.DebugTree() {Build.DEVICE,Build.VERSION.SDK_INT,ZonedDateTime.now(ZoneOffset.UTC).format(formatter),+ Settings.getUserID(context),t)queue.add(logEntry)@@ -76,5 +78,6 @@ class LogtailTree: Timber.DebugTree() {val device: String,val version: Int,val dt: String,+ val userID: String,val throwable: Throwable?)}diff --git a/android/app/src/main/java/com/httpsms/MainActivity.kt b/android/app/src/main/java/com/httpsms/MainActivity.ktindex 042c3de7..de29d645 100644--- a/android/app/src/main/java/com/httpsms/MainActivity.kt+++ b/android/app/src/main/java/com/httpsms/MainActivity.kt@@ -198,8 +198,9 @@ class MainActivity : AppCompatActivity() {private fun sendFCMToken(timestamp: Long, context:Context, phoneNumber: String, sim: String) {Thread {- val updated = HttpSmsApiService.create(context).updatePhone(phoneNumber, Settings.getFcmToken(context) ?: "", sim)- if (updated) {+ val phone = HttpSmsApiService.create(context).updatePhone(phoneNumber, Settings.getFcmToken(context) ?: "", sim)+ if (phone != null) {+ Settings.setUserID(context, phone.userID)Settings.setFcmTokenLastUpdateTimestampAsync(context, timestamp)Timber.i("[${sim}] FCM token uploaded successfully")return@Thread@@ -217,7 +218,7 @@ class MainActivity : AppCompatActivity() {if (BuildConfig.DEBUG) {Timber.plant(Timber.DebugTree())- Timber.plant(LogtailTree())+ Timber.plant(LogtailTree(this.applicationContext))}}diff --git a/android/app/src/main/java/com/httpsms/Models.kt b/android/app/src/main/java/com/httpsms/Models.ktindex 4b51207c..3b2c859c 100644--- a/android/app/src/main/java/com/httpsms/Models.kt+++ b/android/app/src/main/java/com/httpsms/Models.kt@@ -18,6 +18,22 @@ data class ResponseMessage (fun fromJson(json: String) = klaxon.parse<ResponseMessage>(json)}}+data class ResponsePhone (+ val data: Phone,+ val message: String,+ val status: String,+) {+ companion object {+ fun fromJson(json: String) = klaxon.parse<ResponsePhone>(json)+ }+}++data class Phone (+ val id: String,++ @Json(name = "user_id")+ val userID: String,+)data class Message (val contact: String,diff --git a/android/app/src/main/java/com/httpsms/Settings.kt b/android/app/src/main/java/com/httpsms/Settings.ktindex 14b87cdb..8a01a5bd 100644--- a/android/app/src/main/java/com/httpsms/Settings.kt+++ b/android/app/src/main/java/com/httpsms/Settings.kt@@ -15,6 +15,7 @@ object Settings {private const val SETTINGS_API_KEY = "SETTINGS_API_KEY"private const val SETTINGS_SERVER_URL = "SETTINGS_SERVER_URL"private const val SETTINGS_FCM_TOKEN = "SETTINGS_FCM_TOKEN"+ private const val SETTINGS_USER_ID = "SETTINGS_USER_ID"private const val SETTINGS_FCM_TOKEN_UPDATE_TIMESTAMP = "SETTINGS_FCM_TOKEN_UPDATE_TIMESTAMP"private const val SETTINGS_HEARTBEAT_TIMESTAMP = "SETTINGS_HEARTBEAT_TIMESTAMP"@@ -179,6 +180,22 @@ object Settings {return getApiKey(context) ?: ""}+ fun setUserID(context:Context, userID: String?) {+ Timber.d(Settings::setUserID.name)+ PreferenceManager.getDefaultSharedPreferences(context)+ .edit()+ .putString(this.SETTINGS_USER_ID, userID)+ .apply()+ }++ // getUserID don't log here as this will create recursion on the LogTail sink+ fun getUserID(context:Context): String {+ val userID = PreferenceManager+ .getDefaultSharedPreferences(context)+ .getString(this.SETTINGS_USER_ID,null)+ return userID ?: ""+ }+fun getServerUrlOrDefault(context:Context): URI {val urlString = getServerUrl(context) ?: "https://api.httpsms.com"return URI(urlString)diff --git a/android/app/src/main/java/com/httpsms/SettingsActivity.kt b/android/app/src/main/java/com/httpsms/SettingsActivity.ktindex 87b0811c..bc89e793 100644--- a/android/app/src/main/java/com/httpsms/SettingsActivity.kt+++ b/android/app/src/main/java/com/httpsms/SettingsActivity.kt@@ -93,6 +93,7 @@ class SettingsActivity : AppCompatActivity() {Settings.setActiveStatusAsync(this, true, Constants.SIM2)Settings.setIncomingActiveSIM1(this, true)Settings.setIncomingActiveSIM2(this, true)+ Settings.setUserID(this, null)Settings.setFcmTokenLastUpdateTimestampAsync(this, 0)redirectToLogin()}diff --git a/web/pages/index.vue b/web/pages/index.vueindex d917b356..9f14459c 100644--- a/web/pages/index.vue+++ b/web/pages/index.vue@@ -318,23 +318,26 @@ fetch('https://api.httpsms.com/v1/messages/send', {<v-tab-item value="php"><pre v-highlight class="php w-full mt-n2 mb-n13"><code><?php-// initialize guzzle client https://github.com/guzzle/guzzle-$client = new GuzzleHttp\Client();-$apiKey = "Get API Key from https://httpsms.com/settings";-$res = $client->request('POST', 'https://api.httpsms.com/v1/messages/send', [- 'headers' => [- 'x-api-key' => $apiKey,- ],- 'json' => [- 'content' => 'This is a sample text message',- 'from' => "+18005550199",- 'to' => '+18005550100'- ]-]);+$options = array(+ 'http' => array(+ 'method' => 'POST',+ 'content' => json_encode( [+ 'content' => 'This is a sample text message',+ 'from' => "+18005550199", // Put the correct phone number here+ 'to' => "+18005550100" // Put the correct phone number here+ ]),+ 'header'=> "Content-Type: application/json\r\n" .+ "Accept: application/json\r\n" .+ "x-api-key: $apiKey\r\n"+ )+);++$context = stream_context_create( $options );+$result = file_get_contents( "https://api.httpsms.com/v1/messages/send", false, $context );-echo $res->getBody();+echo $result;</code></pre></v-tab-item>