Skip to content

Send and receive SMS messages using your Android phone programmatically via a simple HTTP API

add field IsDualSIM to the Phone model.

Eriksson Monteiroc0c02b563030
diff --git a/android/app/src/main/java/com/httpsms/FirebaseMessagingService.kt b/android/app/src/main/java/com/httpsms/FirebaseMessagingService.kt
index 2732a582..210881c0 100644
--- a/android/app/src/main/java/com/httpsms/FirebaseMessagingService.kt
+++ b/android/app/src/main/java/com/httpsms/FirebaseMessagingService.kt
@@ -67,7 +67,7 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
if (Settings.isLoggedIn(this)) {
Timber.d("updating phone with new fcm token")
- HttpSmsApiService.create(this).updatePhone(Settings.getOwnerOrDefault(this), token)
+ HttpSmsApiService.create(this).updatePhone(Settings.getOwnerOrDefault(this), token, SmsManagerService.isDualSIM(this))
}
}
diff --git a/android/app/src/main/java/com/httpsms/HttpSmsApiService.kt b/android/app/src/main/java/com/httpsms/HttpSmsApiService.kt
index 4499d4cf..8b64e59e 100644
--- a/android/app/src/main/java/com/httpsms/HttpSmsApiService.kt
+++ b/android/app/src/main/java/com/httpsms/HttpSmsApiService.kt
@@ -156,11 +156,12 @@ class HttpSmsApiService(private val apiKey: String, private val baseURL: URI) {
}
- fun updatePhone(phoneNumber: String, fcmToken: String): Boolean {
+ fun updatePhone(phoneNumber: String, fcmToken: String, isDualSIM: Boolean): Boolean {
val body = """
{
"fcm_token": "$fcmToken",
- "phone_number": "$phoneNumber"
+ "phone_number": "$phoneNumber",
+ "is_dual_sim": $isDualSIM
}
""".trimIndent()
diff --git a/android/app/src/main/java/com/httpsms/MainActivity.kt b/android/app/src/main/java/com/httpsms/MainActivity.kt
index f4ee07d4..95622abe 100644
--- a/android/app/src/main/java/com/httpsms/MainActivity.kt
+++ b/android/app/src/main/java/com/httpsms/MainActivity.kt
@@ -146,7 +146,7 @@ class MainActivity : AppCompatActivity() {
}
Thread {
- val updated = HttpSmsApiService.create(context).updatePhone(Settings.getOwnerOrDefault(context), Settings.getFcmToken(context) ?: "")
+ val updated = HttpSmsApiService.create(context).updatePhone(Settings.getOwnerOrDefault(context), Settings.getFcmToken(context) ?: "", SmsManagerService.isDualSIM(this))
if (updated) {
Settings.setFcmTokenLastUpdateTimestampAsync(context, currentTimeStamp)
Timber.i("fcm token uploaded successfully")
diff --git a/android/app/src/main/java/com/httpsms/SmsManagerService.kt b/android/app/src/main/java/com/httpsms/SmsManagerService.kt
index 87040bee..18e1544b 100644
--- a/android/app/src/main/java/com/httpsms/SmsManagerService.kt
+++ b/android/app/src/main/java/com/httpsms/SmsManagerService.kt
@@ -20,6 +20,16 @@ class SmsManagerService {
fun deliveredAction(messageID: String): String {
return "$ACTION_SMS_DELIVERED.$messageID"
}
+
+ @SuppressLint("MissingPermission")
+ fun isDualSIM(context: Context) : Boolean {
+ val localSubscriptionManager: SubscriptionManager = if (Build.VERSION.SDK_INT < 31) {
+ SubscriptionManager.from(context)
+ } else {
+ context.getSystemService(SubscriptionManager::class.java)
+ }
+ return localSubscriptionManager.activeSubscriptionInfoList.size > 1
+ }
}
fun messageParts(context: Context, content: String): ArrayList<String> {
diff --git a/api/pkg/entities/phone.go b/api/pkg/entities/phone.go
index 17cea698..00b91a59 100644
--- a/api/pkg/entities/phone.go
+++ b/api/pkg/entities/phone.go
@@ -13,6 +13,7 @@ type Phone struct {
FcmToken *string `json:"fcm_token" example:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzd....."`
PhoneNumber string `json:"phone_number" example:"+18005550199"`
MessagesPerMinute uint `json:"messages_per_minute" example:"1"`
+ IsDualSIM bool `json:"is_dual_sim" example:"false"`
// MaxSendAttempts determines how many times to retry sending an SMS message
MaxSendAttempts uint `json:"max_send_attempts" example:"1"`
diff --git a/api/pkg/events/phone_deleted_event.go b/api/pkg/events/phone_deleted_event.go
index 69dedb7f..b1659d76 100644
--- a/api/pkg/events/phone_deleted_event.go
+++ b/api/pkg/events/phone_deleted_event.go
@@ -16,4 +16,5 @@ type PhoneDeletedPayload struct {
UserID entities.UserID `json:"user_id"`
Timestamp time.Time `json:"timestamp"`
Owner string `json:"owner"`
+ IsDualSIM bool `json:"is_dual_sim"`
}
diff --git a/api/pkg/events/phone_updated_event.go b/api/pkg/events/phone_updated_event.go
index 5ef999a1..20d2b34b 100644
--- a/api/pkg/events/phone_updated_event.go
+++ b/api/pkg/events/phone_updated_event.go
@@ -16,4 +16,5 @@ type PhoneUpdatedPayload struct {
UserID entities.UserID `json:"user_id"`
Timestamp time.Time `json:"timestamp"`
Owner string `json:"owner"`
+ IsDualSIM bool `json:"is_dual_sim"`
}
diff --git a/api/pkg/requests/phone_update_request.go b/api/pkg/requests/phone_update_request.go
index 6d485540..94961f04 100644
--- a/api/pkg/requests/phone_update_request.go
+++ b/api/pkg/requests/phone_update_request.go
@@ -23,6 +23,9 @@ type PhoneUpsert struct {
MaxSendAttempts uint `json:"max_send_attempts" example:"2"`
FcmToken string `json:"fcm_token" example:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzd....."`
+
+ // IsDualSIM is true if the phone has more than one SIM active
+ IsDualSIM bool `json:"is_dual_sim" example:"false"`
}
// Sanitize sets defaults to MessageOutstanding
@@ -68,5 +71,6 @@ func (input *PhoneUpsert) ToUpsertParams(user entities.AuthUser, source string)
MaxSendAttempts: maxSendAttempts,
FcmToken: fcmToken,
UserID: user.ID,
+ IsDualSIM: input.IsDualSIM,
}
}
diff --git a/api/pkg/services/emulator_push_queue.go b/api/pkg/services/emulator_push_queue.go
index 00393307..ece74b91 100644
--- a/api/pkg/services/emulator_push_queue.go
+++ b/api/pkg/services/emulator_push_queue.go
@@ -73,8 +73,7 @@ func (queue *emulatorPushQueue) push(task PushQueueTask, queueID string) func()
// add content type
request.Header("Content-Type", "application/json")
- err := request.Fetch(ctx)
- if err != nil {
+ if err := request.Fetch(ctx); err != nil {
queue.logger.Error(stacktrace.Propagate(err, fmt.Sprintf("cannot send http request to [%s] for queue task [%s]", task.URL, queueID)))
return
}
diff --git a/api/pkg/services/phone_service.go b/api/pkg/services/phone_service.go
index 3f3a94de..2c0cefb2 100644
--- a/api/pkg/services/phone_service.go
+++ b/api/pkg/services/phone_service.go
@@ -74,6 +74,7 @@ type PhoneUpsertParams struct {
MessagesPerMinute *uint
MaxSendAttempts *uint
MessageExpirationDuration *time.Duration
+ IsDualSIM bool
Source string
UserID entities.UserID
}
@@ -106,9 +107,10 @@ func (service *PhoneService) Upsert(ctx context.Context, params PhoneUpsertParam
UserID: phone.UserID,
Timestamp: phone.UpdatedAt,
Owner: phone.PhoneNumber,
+ IsDualSIM: phone.IsDualSIM,
})
if err != nil {
- msg := fmt.Sprintf("cannot create event when phone is updated")
+ msg := "cannot create event when phone is updated"
return nil, service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
}
@@ -145,9 +147,10 @@ func (service *PhoneService) Delete(ctx context.Context, source string, userID e
UserID: phone.UserID,
Timestamp: phone.UpdatedAt,
Owner: phone.PhoneNumber,
+ IsDualSIM: phone.IsDualSIM,
})
if err != nil {
- msg := fmt.Sprintf("cannot create event when phone is deleted")
+ msg := "cannot create event when phone is deleted"
return service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
}
@@ -172,6 +175,7 @@ func (service *PhoneService) createPhone(ctx context.Context, params PhoneUpsert
MessagesPerMinute: 29,
MessageExpirationSeconds: 15 * 60, // 30 minutes
MaxSendAttempts: 2,
+ IsDualSIM: params.IsDualSIM,
PhoneNumber: phonenumbers.Format(&params.PhoneNumber, phonenumbers.E164),
CreatedAt: time.Now().UTC(),
UpdatedAt: time.Now().UTC(),
@@ -208,5 +212,8 @@ func (service *PhoneService) update(phone *entities.Phone, params PhoneUpsertPar
if params.MessageExpirationDuration != nil {
phone.MessageExpirationSeconds = uint(params.MessageExpirationDuration.Seconds())
}
+
+ phone.IsDualSIM = params.IsDualSIM
+
return phone
}
diff --git a/api/pkg/services/user_service.go b/api/pkg/services/user_service.go
index 6f88e54a..bd707283 100644
--- a/api/pkg/services/user_service.go
+++ b/api/pkg/services/user_service.go
@@ -197,7 +197,7 @@ func (service *UserService) GetSubscriptionUpdateURL(ctx context.Context, userID
subscription, _, err := service.lemonsqueezyClient.Subscriptions.Get(ctx, *user.SubscriptionID)
if err != nil {
- msg := fmt.Sprintf("could not get subscription [%s] for [%T] with with ID [%s]", user.SubscriptionID, user, user.ID)
+ msg := fmt.Sprintf("could not get subscription [%s] for [%T] with with ID [%s]", *user.SubscriptionID, user, user.ID)
return url, service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
}