Skip to content

Commit fbc0ed6

Browse files
committed
Fix ServerService/Foreground-Service for Android 13+
1 parent 8c63dc6 commit fbc0ed6

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<uses-permission android:name="android.permission.INTERNET" />
99
<uses-permission android:name="android.permission.SEND_SMS" />
1010
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
11+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC"/>
12+
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
1113

1214
<application
1315
android:name=".AppContext"
@@ -28,7 +30,9 @@
2830
</intent-filter>
2931
</activity>
3032

31-
<service android:name=".server.ServerService" />
33+
<service android:name=".server.ServerService"
34+
android:exported="false"
35+
android:foregroundServiceType="dataSync" />
3236
</application>
3337

3438
</manifest>

app/src/main/java/net/xcreen/restsms/fragments/HomeFragment.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class HomeFragment : Fragment() {
2525
private var toggleServerBtn: Button? = null
2626
private var appContext: AppContext? = null
2727
private val SMS_PERMISSION_REQUEST = 100
28+
private val NOTIFICATION_PERMISSION_REQUEST = 101
2829

2930
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
3031
val rootView = inflater.inflate(R.layout.fragment_home, container, false)
@@ -42,6 +43,16 @@ class HomeFragment : Fragment() {
4243
ActivityCompat.requestPermissions(requireActivity(), arrayOf(Manifest.permission.SEND_SMS), SMS_PERMISSION_REQUEST)
4344
return@setOnClickListener
4445
}
46+
// Check if Notification-Permission is grant
47+
if (Build.VERSION.SDK_INT >= 33) {
48+
if (ActivityCompat.checkSelfPermission(v.context, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
49+
//Show Error Toast
50+
Toast.makeText(v.context, resources.getText(R.string.no_notification_permission), Toast.LENGTH_LONG).show()
51+
//Request Permission
52+
ActivityCompat.requestPermissions(requireActivity(), arrayOf(Manifest.permission.POST_NOTIFICATIONS), NOTIFICATION_PERMISSION_REQUEST)
53+
return@setOnClickListener
54+
}
55+
}
4556
//Check if Device has a Sim-Card
4657
try {
4758
val telephonyManager = v.context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<string name="server_failed_bindex">Server can not run on this port! (Bind-Exception)</string>
1616
<string name="invalid_sim">Sim-Card is missing or not ready to send sms!</string>
1717
<string name="no_sms_permission">SMS-Permission is required to run the server!</string>
18+
<string name="no_notification_permission">Notification-Permission is required to run the server as a foreground-service!</string>
1819

1920
<string name="notification_text">Server is running! On port: %1$d</string>
2021

0 commit comments

Comments
 (0)