Allow control of DND via notification (#1024)

* Allow notification command to turn on or off DND

* Improve existing logic to account for additional commands and do better error handling
This commit is contained in:
Daniel Shokouhi 2020-10-10 20:59:06 -07:00 committed by GitHub
parent 3fbb1874af
commit 490485b982
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 0 deletions

View file

@ -66,6 +66,13 @@ class MessagingService : FirebaseMessagingService() {
const val CLEAR_NOTIFICATION = "clear_notification"
const val REMOVE_CHANNEL = "remove_channel"
const val TTS = "TTS"
const val COMMAND_DND = "command_dnd"
const val DND_PRIORITY_ONLY = "priority_only"
const val DND_ALARMS_ONLY = "alarms_only"
const val DND_ALL = "off"
const val DND_NONE = "total_silence"
val DEVICE_COMMANDS = listOf(COMMAND_DND)
val DND_COMMANDS = listOf(DND_ALARMS_ONLY, DND_ALL, DND_NONE, DND_PRIORITY_ONLY)
}
@Inject
@ -111,6 +118,29 @@ class MessagingService : FirebaseMessagingService() {
Log.d(TAG, "Sending notification title to TTS")
speakNotification(it[TITLE])
}
it[MESSAGE] in DEVICE_COMMANDS -> {
Log.d(TAG, "Processing device command")
when (it[MESSAGE]) {
COMMAND_DND -> {
if (it[TITLE] in DND_COMMANDS) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
handleDeviceCommands(it[MESSAGE], it[TITLE])
else {
mainScope.launch {
Log.d(TAG, "Posting notification to device as it does not support DND commands")
sendNotification(it)
}
}
} else {
mainScope.launch {
Log.d(TAG, "Invalid DND command received, posting notification to device")
sendNotification(it)
}
}
}
else -> Log.d(TAG, "No command received")
}
}
else -> mainScope.launch {
Log.d(TAG, "Creating notification with following data: $it")
sendNotification(it)
@ -177,6 +207,30 @@ class MessagingService : FirebaseMessagingService() {
}
}
@RequiresApi(Build.VERSION_CODES.M)
private fun handleDeviceCommands(message: String?, title: String?) {
when (message) {
COMMAND_DND -> {
val notificationManager =
applicationContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
if (!notificationManager.isNotificationPolicyAccessGranted) {
val intent =
Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
startActivity(intent)
} else {
when (title) {
DND_ALARMS_ONLY -> notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_ALARMS)
DND_ALL -> notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_ALL)
DND_NONE -> notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE)
DND_PRIORITY_ONLY -> notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_PRIORITY)
else -> Log.d(TAG, "Skipping invalid command")
}
}
}
else -> Log.d(TAG, "No command received")
}
}
/**
* Create and show a simple notification containing the received FCM message.
*

View file

@ -18,6 +18,7 @@
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
<uses-feature android:name="android.hardware.touchscreen" android:required="false"/>
<uses-feature android:name="android.hardware.telephony" android:required="false"/>