android - 为android10中的通知操作设置背景颜色
问题描述
我 按照这篇文章https://medium.com/@dcostalloyd90/show-incoming-voip-call-notification-and-open-activity-for-android-os-10-5aada2d4c1e4实现了这个图片链接 通知按钮正在工作美好的。唯一的问题是我无法为这些操作按钮设置背景颜色,如上面文章中所示。我想分别设置绿色和红色来接受和取消按钮。我怎样才能做到这一点?检查以下代码:
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)
按钮.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>
解决方案
我希望你的问题仍然相关。我也遇到了这个问题,但经过一些研究,我决定不使用“样式化”操作(没有按预期工作),而是使用自定义通知布局。
是的,还有一个选项可以将自定义视图附加到通知中,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()
notificationLayout
创建如下:
val notificationLayout = RemoteViews(contex.packageName, R.layout.some_layout)
notificationLayout.setOnClickPendingIntent(R.id.some_button, actionPendingIntent)
您的布局some_layout
可以是普通布局,就像您为自定义对话框、片段或活动创建的一样。但请注意,a 的布局RemoteViews
仅限于以下视图和视图组:
https ://developer.android.com/reference/android/widget/RemoteViews
如果您使用文档中未列出的其他视图或视图组,则根本不会显示您的通知。
还要关注https://developer.android.com/training/notify-user/custom-notification。在这里你可以找到一些有用的提示,为什么你应该为你在布局中使用的 TextViews 使用预定义的样式,以符合日夜模式,而不是意外地出现白色文本。
如果一切正常,您可以提出如下样式通知:
PS:如果您想为来电(或类似的东西)创建自定义通知,并且您希望您的通知始终保持打开状态而不是折叠到托盘中,然后在FullscreenIntent
您的通知中添加一个(https://developer .android.com/training/notify-user/time-sensitive):
notificationBuilder.setFullScreenIntent(anotherActionPendingIntent, true)
并且不要忘记在清单中包含所需的权限:
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
PPS:另外,这是自定义通知的完整布局:
<?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>
推荐阅读
- r - 创建一个包含缺少数据的计数和百分比的表
- node.js - 运行win32函数的electron/node-ffi错误
- azure-devops - 在 wiql 中,有没有办法从所有子任务中总结已完成、剩余时间等...?
- java - Mockito 中缺少任何 *() 静态方法 1.如何合理替换它们?
- javascript - 将现代javascript中输入类型数字的两个值相加
- android - Android Webview 访问被拒绝
- c# - 语音频道 ID
- ios - Swift - 在 UICollectionView 单元格上应用效果减半
- django - Django celery - 检查一个任务是否完成,如果完成则重新启动任务(如果没有完成,不重新启动任务)
- android - 带有 NEON 的 OpenCV 4.0 Android (NDK):为什么?为什么不?