Skip to content

Commit df340e9

Browse files
committed
[gateway] select notification channel
1 parent 1245c7b commit df340e9

File tree

9 files changed

+95
-23
lines changed

9 files changed

+95
-23
lines changed

app/src/main/java/me/capcom/smsgateway/modules/gateway/GatewayApi.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ class GatewayApi(
153153

154154
data class DevicePatchRequest(
155155
val id: String,
156-
val pushToken: String,
156+
val pushToken: String?,
157157
)
158158

159159
data class MessagePatchRequest(

app/src/main/java/me/capcom/smsgateway/modules/gateway/GatewayService.kt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class GatewayService(
9696
}
9797
//endregion
9898

99-
//region Device
99+
//region Device
100100
internal suspend fun registerDevice(
101101
pushToken: String?,
102102
registerMode: RegistrationMode
@@ -162,15 +162,13 @@ class GatewayService(
162162
val settings = settings.registrationInfo ?: return
163163
val accessToken = settings.token
164164

165-
pushToken?.let {
166-
api.devicePatch(
167-
accessToken,
168-
GatewayApi.DevicePatchRequest(
169-
settings.id,
170-
it
171-
)
165+
api.devicePatch(
166+
accessToken,
167+
GatewayApi.DevicePatchRequest(
168+
settings.id,
169+
pushToken
172170
)
173-
}
171+
)
174172

175173
this.settings.fcmToken = pushToken
176174

app/src/main/java/me/capcom/smsgateway/modules/gateway/GatewaySettings.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import me.capcom.smsgateway.modules.settings.get
88
class GatewaySettings(
99
private val storage: KeyValueStorage,
1010
) : Exporter, Importer {
11+
enum class NotificationChannel {
12+
AUTO,
13+
SSE_ONLY,
14+
}
15+
1116
var enabled: Boolean
1217
get() = storage.get<Boolean>(ENABLED) ?: false
1318
set(value) = storage.set(ENABLED, value)
@@ -33,20 +38,25 @@ class GatewaySettings(
3338
val privateToken: String?
3439
get() = storage.get<String>(PRIVATE_TOKEN)
3540

41+
val notificationChannel: NotificationChannel
42+
get() = storage.get<NotificationChannel>(NOTIFICATION_CHANNEL) ?: NotificationChannel.AUTO
43+
3644
companion object {
3745
private const val REGISTRATION_INFO = "REGISTRATION_INFO"
3846
private const val ENABLED = "ENABLED"
3947
private const val FCM_TOKEN = "fcm_token"
4048

4149
private const val CLOUD_URL = "cloud_url"
4250
private const val PRIVATE_TOKEN = "private_token"
51+
private const val NOTIFICATION_CHANNEL = "notification_channel"
4352

4453
const val PUBLIC_URL = "https://api.sms-gate.app/mobile/v1"
4554
}
4655

4756
override fun export(): Map<String, *> {
4857
return mapOf(
4958
CLOUD_URL to serverUrl,
59+
NOTIFICATION_CHANNEL to notificationChannel.name,
5060
)
5161
}
5262

@@ -75,6 +85,16 @@ class GatewaySettings(
7585
changed
7686
}
7787

88+
NOTIFICATION_CHANNEL -> {
89+
val newValue = it.value?.let { NotificationChannel.valueOf(it.toString()) }
90+
?: NotificationChannel.AUTO
91+
val changed = notificationChannel != newValue
92+
93+
storage.set(it.key, newValue.name)
94+
95+
changed
96+
}
97+
7898
else -> false
7999
}
80100
}.any { it }

app/src/main/java/me/capcom/smsgateway/services/PushService.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.google.firebase.messaging.FirebaseMessagingService
77
import com.google.firebase.messaging.RemoteMessage
88
import me.capcom.smsgateway.modules.events.ExternalEvent
99
import me.capcom.smsgateway.modules.events.ExternalEventType
10+
import me.capcom.smsgateway.modules.gateway.GatewaySettings
1011
import me.capcom.smsgateway.modules.gateway.workers.RegistrationWorker
1112
import me.capcom.smsgateway.modules.logs.LogsService
1213
import me.capcom.smsgateway.modules.logs.db.LogEntry
@@ -53,6 +54,18 @@ class PushService : FirebaseMessagingService(), KoinComponent {
5354
companion object : KoinComponent {
5455
fun register(context: Context): Unit {
5556
val logger = get<LogsService>()
57+
val settings = get<GatewaySettings>()
58+
59+
if (settings.notificationChannel == GatewaySettings.NotificationChannel.SSE_ONLY) {
60+
logger.insert(
61+
priority = LogEntry.Priority.INFO,
62+
module = PushService::class.java.simpleName,
63+
message = "FCM registration skipped: SSE_ONLY channel configured"
64+
)
65+
RegistrationWorker.start(context, null, false)
66+
return
67+
}
68+
5669

5770
logger.insert(
5871
priority = LogEntry.Priority.INFO,
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24"
5+
android:viewportHeight="24">
6+
<path
7+
android:fillColor="?attr/colorControlNormal"
8+
android:pathData="M12,22c1.1,0 2,-0.9 2,-2h-4c0,1.1 0.89,2 2,2zM18,16v-5c0,-3.07 -1.64,-5.64 -4.5,-6.32L13.5,4c0,-0.83 -0.67,-1.5 -1.5,-1.5s-1.5,0.67 -1.5,1.5v0.68C7.63,5.36 6,7.92 6,11v5l-2,2v1h16v-1l-2,-2z" />
9+
</vector>

app/src/main/res/values/arrays.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,12 @@
2929
<item>LIFO</item>
3030
<item>FIFO</item>
3131
</string-array>
32+
<string-array name="notification_channels_titles">
33+
<item>Auto</item>
34+
<item>SSE Only</item>
35+
</string-array>
36+
<string-array name="notification_channels_values" translatable="false">
37+
<item>AUTO</item>
38+
<item>SSE_ONLY</item>
39+
</string-array>
3240
</resources>

app/src/main/res/values/strings.xml

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
<string name="app_name">SMSGate</string>
66
<string name="app_version_build">App version (build)</string>
77
<string name="battery_optimization_already_disabled">Battery optimization already disabled</string>
8-
<string name="battery_optimization_is_not_supported_on_this_device">Battery optimization is not supported on this device</string>
8+
<string name="battery_optimization_is_not_supported_on_this_device">Battery optimization is not
9+
supported on this device</string>
910
<string name="battery_optimization">Battery optimization</string>
1011
<string name="btn_cancel">Cancel</string>
1112
<string name="btn_continue">Continue</string>
1213
<string name="by_code">By Code</string>
1314
<string name="can_affect_battery_life">can affect battery life</string>
14-
<string name="click_continue_to_create_an_account_no_personal_information_is_required_nby_continuing_you_agree_to_our_privacy_policy_at_https_docs_sms_gate_app_privacy_policy">Click Continue to create an account. No personal information is required.\nBy continuing, you agree to our Privacy Policy at https://docs.sms-gate.app/privacy/policy/</string>
15+
<string
16+
name="click_continue_to_create_an_account_no_personal_information_is_required_nby_continuing_you_agree_to_our_privacy_policy_at_https_docs_sms_gate_app_privacy_policy">Click
17+
Continue to create an account. No personal information is required.\nBy continuing, you
18+
agree to our Privacy Policy at https://docs.sms-gate.app/privacy/policy/</string>
1519
<string name="cloud_server_dotdotdot">Cloud server…</string>
1620
<string name="cloud_server">Cloud server</string>
1721
<string name="cloud">Cloud</string>
@@ -37,7 +41,8 @@
3741
<string name="internet_connection_unavailable">Internet connection: unavailable</string>
3842
<string name="interval_seconds">Interval (seconds)</string>
3943
<string name="invalid_url">Invalid URL</string>
40-
<string name="is_not_a_valid_port_must_be_between_1024_and_65535">%1$s is not a valid port. Must be between 1024 and 65535.</string>
44+
<string name="is_not_a_valid_port_must_be_between_1024_and_65535">%1$s is not a valid port. Must
45+
be between 1024 and 65535.</string>
4146
<string name="label_password">Password</string>
4247
<string name="label_username">Username</string>
4348
<string name="limits">Limits</string>
@@ -62,7 +67,10 @@
6267
<string name="not_registered">not registered</string>
6368
<string name="not_set">Not set</string>
6469
<string name="notification_title">SMSGate</string>
65-
<string name="online_status_at_the_cost_of_battery_life">Online status at the cost of battery life</string>
70+
<string name="notification_channel">Notification channel</string>
71+
<string name="notifications">Notifications</string>
72+
<string name="online_status_at_the_cost_of_battery_life">Online status at the cost of battery
73+
life</string>
6674
<string name="passphrase">Passphrase</string>
6775
<string name="password_changed_successfully">Password changed successfully</string>
6876
<string name="password_must_be_at_least_14_characters">Password must be at least 14 characters</string>
@@ -71,7 +79,8 @@
7179
<string name="period">Period</string>
7280
<string name="ping_service_is_active">Ping service is active</string>
7381
<string name="ping">Ping</string>
74-
<string name="please_enter_one_time_code_displayed_on_already_registered_device">Please enter the one-time code displayed on the already-registered device</string>
82+
<string name="please_enter_one_time_code_displayed_on_already_registered_device">Please enter
83+
the one-time code displayed on the already-registered device</string>
7584
<string name="port_credentials_etc">Port, credentials, etc.</string>
7685
<string name="port">Port</string>
7786
<string name="private_token">Private Token</string>
@@ -87,7 +96,8 @@
8796
<string name="server">Server</string>
8897
<string name="set_maximum_value_to_activate">Set maximum value to activate</string>
8998
<string name="settings_address_is_sms_capcom_me" translatable="false">api.sms-gate.app</string>
90-
<string name="settings_changed_via_api_restart_the_app_to_apply_changes">Settings changed via API. Restart the app to apply changes.</string>
99+
<string name="settings_changed_via_api_restart_the_app_to_apply_changes">Settings changed via
100+
API. Restart the app to apply changes.</string>
91101
<string name="settings_local_address_is">&lt;a href>%1$s:%2$d&lt;/a></string>
92102
<string name="settings_local_address_not_found">Not available</string>
93103
<string name="settings_local_server">Local server</string>
@@ -107,11 +117,15 @@
107117
<string name="tab_text_home">Home</string>
108118
<string name="tab_text_messages">MESSAGES</string>
109119
<string name="tab_text_settings">SETTINGS</string>
110-
<string name="the_webhook_request_will_wait_for_an_internet_connection">The webhook request will wait for an internet connection</string>
111-
<string name="to_add_a_device_to_an_existing_account_please_fill_in_the_credentials_below">To add a device to an existing account, please fill in the credentials below.</string>
112-
<string name="to_apply_the_changes_restart_the_app_using_the_button_below">To apply the changes, restart the app using the button below.</string>
120+
<string name="the_webhook_request_will_wait_for_an_internet_connection">The webhook request will
121+
wait for an internet connection</string>
122+
<string name="to_add_a_device_to_an_existing_account_please_fill_in_the_credentials_below">To
123+
add a device to an existing account, please fill in the credentials below.</string>
124+
<string name="to_apply_the_changes_restart_the_app_using_the_button_below">To apply the changes,
125+
restart the app using the button below.</string>
113126
<string name="use_empty_to_disable">Use empty to disable</string>
114-
<string name="use_this_code_to_sign_in_on_another_device">Use this code to sign in on another device</string>
127+
<string name="use_this_code_to_sign_in_on_another_device">Use this code to sign in on another
128+
device</string>
115129
<string name="username_must_be_at_least_3_characters">Username must be at least 3 characters</string>
116130
<string name="username">Username</string>
117131
<string name="view">View</string>
@@ -121,8 +135,10 @@
121135
<string name="webhooks_dotdotdot">Webhooks…</string>
122136
<string name="webhooks">Webhooks</string>
123137
<plurals name="review_incoming_sms_webhooks">
124-
<item quantity="one">You have %1$d incoming SMS webhook registered. Please review it to avoid any security risks.</item>
125-
<item quantity="other">You have %1$d incoming SMS webhooks registered. Please review them to avoid any security risks.</item>
138+
<item quantity="one">You have %1$d incoming SMS webhook registered. Please review it to
139+
avoid any security risks.</item>
140+
<item quantity="other">You have %1$d incoming SMS webhooks registered. Please review them to
141+
avoid any security risks.</item>
126142
</plurals>
127143
<string name="processing">Processing</string>
128144
<string name="processing_order_title">Processing order</string>

app/src/main/res/xml/cloud_server_preferences.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212
app:key="gateway.private_token"
1313
app:summary="@string/ignored_for_public_server"
1414
app:title="@string/private_token" />
15+
<ListPreference
16+
app:icon="@drawable/ic_notifications"
17+
app:key="gateway.notification_channel"
18+
app:defaultValue="AUTO"
19+
app:entries="@array/notification_channels_titles"
20+
app:entryValues="@array/notification_channels_values"
21+
app:title="@string/notification_channel"
22+
app:useSimpleSummaryProvider="true" />
1523
</PreferenceCategory>
1624
<PreferenceCategory app:title="@string/credentials">
1725
<EditTextPreference

app/src/main/res/xml/root_preferences.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
android:icon="@drawable/ic_messages"
1616
app:fragment="me.capcom.smsgateway.ui.settings.MessagesSettingsFragment"
1717
app:title="@string/messages"
18-
app:summary="@string/delays_limits_etc"/>
18+
app:summary="@string/delays_limits_etc" />
1919

2020
<Preference
2121
android:icon="@drawable/ic_webhook"

0 commit comments

Comments
 (0)