Skip to content

Commit 8631b3d

Browse files
authored
Fix notification command permissions not launching by directing user to open the app (#1848)
1 parent 8949274 commit 8631b3d

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

app/src/full/java/io/homeassistant/companion/android/notifications/MessagingService.kt

+40-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.homeassistant.companion.android.notifications
22

3+
import android.app.ActivityManager
34
import android.app.Notification
45
import android.app.NotificationChannel
56
import android.app.NotificationManager
@@ -62,6 +63,7 @@ import kotlinx.coroutines.CoroutineScope
6263
import kotlinx.coroutines.Dispatchers
6364
import kotlinx.coroutines.Job
6465
import kotlinx.coroutines.launch
66+
import kotlinx.coroutines.runBlocking
6567
import kotlinx.coroutines.withContext
6668
import org.json.JSONObject
6769
import java.net.URL
@@ -423,7 +425,7 @@ class MessagingService : FirebaseMessagingService() {
423425
val notificationManager =
424426
applicationContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
425427
if (!notificationManager.isNotificationPolicyAccessGranted) {
426-
requestDNDPermission()
428+
notifyMissingPermission(data[MESSAGE].toString())
427429
} else {
428430
when (title) {
429431
DND_ALARMS_ONLY -> notificationManager.setInterruptionFilter(
@@ -447,7 +449,7 @@ class MessagingService : FirebaseMessagingService() {
447449
val notificationManager =
448450
applicationContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
449451
if (!notificationManager.isNotificationPolicyAccessGranted) {
450-
requestDNDPermission()
452+
notifyMissingPermission(data[MESSAGE].toString())
451453
} else {
452454
processRingerMode(audioManager, title)
453455
}
@@ -500,7 +502,7 @@ class MessagingService : FirebaseMessagingService() {
500502
val notificationManager =
501503
applicationContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
502504
if (!notificationManager.isNotificationPolicyAccessGranted) {
503-
requestDNDPermission()
505+
notifyMissingPermission(data[MESSAGE].toString())
504506
} else {
505507
processStreamVolume(
506508
audioManager,
@@ -544,7 +546,7 @@ class MessagingService : FirebaseMessagingService() {
544546
COMMAND_ACTIVITY -> {
545547
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
546548
if (!Settings.canDrawOverlays(applicationContext))
547-
requestSystemAlertPermission()
549+
notifyMissingPermission(data[MESSAGE].toString())
548550
else
549551
processActivityCommand(data)
550552
} else
@@ -553,7 +555,7 @@ class MessagingService : FirebaseMessagingService() {
553555
COMMAND_WEBVIEW -> {
554556
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
555557
if (!Settings.canDrawOverlays(applicationContext))
556-
requestSystemAlertPermission()
558+
notifyMissingPermission(data[MESSAGE].toString())
557559
else
558560
openWebview(title)
559561
} else
@@ -581,7 +583,7 @@ class MessagingService : FirebaseMessagingService() {
581583
COMMAND_MEDIA -> {
582584
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
583585
if (!NotificationManagerCompat.getEnabledListenerPackages(applicationContext).contains(applicationContext.packageName))
584-
requestNotificationPermission()
586+
notifyMissingPermission(data[MESSAGE].toString())
585587
else {
586588
processMediaCommand(data)
587589
}
@@ -1359,6 +1361,38 @@ class MessagingService : FirebaseMessagingService() {
13591361
Log.e(TAG, "Unable to open webview", e)
13601362
}
13611363
}
1364+
1365+
private fun notifyMissingPermission(type: String) {
1366+
val appManager =
1367+
applicationContext.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
1368+
val currentProcess = appManager.runningAppProcesses
1369+
if (currentProcess != null) {
1370+
for (item in currentProcess) {
1371+
if (applicationContext.applicationInfo.processName == item.processName) {
1372+
if (item.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
1373+
val data =
1374+
mutableMapOf(MESSAGE to getString(R.string.missing_command_permission))
1375+
runBlocking {
1376+
sendNotification(data)
1377+
}
1378+
} else {
1379+
when (type) {
1380+
COMMAND_WEBVIEW, COMMAND_ACTIVITY -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
1381+
requestSystemAlertPermission()
1382+
}
1383+
COMMAND_RINGER_MODE, COMMAND_DND, COMMAND_VOLUME_LEVEL -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
1384+
requestDNDPermission()
1385+
}
1386+
COMMAND_MEDIA -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
1387+
requestNotificationPermission()
1388+
}
1389+
}
1390+
}
1391+
}
1392+
}
1393+
}
1394+
}
1395+
13621396
/**
13631397
* Called if InstanceID token is updated. This may occur if the security of
13641398
* the previous token had been compromised. Note that this is called when the InstanceID token

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

+1
Original file line numberDiff line numberDiff line change
@@ -641,4 +641,5 @@ like to connect to:</string>
641641
<string name="entity_widget_desc">Current state and attribute of any entity</string>
642642
<string name="media_player_widget_desc">Control any media player and see current now playing image</string>
643643
<string name="template_widget_desc">Render any template with HTML formatting</string>
644+
<string name="missing_command_permission">Please open the Home Assistant app and send the command again in order to grant the proper permissions.</string>
644645
</resources>

0 commit comments

Comments
 (0)