Skip to content

Commit

Permalink
Allow notification icon to be changed (#1837)
Browse files Browse the repository at this point in the history
  • Loading branch information
dshokouhi authored Oct 28, 2021
1 parent 27f8c18 commit 3d4c37e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ dependencies {
implementation("androidx.activity:activity-compose:1.3.1")
implementation("com.google.android.material:compose-theme-adapter:1.0.4")
implementation("com.google.accompanist:accompanist-appcompat-theme:0.20.0")

implementation("com.mikepenz:iconics-core:5.3.2")
implementation("com.mikepenz:community-material-typeface:5.8.55.0-kotlin@aar")
}

// Disable to fix memory leak and be compatible with the configuration cache.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import androidx.core.text.HtmlCompat
import androidx.core.text.isDigitsOnly
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import com.mikepenz.iconics.IconicsDrawable
import com.mikepenz.iconics.utils.toAndroidIconCompat
import com.vdurmont.emoji.EmojiParser
import io.homeassistant.companion.android.R
import io.homeassistant.companion.android.common.dagger.GraphComponentAccessor
Expand Down Expand Up @@ -85,6 +87,7 @@ class MessagingService : FirebaseMessagingService() {
const val KEY_TEXT_REPLY = "key_text_reply"
const val ALERT_ONCE = "alert_once"
const val INTENT_CLASS_NAME = "intent_class_name"
const val NOTIFICATION_ICON = "notification_icon"

// special action constants
const val REQUEST_LOCATION_UPDATE = "request_location_update"
Expand Down Expand Up @@ -617,7 +620,8 @@ class MessagingService : FirebaseMessagingService() {
val channelId = handleChannel(notificationManagerCompat, data)

val notificationBuilder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_stat_ic_notification)

handleSmallIcon(notificationBuilder, data)

handleSound(notificationBuilder, data)

Expand Down Expand Up @@ -691,6 +695,18 @@ class MessagingService : FirebaseMessagingService() {
}
}

private fun handleSmallIcon(
builder: NotificationCompat.Builder,
data: Map<String, String>
) {
if (data[NOTIFICATION_ICON]?.startsWith("mdi") == true && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val iconName = data[NOTIFICATION_ICON]!!.split(":")[1]
val iconDrawable = IconicsDrawable(applicationContext, "cmd-$iconName").toAndroidIconCompat()
builder.setSmallIcon(iconDrawable)
} else
builder.setSmallIcon(R.drawable.ic_stat_ic_notification)
}

private fun handleContentIntent(
builder: NotificationCompat.Builder,
messageId: Int,
Expand Down Expand Up @@ -756,7 +772,6 @@ class MessagingService : FirebaseMessagingService() {
): NotificationCompat.Builder {

val groupNotificationBuilder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setStyle(
NotificationCompat.BigTextStyle()
.setSummaryText(
Expand All @@ -769,6 +784,7 @@ class MessagingService : FirebaseMessagingService() {
if (data[ALERT_ONCE].toBoolean())
groupNotificationBuilder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN)
handleColor(groupNotificationBuilder, data)
handleSmallIcon(groupNotificationBuilder, data)
return groupNotificationBuilder
}

Expand Down

0 comments on commit 3d4c37e

Please sign in to comment.