Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix actionable notifications for Android 12. #2024

Merged
merged 1 commit into from Dec 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 0 additions & 5 deletions app/src/full/AndroidManifest.xml
Expand Up @@ -18,11 +18,6 @@
android:enabled="true"
android:exported="true" />

<receiver
android:name=".notifications.NotificationContentReceiver"
android:enabled="true"
android:exported="true" />

<receiver android:name=".location.HighAccuracyLocationReceiver"
android:enabled="true"
android:exported="true" />
Expand Down
Expand Up @@ -77,6 +77,9 @@ import io.homeassistant.companion.android.common.R as commonR
class MessagingService : FirebaseMessagingService() {
companion object {
const val TAG = "MessagingService"

const val APP_PREFIX = "app://"

const val TITLE = "title"
const val MESSAGE = "message"
const val SUBJECT = "subject"
Expand All @@ -94,6 +97,8 @@ class MessagingService : FirebaseMessagingService() {
const val ALERT_ONCE = "alert_once"
const val INTENT_CLASS_NAME = "intent_class_name"
const val NOTIFICATION_ICON = "notification_icon"
const val URI = "URI"
const val REPLY = "REPLY"

// special action constants
const val REQUEST_LOCATION_UPDATE = "request_location_update"
Expand Down Expand Up @@ -788,19 +793,8 @@ class MessagingService : FirebaseMessagingService() {
groupId: Int,
data: Map<String, String>
) {
val actionUri = if (!data["clickAction"].isNullOrEmpty()) data["clickAction"] else "/"
val contentIntent = Intent(this, NotificationContentReceiver::class.java).apply {
putExtra(NotificationContentReceiver.EXTRA_NOTIFICATION_GROUP, group)
putExtra(NotificationContentReceiver.EXTRA_NOTIFICATION_GROUP_ID, groupId)
putExtra(NotificationContentReceiver.EXTRA_NOTIFICATION_ACTION_URI, actionUri)
}
val contentPendingIntent = PendingIntent.getBroadcast(
this,
messageId,
contentIntent,
PendingIntent.FLAG_CANCEL_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
builder.setContentIntent(contentPendingIntent)
val actionUri = data["clickAction"] ?: "/"
builder.setContentIntent(createOpenUriPendingIntent(actionUri))
}

private fun handleDeleteIntent(
Expand Down Expand Up @@ -1107,12 +1101,8 @@ class MessagingService : FirebaseMessagingService() {
data["action_${i}_uri"],
data
)
val actionIntent = Intent(this, NotificationActionReceiver::class.java).apply {
action =
if (notificationAction.key == "URI")
NotificationActionReceiver.OPEN_URI
else
NotificationActionReceiver.FIRE_EVENT
val eventIntent = Intent(this, NotificationActionReceiver::class.java).apply {
action = NotificationActionReceiver.FIRE_EVENT
if (data["sticky"]?.toBoolean() != true) {
putExtra(NotificationActionReceiver.EXTRA_NOTIFICATION_TAG, tag)
putExtra(NotificationActionReceiver.EXTRA_NOTIFICATION_ID, messageId)
Expand All @@ -1122,44 +1112,82 @@ class MessagingService : FirebaseMessagingService() {
notificationAction
)
}
if (notificationAction.key != "REPLY") {
val actionPendingIntent = PendingIntent.getBroadcast(
this,
(notificationAction.title.hashCode() + System.currentTimeMillis()).toInt(),
actionIntent,
PendingIntent.FLAG_IMMUTABLE
)

val icon =
if (notificationAction.key == "URI")
R.drawable.ic_globe
else
commonR.drawable.ic_stat_ic_notification
builder.addAction(icon, notificationAction.title, actionPendingIntent)
} else {
val remoteInput: RemoteInput = RemoteInput.Builder(KEY_TEXT_REPLY).run {
setLabel(getString(commonR.string.action_reply))
build()
when (notificationAction.key) {
URI -> {
if (!notificationAction.uri.isNullOrBlank()) {
builder.addAction(
R.drawable.ic_globe,
notificationAction.title,
createOpenUriPendingIntent(notificationAction.uri)
)
}
}
REPLY -> {
val remoteInput: RemoteInput = RemoteInput.Builder(KEY_TEXT_REPLY).run {
setLabel(getString(commonR.string.action_reply))
build()
}
val replyPendingIntent = PendingIntent.getBroadcast(
this,
0,
eventIntent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
)
val action: NotificationCompat.Action = NotificationCompat.Action.Builder(
R.drawable.ic_baseline_reply_24,
notificationAction.title,
replyPendingIntent
)
.addRemoteInput(remoteInput)
.build()
builder.addAction(action)
}
else -> {
val actionPendingIntent = PendingIntent.getBroadcast(
this,
(notificationAction.title.hashCode() + System.currentTimeMillis()).toInt(),
eventIntent,
PendingIntent.FLAG_IMMUTABLE
)
builder.addAction(commonR.drawable.ic_stat_ic_notification, notificationAction.title, actionPendingIntent)
}
val replyPendingIntent = PendingIntent.getBroadcast(
this,
0,
actionIntent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
)
val action: NotificationCompat.Action = NotificationCompat.Action.Builder(
R.drawable.ic_baseline_reply_24,
notificationAction.title,
replyPendingIntent
)
.addRemoteInput(remoteInput)
.build()
builder.addAction(action)
}
}
}
}

private fun createOpenUriPendingIntent(
uri: String
): PendingIntent {
val intent = when {
uri.isBlank() -> {
WebViewActivity.newInstance(this)
}
uri.startsWith(APP_PREFIX) -> {
packageManager.getLaunchIntentForPackage(uri.substringAfter(APP_PREFIX))
}
UrlHandler.isAbsoluteUrl(uri) -> {
Intent(Intent.ACTION_VIEW).apply {
this.data = Uri.parse(uri)
}
}
else -> {
WebViewActivity.newInstance(this, uri)
}
} ?: WebViewActivity.newInstance(this)

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK)

return PendingIntent.getActivity(
this,
(uri.hashCode() + System.currentTimeMillis()).toInt(),
intent,
PendingIntent.FLAG_IMMUTABLE
)
}

private fun handleChannel(
notificationManagerCompat: NotificationManagerCompat,
data: Map<String, String>
Expand Down
Expand Up @@ -10,7 +10,6 @@ import androidx.core.app.NotificationManagerCompat
import androidx.core.app.RemoteInput
import dagger.hilt.android.AndroidEntryPoint
import io.homeassistant.companion.android.common.data.integration.IntegrationRepository
import io.homeassistant.companion.android.util.NotificationActionContentHandler
import io.homeassistant.companion.android.util.cancel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand All @@ -25,7 +24,6 @@ class NotificationActionReceiver : BroadcastReceiver() {
companion object {
const val TAG = "NotifActionReceiver"
const val FIRE_EVENT = "FIRE_EVENT"
const val OPEN_URI = "OPEN_URI"
const val EXTRA_NOTIFICATION_TAG = "EXTRA_NOTIFICATION_TAG"
const val EXTRA_NOTIFICATION_ID = "EXTRA_NOTIFICATION_ID"
const val EXTRA_NOTIFICATION_ACTION = "EXTRA_ACTION_KEY"
Expand Down Expand Up @@ -71,11 +69,6 @@ class NotificationActionReceiver : BroadcastReceiver() {

when (intent.action) {
FIRE_EVENT -> fireEvent(notificationAction, onComplete, onFailure)
OPEN_URI -> NotificationActionContentHandler.openUri(
context,
notificationAction.uri,
onComplete
)
}
}

Expand Down

This file was deleted.

This file was deleted.