Skip to content

Commit

Permalink
feat: added support to modify notification small icon
Browse files Browse the repository at this point in the history
  • Loading branch information
mrehan27 committed Sep 22, 2022
1 parent 4633c49 commit b93c2dc
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@ import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.BitmapFactory
import android.media.RingtoneManager
import android.os.Build
import android.os.Bundle
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import androidx.core.app.NotificationCompat
import androidx.core.app.TaskStackBuilder
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import io.customer.messagingpush.data.model.CustomerIOParsedPushPayload
import io.customer.messagingpush.di.deepLinkUtil
import io.customer.messagingpush.di.moduleConfig
import io.customer.messagingpush.extensions.*
import io.customer.messagingpush.util.DeepLinkUtil
import io.customer.messagingpush.util.PushTrackingUtil.Companion.DELIVERY_ID_KEY
import io.customer.messagingpush.util.PushTrackingUtil.Companion.DELIVERY_TOKEN_KEY
Expand All @@ -39,6 +43,10 @@ internal class CustomerIOPushNotificationHandler(private val remoteMessage: Remo
const val BODY_KEY = "body"

const val NOTIFICATION_REQUEST_CODE = "requestCode"
private const val FCM_METADATA_DEFAULT_NOTIFICATION_ICON =
"com.google.firebase.messaging.default_notification_icon"
private const val FCM_METADATA_DEFAULT_NOTIFICATION_COLOR =
"com.google.firebase.messaging.default_notification_color"
}

private val diGraph: CustomerIOComponent
Expand Down Expand Up @@ -111,7 +119,30 @@ internal class CustomerIOPushNotificationHandler(private val remoteMessage: Remo

bundle.putInt(NOTIFICATION_REQUEST_CODE, requestCode)

val icon = context.applicationInfo.icon
val applicationInfo = try {
context.packageManager.getApplicationInfo(
context.packageName,
PackageManager.GET_META_DATA
)
} catch (ex: Exception) {
logger.error("Package not found ${ex.message}")
null
}
val appMetaData = applicationInfo?.metaData

@DrawableRes
val smallIcon: Int =
remoteMessage.notification?.icon?.let { iconName -> context.getDrawableByName(iconName) }
?: appMetaData?.getMetaDataResource(name = FCM_METADATA_DEFAULT_NOTIFICATION_ICON)
?: context.applicationInfo.icon

@ColorInt
val tintColor: Int? =
remoteMessage.notification?.color?.toColorOrNull()
?: appMetaData?.getMetaDataResource(name = FCM_METADATA_DEFAULT_NOTIFICATION_COLOR)
?.let { id -> context.getColorOrNull(id) }
?: appMetaData?.getMetaDataString(name = FCM_METADATA_DEFAULT_NOTIFICATION_COLOR)
?.toColorOrNull()

// set title and body
val title = bundle.getString(TITLE_KEY) ?: remoteMessage.notification?.title ?: ""
Expand All @@ -120,13 +151,14 @@ internal class CustomerIOPushNotificationHandler(private val remoteMessage: Remo
val channelId = context.packageName
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(context, channelId)
.setSmallIcon(icon)
.setSmallIcon(smallIcon)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setTicker(applicationName)
.setStyle(NotificationCompat.BigTextStyle().bigText(body))
tintColor?.let { color -> notificationBuilder.setColor(color) }

try {
// check for image
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.customer.messagingpush.extensions

import android.content.res.Resources
import android.os.Build
import android.os.Bundle

private val RESOURCE_ID_NULL: Int =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) Resources.ID_NULL else 0

internal fun Bundle.getMetaDataResource(name: String): Int? {
return getInt(name, RESOURCE_ID_NULL).takeUnless { id -> id == RESOURCE_ID_NULL }
}

internal fun Bundle.getMetaDataString(name: String): String? {
return getString(name, null).takeUnless { value -> value.isNullOrBlank() }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.customer.messagingpush.extensions

import android.content.Context
import android.content.res.Resources
import android.os.Build
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import io.customer.sdk.CustomerIO

@DrawableRes
internal fun Context.getDrawableByName(name: String?): Int? = if (name.isNullOrBlank()) null
else resources?.getIdentifier(name, "drawable", packageName)?.takeUnless { id ->
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) id == Resources.ID_NULL
else id == 0
}

@ColorInt
internal fun Context.getColorOrNull(@ColorRes id: Int): Int? = try {
ContextCompat.getColor(this, id)
} catch (ex: Resources.NotFoundException) {
CustomerIO.instance().diGraph.logger.error("Invalid resource $id, ${ex.message}")
null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* ktlint-disable filename */ // until this extension file contains 2+ functions in it, we will disable this ktlint rule.
package io.customer.messagingpush.extensions

import android.graphics.Color
import androidx.annotation.DrawableRes
import io.customer.sdk.CustomerIO

@DrawableRes
internal fun String.toColorOrNull(): Int? = try {
Color.parseColor(this)
} catch (ex: IllegalArgumentException) {
CustomerIO.instance().diGraph.logger.error("Invalid color string $this, ${ex.message}")
null
}

0 comments on commit b93c2dc

Please sign in to comment.