quick question, is it possible to customize style for Notification action in android, so the notification can look like the one from popular messaging apps like WhatsApp, Viber, Slack.
So far I've tried using custom layout, but then the behaviour is not the same, e.g. when switching from small to big layout the title is not animated the same way as the forementioned apps.
Also I've tried setting the span to the Action Text but it doesn't work.
Using NotificationCompat.CallsStyle() gives different result, it includes icons within these Answer and decline buttons, also you can display only one notification (which doesn't work for my case).
Ok, as things usually happen after 3 days of try outs and right after I've posted a question, I've managed to find the solution. I will not add the code for all the pre-required steps here (create notif. channel etc...), only the important part:
First you need the following permission into Manifest file:
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT"/>
Then: Please note that the given solution did not work without the fullscreen Intent.
val answerAction = NotificationCompat.Action.Builder(
IconCompat.createWithResource(this, R.drawable.your_icon_drawable),
getSpannableText("Answer", R.color.green),
answerCallPendingIntent
).build()
NotificationCompat.Builder(context, "channel_id")
.setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE)
.setSmallIcon(R.drawable.your_icon)
.setColor(ContextCompat.getColor`enter code here`(context, R.color.your_color))
.setContentTitle("Your content title")
.setContentText("Your content text")
.setFullScreenIntent(fullScreenIntent) // Important!
.setStyle(NotificationCompat.DecoratedCustomViewStyle())
.addAction(hangUpAction) // same as answer action but different intent
.addAction(answerAction)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.build()
private fun getSpannedText(
text: String,
@ColorRes color: Int
):Spannable {
val spannable = SpannableString(text)
spannable.setSpan(
ForegroundColorSpan(this.getColor(color)),
0, text.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
return spannable
}
Keep in mind that the appearance will not be identical across different OS versions, but this way notification will adjust to the default system notification, notification buttons will appear as notification buttons on other apps which uses this approach, and the default animation will work as expected! Happy coding everyone!