使用 Remote View 在 Android 10 上发出暗通知
Dark notification on Android 10 with Remote View
当我在 Android 中将显示模式更改为深色时,根据此处显示的图像获取深色(黑色)通知 10。我尝试更改文本颜色,但它没有重新填充。我正在使用远程视图来创建自定义通知。如何更改深色模式和浅色模式的远程内容视图文本颜色不同?这是我的代码:
val contentViewBig = RemoteViews(context.applicationContext.packageName, R.layout.design_custom_notification)
contentViewBig.setTextViewText(R.id.tvNotificationTitle, notificationTitle)
contentViewBig.setTextViewText(R.id.tvNotificationBody, notificationBody)
val nId = Random().nextInt()
val pendingIntent = PendingIntent.getActivity(context,
nId /* Request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT)
val channelId = context.getString(R.string.default_notification_channel_id)
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.drawable.ic_notification)
.setAutoCancel(true)
.setCustomContentView(contentViewBig)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setLights(Color.WHITE, 2000, 3000)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(channelId,
context.resources.getString(R.string.app_name), NotificationManager.IMPORTANCE_HIGH)
channel.description = context.resources.getString(R.string.app_name)
channel.setShowBadge(true)
channel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
notificationManager?.createNotificationChannel(channel)
}
if (notificationManager != null) {
notificationManager.notify(nId, notificationBuilder.build())
wakeLockScreen(context)
}
我通过创建 values-night 文件夹解决了问题。我在 values-night 文件夹中添加了 colors.xml 并覆盖了我在正常 values - colors.xml 中使用的颜色在远程视图设计文件中应用文本颜色。
参考。 https://medium.cobeisfresh.com/how-to-implement-day-night-mode-in-your-android-app-2f21907f9b0a
更新:
还有一件事要防止最新的 Xiaomi devices
在您处于暗模式时强制进行暗应用设计。
将 theme.xml
中的以下参数应用于您的主要主题样式。
<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
当我在 Android 中将显示模式更改为深色时,根据此处显示的图像获取深色(黑色)通知 10。我尝试更改文本颜色,但它没有重新填充。我正在使用远程视图来创建自定义通知。如何更改深色模式和浅色模式的远程内容视图文本颜色不同?这是我的代码:
val contentViewBig = RemoteViews(context.applicationContext.packageName, R.layout.design_custom_notification)
contentViewBig.setTextViewText(R.id.tvNotificationTitle, notificationTitle)
contentViewBig.setTextViewText(R.id.tvNotificationBody, notificationBody)
val nId = Random().nextInt()
val pendingIntent = PendingIntent.getActivity(context,
nId /* Request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT)
val channelId = context.getString(R.string.default_notification_channel_id)
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.drawable.ic_notification)
.setAutoCancel(true)
.setCustomContentView(contentViewBig)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setLights(Color.WHITE, 2000, 3000)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(channelId,
context.resources.getString(R.string.app_name), NotificationManager.IMPORTANCE_HIGH)
channel.description = context.resources.getString(R.string.app_name)
channel.setShowBadge(true)
channel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
notificationManager?.createNotificationChannel(channel)
}
if (notificationManager != null) {
notificationManager.notify(nId, notificationBuilder.build())
wakeLockScreen(context)
}
我通过创建 values-night 文件夹解决了问题。我在 values-night 文件夹中添加了 colors.xml 并覆盖了我在正常 values - colors.xml 中使用的颜色在远程视图设计文件中应用文本颜色。
参考。 https://medium.cobeisfresh.com/how-to-implement-day-night-mode-in-your-android-app-2f21907f9b0a
更新:
还有一件事要防止最新的 Xiaomi devices
在您处于暗模式时强制进行暗应用设计。
将 theme.xml
中的以下参数应用于您的主要主题样式。
<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>