Skip to content

Commit

Permalink
Share code for sending messages
Browse files Browse the repository at this point in the history
  • Loading branch information
tananaev committed Sep 22, 2024
1 parent 8fa5696 commit 05718aa
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package org.traccar.gateway
import android.annotation.SuppressLint
import android.os.Handler
import android.os.Looper
import android.telephony.SmsManager
import android.telephony.SubscriptionManager
import android.widget.Toast
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
Expand All @@ -21,14 +19,7 @@ class GatewayMessagingService : FirebaseMessagingService() {
val slot = remoteMessage.data["slot"]?.toInt()
if (phone != null && message != null) {
try {
val smsManager = if (slot != null) {
val subscriptionManager = getSystemService(SubscriptionManager::class.java)
val subscriptionInfo = subscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(slot)
SmsManager.getSmsManagerForSubscriptionId(subscriptionInfo.subscriptionId)
} else {
SmsManager.getDefault()
}
smsManager.sendTextMessage(phone, null, message, null, null)
GatewayServiceUtil.sendMessage(this, phone, message, slot)
} catch (e: Exception) {
handler.post {
Toast.makeText(this, e.message, Toast.LENGTH_LONG).show()
Expand Down
8 changes: 4 additions & 4 deletions app/src/traccar/kotlin/org/traccar/gateway/GatewayServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class GatewayServer(
) : Server(port) {

interface Handler {
fun onSendMessage(phone: String, message: String, saveMessage: Boolean): String?
fun onSendMessage(phone: String, message: String, slot: Int?): String?
}

init {
Expand Down Expand Up @@ -50,20 +50,20 @@ class GatewayServer(

var phone: String? = null
var message: String? = null
var saveMessage = false
var slot: Int? = null

val reader = JsonReader(request.reader)
reader.beginObject()
while (reader.hasNext()) {
when (reader.nextName()) {
"to" -> phone = reader.nextString()
"message" -> message = reader.nextString()
"saveMessage" -> saveMessage = reader.nextBoolean()
"slot" -> slot = reader.nextInt()
}
}

val result = if (phone != null && message != null) {
handler.onSendMessage(phone, message, saveMessage)
handler.onSendMessage(phone, message, slot)
} else {
"Missing phone or message"
}
Expand Down
5 changes: 2 additions & 3 deletions app/src/traccar/kotlin/org/traccar/gateway/GatewayService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import android.content.pm.ServiceInfo
import android.os.Build
import android.os.IBinder
import android.preference.PreferenceManager
import android.telephony.SmsManager
import android.widget.Toast
import androidx.core.app.NotificationCompat
import com.simplemobiletools.smsmessenger.R
Expand Down Expand Up @@ -76,9 +75,9 @@ class GatewayService : Service(), GatewayServer.Handler {
return null
}

override fun onSendMessage(phone: String, message: String, saveMessage: Boolean): String? {
override fun onSendMessage(phone: String, message: String, slot: Int?): String? {
return try {
SmsManager.getDefault().sendTextMessage(phone, null, message, null, null)
GatewayServiceUtil.sendMessage(this, phone, message, slot)
null
} catch (e: Exception) {
Toast.makeText(this, e.message, Toast.LENGTH_LONG).show()
Expand Down
15 changes: 15 additions & 0 deletions app/src/traccar/kotlin/org/traccar/gateway/GatewayServiceUtil.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package org.traccar.gateway

import android.annotation.SuppressLint
import android.app.ActivityManager
import android.content.Context
import android.telephony.SmsManager
import android.telephony.SubscriptionManager

@Suppress("DEPRECATION")
object GatewayServiceUtil {
Expand All @@ -16,4 +19,16 @@ object GatewayServiceUtil {
return false
}

@SuppressLint("MissingPermission")
fun sendMessage(context: Context, phone: String, message: String, slot: Int?) {
val smsManager = if (slot != null) {
val subscriptionManager = context.getSystemService(SubscriptionManager::class.java)
val subscriptionInfo = subscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(slot)
SmsManager.getSmsManagerForSubscriptionId(subscriptionInfo.subscriptionId)
} else {
SmsManager.getDefault()
}
smsManager.sendTextMessage(phone, null, message, null, null)
}

}

0 comments on commit 05718aa

Please sign in to comment.