修复权限问题

pull/2/head
minxiwan 2 weeks ago
parent fc6ebccb59
commit 69bc905300

3
.idea/.gitignore vendored

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

File diff suppressed because it is too large Load Diff

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/OnlineMsgServer.iml" filepath="$PROJECT_DIR$/.idea/OnlineMsgServer.iml" />
</modules>
</component>
</project>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

@ -9,9 +9,11 @@ import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge import androidx.activity.enableEdgeToEdge
import androidx.core.app.ActivityCompat import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import com.onlinemsg.client.service.ChatForegroundService
import com.onlinemsg.client.ui.OnlineMsgApp import com.onlinemsg.client.ui.OnlineMsgApp
class MainActivity : ComponentActivity() { class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
requestNotificationPermissionIfNeeded() requestNotificationPermissionIfNeeded()
@ -21,18 +23,50 @@ class MainActivity : ComponentActivity() {
} }
} }
private val PERMISSION_REQUEST_NOTIFICATIONS = 1001
private fun requestNotificationPermissionIfNeeded() { private fun requestNotificationPermissionIfNeeded() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) return if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) return
val granted = ContextCompat.checkSelfPermission( val granted = ContextCompat.checkSelfPermission(
this, this,
Manifest.permission.POST_NOTIFICATIONS Manifest.permission.POST_NOTIFICATIONS
) == PackageManager.PERMISSION_GRANTED ) == PackageManager.PERMISSION_GRANTED
if (granted) return if (granted) {
ActivityCompat.requestPermissions( // 已有权限
this, startForegroundServiceIfNeeded()
arrayOf(Manifest.permission.POST_NOTIFICATIONS), } else {
REQUEST_NOTIFICATION_PERMISSION // 请求权限
) ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.POST_NOTIFICATIONS),
REQUEST_NOTIFICATION_PERMISSION
)
}
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) {
REQUEST_NOTIFICATION_PERMISSION -> {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
/* 授予权限的处理 */
startForegroundServiceIfNeeded()
} else {
/* 拒绝权限的处理 */
}
}
}
}
/**
* 根据业务逻辑决定何时启动前台服务(@emilia-t)
*/
private fun startForegroundServiceIfNeeded() {
ChatForegroundService.start(this)
} }
private companion object { private companion object {

@ -21,6 +21,10 @@ import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import android.Manifest
import android.content.pm.PackageManager
import android.util.Log
import android.annotation.SuppressLint
class ChatForegroundService : Service() { class ChatForegroundService : Service() {
@ -62,6 +66,18 @@ class ChatForegroundService : Service() {
override fun onBind(intent: Intent?): IBinder? = null override fun onBind(intent: Intent?): IBinder? = null
/**
* 权限检查函数(@emilia-t)
*/
private fun hasNotificationPermission(): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED
} else {
true // 低于 Android 13 不需要权限
}
}
@SuppressLint("MissingPermission")
private fun observeStatusAndRefreshNotification() { private fun observeStatusAndRefreshNotification() {
if (statusJob != null) return if (statusJob != null) return
statusJob = serviceScope.launch { statusJob = serviceScope.launch {
@ -69,10 +85,15 @@ class ChatForegroundService : Service() {
.map { it.status to it.statusHint } .map { it.status to it.statusHint }
.distinctUntilChanged() .distinctUntilChanged()
.collect { (status, hint) -> .collect { (status, hint) ->
NotificationManagerCompat.from(this@ChatForegroundService).notify( /* 检查是否有通知权限(@emilia-t) */
FOREGROUND_NOTIFICATION_ID, if (hasNotificationPermission()) {
buildForegroundNotification(status, hint) NotificationManagerCompat.from(this@ChatForegroundService).notify(
) FOREGROUND_NOTIFICATION_ID,
buildForegroundNotification(status, hint)
)
} else {
Log.d("ChatForegroundService", "通知权限缺失,跳过前台通知更新")
}
if (status == ConnectionStatus.IDLE && !ChatSessionManager.shouldForegroundServiceRun()) { if (status == ConnectionStatus.IDLE && !ChatSessionManager.shouldForegroundServiceRun()) {
stopForeground(STOP_FOREGROUND_REMOVE) stopForeground(STOP_FOREGROUND_REMOVE)
stopSelf() stopSelf()

Loading…
Cancel
Save