Skip to content

Commit 2b50f4f

Browse files
committed
Add userID to debug logs
1 parent 0afef71 commit 2b50f4f

File tree

8 files changed

+68
-23
lines changed

8 files changed

+68
-23
lines changed

android/app/src/main/java/com/httpsms/FirebaseMessagingService.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
8989

9090
if (Settings.isLoggedIn(this)) {
9191
Timber.d("updating SIM1 phone with new fcm token")
92-
HttpSmsApiService.create(this).updatePhone(Settings.getSIM1PhoneNumber(this), token, Constants.SIM1)
92+
val phone = HttpSmsApiService.create(this).updatePhone(Settings.getSIM1PhoneNumber(this), token, Constants.SIM1)
93+
if (phone != null) {
94+
Settings.setUserID(this, phone.userID)
95+
}
9396
}
9497

9598
if(Settings.isDualSIM(this)) {
@@ -106,7 +109,7 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
106109

107110
if (BuildConfig.DEBUG) {
108111
Timber.plant(Timber.DebugTree())
109-
Timber.plant(LogtailTree())
112+
Timber.plant(LogtailTree(this.applicationContext))
110113
}
111114
}
112115

android/app/src/main/java/com/httpsms/HttpSmsApiService.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class HttpSmsApiService(private val apiKey: String, private val baseURL: URI) {
162162
}
163163

164164

165-
fun updatePhone(phoneNumber: String, fcmToken: String, sim: String): Boolean {
165+
fun updatePhone(phoneNumber: String, fcmToken: String, sim: String): Phone? {
166166
val body = """
167167
{
168168
"fcm_token": "$fcmToken",
@@ -181,12 +181,13 @@ class HttpSmsApiService(private val apiKey: String, private val baseURL: URI) {
181181
val response = client.newCall(request).execute()
182182
if (!response.isSuccessful) {
183183
Timber.e("error response [${response.body?.string()}] with code [${response.code}] while sending fcm token [${body}]")
184-
return false
184+
return null
185185
}
186186

187+
val payload = ResponsePhone.fromJson(response.body!!.string())?.data
187188
response.close()
188189
Timber.i("fcm token sent successfully for phone [$phoneNumber]" )
189-
return true
190+
return payload
190191
}
191192

192193

android/app/src/main/java/com/httpsms/LogtailTree.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.httpsms
22

3+
import android.content.Context
34
import android.os.Build
45
import com.beust.klaxon.Klaxon
56
import okhttp3.MediaType.Companion.toMediaType
@@ -12,7 +13,7 @@ import java.time.ZonedDateTime
1213
import java.time.format.DateTimeFormatter
1314
import java.util.concurrent.ConcurrentLinkedQueue
1415

15-
class LogtailTree: Timber.DebugTree() {
16+
class LogtailTree(val context: Context): Timber.DebugTree() {
1617
private val client = OkHttpClient()
1718
private val jsonMediaType = "application/json; charset=utf-8".toMediaType()
1819
private val queue: ConcurrentLinkedQueue<LogEntry> = ConcurrentLinkedQueue<LogEntry>()
@@ -30,6 +31,7 @@ class LogtailTree: Timber.DebugTree() {
3031
Build.DEVICE,
3132
Build.VERSION.SDK_INT,
3233
ZonedDateTime.now(ZoneOffset.UTC).format(formatter),
34+
Settings.getUserID(context),
3335
t
3436
)
3537
queue.add(logEntry)
@@ -76,5 +78,6 @@ class LogtailTree: Timber.DebugTree() {
7678
val device: String,
7779
val version: Int,
7880
val dt: String,
81+
val userID: String,
7982
val throwable: Throwable?)
8083
}

android/app/src/main/java/com/httpsms/MainActivity.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,9 @@ class MainActivity : AppCompatActivity() {
198198

199199
private fun sendFCMToken(timestamp: Long, context:Context, phoneNumber: String, sim: String) {
200200
Thread {
201-
val updated = HttpSmsApiService.create(context).updatePhone(phoneNumber, Settings.getFcmToken(context) ?: "", sim)
202-
if (updated) {
201+
val phone = HttpSmsApiService.create(context).updatePhone(phoneNumber, Settings.getFcmToken(context) ?: "", sim)
202+
if (phone != null) {
203+
Settings.setUserID(context, phone.userID)
203204
Settings.setFcmTokenLastUpdateTimestampAsync(context, timestamp)
204205
Timber.i("[${sim}] FCM token uploaded successfully")
205206
return@Thread
@@ -217,7 +218,7 @@ class MainActivity : AppCompatActivity() {
217218

218219
if (BuildConfig.DEBUG) {
219220
Timber.plant(Timber.DebugTree())
220-
Timber.plant(LogtailTree())
221+
Timber.plant(LogtailTree(this.applicationContext))
221222
}
222223
}
223224

android/app/src/main/java/com/httpsms/Models.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@ data class ResponseMessage (
1818
fun fromJson(json: String) = klaxon.parse<ResponseMessage>(json)
1919
}
2020
}
21+
data class ResponsePhone (
22+
val data: Phone,
23+
val message: String,
24+
val status: String,
25+
) {
26+
companion object {
27+
fun fromJson(json: String) = klaxon.parse<ResponsePhone>(json)
28+
}
29+
}
30+
31+
data class Phone (
32+
val id: String,
33+
34+
@Json(name = "user_id")
35+
val userID: String,
36+
)
2137

2238
data class Message (
2339
val contact: String,

android/app/src/main/java/com/httpsms/Settings.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ object Settings {
1515
private const val SETTINGS_API_KEY = "SETTINGS_API_KEY"
1616
private const val SETTINGS_SERVER_URL = "SETTINGS_SERVER_URL"
1717
private const val SETTINGS_FCM_TOKEN = "SETTINGS_FCM_TOKEN"
18+
private const val SETTINGS_USER_ID = "SETTINGS_USER_ID"
1819
private const val SETTINGS_FCM_TOKEN_UPDATE_TIMESTAMP = "SETTINGS_FCM_TOKEN_UPDATE_TIMESTAMP"
1920
private const val SETTINGS_HEARTBEAT_TIMESTAMP = "SETTINGS_HEARTBEAT_TIMESTAMP"
2021

@@ -179,6 +180,22 @@ object Settings {
179180
return getApiKey(context) ?: ""
180181
}
181182

183+
fun setUserID(context:Context, userID: String?) {
184+
Timber.d(Settings::setUserID.name)
185+
PreferenceManager.getDefaultSharedPreferences(context)
186+
.edit()
187+
.putString(this.SETTINGS_USER_ID, userID)
188+
.apply()
189+
}
190+
191+
// getUserID don't log here as this will create recursion on the LogTail sink
192+
fun getUserID(context:Context): String {
193+
val userID = PreferenceManager
194+
.getDefaultSharedPreferences(context)
195+
.getString(this.SETTINGS_USER_ID,null)
196+
return userID ?: ""
197+
}
198+
182199
fun getServerUrlOrDefault(context:Context): URI {
183200
val urlString = getServerUrl(context) ?: "https://api.httpsms.com"
184201
return URI(urlString)

android/app/src/main/java/com/httpsms/SettingsActivity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class SettingsActivity : AppCompatActivity() {
9393
Settings.setActiveStatusAsync(this, true, Constants.SIM2)
9494
Settings.setIncomingActiveSIM1(this, true)
9595
Settings.setIncomingActiveSIM2(this, true)
96+
Settings.setUserID(this, null)
9697
Settings.setFcmTokenLastUpdateTimestampAsync(this, 0)
9798
redirectToLogin()
9899
}

web/pages/index.vue

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -318,23 +318,26 @@ fetch('https://api.httpsms.com/v1/messages/send', {
318318
<v-tab-item value="php">
319319
<pre v-highlight class="php w-full mt-n2 mb-n13">
320320
<code>&#60;?php
321-
// initialize guzzle client https://github.com/guzzle/guzzle
322-
$client = new GuzzleHttp\Client();
323-
324321
$apiKey = "Get API Key from https://httpsms.com/settings";
325322

326-
$res = $client->request('POST', 'https://api.httpsms.com/v1/messages/send', [
327-
'headers' => [
328-
'x-api-key' => $apiKey,
329-
],
330-
'json' => [
331-
'content' => 'This is a sample text message',
332-
'from' => "+18005550199",
333-
'to' => '+18005550100'
334-
]
335-
]);
323+
$options = array(
324+
'http' => array(
325+
'method' => 'POST',
326+
'content' => json_encode( [
327+
'content' => 'This is a sample text message',
328+
'from' => "+18005550199", // Put the correct phone number here
329+
'to' => "+18005550100" // Put the correct phone number here
330+
]),
331+
'header'=> "Content-Type: application/json\r\n" .
332+
"Accept: application/json\r\n" .
333+
"x-api-key: $apiKey\r\n"
334+
)
335+
);
336+
337+
$context = stream_context_create( $options );
338+
$result = file_get_contents( "https://api.httpsms.com/v1/messages/send", false, $context );
336339

337-
echo $res->getBody();
340+
echo $result;
338341
</code>
339342
</pre>
340343
</v-tab-item>

0 commit comments

Comments
 (0)