Search code examples
androidkotlinnotificationsandroid-10.0android-remoteview

Dark notification on Android 10 with Remote View


Getting Dark (black) notification as per image shown here when I changed display mode to Dark in Android 10. I tried to change text color but it is not reflating. I am using Remote view to create custom notification. How to change remote content view text colors different for Dark mode and light mode? Here is my code:

        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)
        }

enter image description here


Solution

  • I resolved problem by creating values-night folder. I have added colors.xml inside values-night folder and override colors which I have used in normal values - colors.xml and applying text color in remote view design file.

    Ref. https://medium.cobeisfresh.com/how-to-implement-day-night-mode-in-your-android-app-2f21907f9b0a

    UPDATE:

    One more thing to prevent from latest Xiaomi devices forces dark app design while you are in dark mode.

    Apply below parameter in theme.xml to your main theme style.

    <item name="android:forceDarkAllowed" tools:targetApi="q">false</item>