在android10中将背景颜色设置为通知操作 [英] set background color to notifications actions in android10

查看:83
本文介绍了在android10中将背景颜色设置为通知操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做到了按照本文

PS:如果您想为来电(或类似内容)创建自定义通知,并且希望您的通知始终保持打开状态而不塌陷到托盘中,请添加 FullscreenIntent 代码>通知( https://developer.android.com/training/通知用户/时间敏感):

  notificationBuilder.setFullScreenIntent(anotherActionPendingIntent,true) 

也不要忘记在清单中包含所需的权限:

 < uses-permission android:name =" android.permission.USE_FULL_SCREEN_INTENT"/> 

PPS:此外,这是自定义通知的完整布局:

 <?xml version ="1.0"encoding ="utf-8"?< LinearLayoutxmlns:android ="http://schemas.android.com/apk/res/android"android:layout_width =" match_parent"android:layout_height =" wrap_content"android:orientation =" vertical">< TextViewandroid: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"/>< LinearLayoutandroid:layout_width =" match_parent"android:layout_height =" wrap_content"android:layout_marginTop =" 16dp"android:orientation =水平"><按钮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"/><按钮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> 

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>

这篇关于在android10中将背景颜色设置为通知操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆