Skip to content

Commit

Permalink
Feature - Runtime Notification Permission for Android 13 or later
Browse files Browse the repository at this point in the history
For Android 13 or later, user will be asked to grant notification permission on first run of websocket server or http server
  • Loading branch information
umer0586 committed Jul 30, 2024
1 parent db1bff8 commit 84aa6a4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4"
implementation 'com.yanzhenjie.andserver:api:2.1.12'
kapt 'com.yanzhenjie.andserver:processor:2.1.12'
implementation 'com.guolindev.permissionx:permissionx:1.8.0'


}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<!-- POST Notifications permission is introduced in Android 13 -->
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

<application
android:name=".MyApplication"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package github.umer0586.sensorserver.activities

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.os.IBinder
import android.util.Log
Expand All @@ -18,6 +19,7 @@ import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.navigation.NavigationBarView
import com.permissionx.guolindev.PermissionX
import github.umer0586.sensorserver.R
import github.umer0586.sensorserver.databinding.ActivityMainBinding
import github.umer0586.sensorserver.fragments.AvailableSensorsFragment
Expand Down Expand Up @@ -220,6 +222,15 @@ class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListen
httpServerSwitch.setOnCheckedChangeListener { _, isChecked ->
val isServerRunning = httpService?.isServerRunning ?: false
if(isChecked && !isServerRunning){

// Whether user grant this permission or not we will start service anyway
// If permission is not granted foreground notification will not be shown
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
PermissionX.init(this)
.permissions(android.Manifest.permission.POST_NOTIFICATIONS)
.request{_,_,_ -> }
}

val intent = Intent(applicationContext, HttpService::class.java)
ContextCompat.startForegroundService(applicationContext, intent)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package github.umer0586.sensorserver.fragments
import android.content.Context
import android.content.Intent
import android.net.wifi.WifiManager
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
Expand All @@ -13,6 +14,7 @@ import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import com.google.android.material.snackbar.Snackbar
import com.permissionx.guolindev.PermissionX
import github.umer0586.sensorserver.R
import github.umer0586.sensorserver.customextensions.isHotSpotEnabled
import github.umer0586.sensorserver.databinding.FragmentServerBinding
Expand Down Expand Up @@ -93,13 +95,21 @@ class ServerFragment : Fragment(), ServerStateListener
showPulseAnimation()
}

// TODO : When starting a foreground service it is required to provide a notification that will be visible to the user for the duration of the service execution.
// Android 13 introduced a runtime permission for posting notifications,
// requiring that apps ask for this permission and users have to explicitly grant it, otherwise notifications will not be visible.
private fun startServer()
{
Log.d(TAG, "startServer() called")

// Android 13 introduced a runtime permission for posting notifications,
// requiring that apps ask for this permission and users have to explicitly grant it, otherwise notifications will not be visible.
//
// Whether user grant this permission or not we will start service anyway
// If permission is not granted foreground notification will not be shown
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
PermissionX.init(this)
.permissions(android.Manifest.permission.POST_NOTIFICATIONS)
.request{_,_,_ -> }
}

val wifiManager = requireContext().applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager

val noOptionEnabled = with(appSettings){
Expand Down

0 comments on commit 84aa6a4

Please sign in to comment.