I have achieved this image link by following this article https://medium.com/@dcostalloyd90/show-incoming-voip-call-notification-and-open-activity-for-android-os-10-5aada2d4c1e4 Notification buttons are working fine. Only issue is i am unable to set background color to these action buttons like visible in above article. I want to set green and red color to accept and cancel buttons respectively. How can i achieve this? Check following codes:
val notificationBuilder =
NotificationCompat.Builder(this@IncomingTripService, CHANNEL_ID2)
.setSmallIcon(R.drawable.logo2)
.setContentTitle("New trip incoming")
.setContentText("Respond as soon as possible")
.setSound(null)
.addAction(R.drawable.buttons,"ACCEPT", receiveCallPendingIntent)
.addAction(R.drawable.buttons, "CANCEL", cancelCallPendingIntent)
.setAutoCancel(true)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setFullScreenIntent(fullScreenPendingIntent, true)
buttons.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="@android:color/black"
>
</solid>
<corners
android:radius="15dp">
</corners>
</shape>
I hope your question is still relevant. I also ran into this issue, but after some research i decided to not work with a "styled" action (didn't work as expected) but with a custom notification layout.
And yes, there's also an option to attach a custom view to the notification, a RemoteViews
:
NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.some_drawable)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setStyle(NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(notificationLayout) // <--- attach custom layout
.build()
The notificationLayout
is created as follows:
val notificationLayout = RemoteViews(contex.packageName, R.layout.some_layout)
notificationLayout.setOnClickPendingIntent(R.id.some_button, actionPendingIntent)
Your layout some_layout
can be a normal layout like you would create for custom dialogs, fragments or activities. But beware, a layout of a RemoteViews
is limited to the following Views and ViewGroups:
https://developer.android.com/reference/android/widget/RemoteViews
If you use other Views or ViewGroups than listed in the documentation your notification will simply not be displayed.
Also have an eye on https://developer.android.com/training/notify-user/custom-notification. Here you can find some useful hints why you should use predefined styles for the TextViews you're using in your layout, to be conform with day- and night mode and not to have white-on-white-text by accident.
And if everything is working fine you can come up with a styled notification like this one:
PS: If you want to create a custom notification for an incoming call (or something like this) and you want your notification to stay open all the time and not to collaps to the tray then add a FullscreenIntent
to your notification (https://developer.android.com/training/notify-user/time-sensitive):
notificationBuilder.setFullScreenIntent(anotherActionPendingIntent, true)
And also don't forget to include the required permission in the manifest:
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
PPS: As an addition, here's the full layout of the custom notification:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/txtTitle"
style="@style/TextAppearance.Compat.Notification.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/notification_incoming_call_title" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:orientation="horizontal">
<Button
android:id="@+id/btnAccept"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_weight="1"
android:backgroundTint="@color/notification_incoming_call_accept"
android:text="@string/notification_incoming_call_accept"
android:textAllCaps="false"
android:textColor="@color/notification_incoming_call_text" />
<Button
android:id="@+id/btnDecline"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:backgroundTint="@color/notification_incoming_call_decline"
android:text="@string/notification_incoming_call_decline"
android:textAllCaps="false"
android:textColor="@color/notification_incoming_call_text" />
</LinearLayout>
</LinearLayout>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With