Android Push Notification intent not localizing app Activity with custom in-app Locale

Viewed 64

I have an app feature that allows users to choose their own custom in-app language (separate from the device language).

Language updates fine when I open the app by tapping the app icon. I used this logic to change the base context:

override fun attachBaseContext(newBase: Context) {
            val updatedContext = LocaleUtils.getLocalizedContextWrapper(getInAppLocale(newBase), locale)
            super.attachBaseContext(updatedContext)
}
fun getLocalizedContextWrapper(context: Context, targetLocale: Locale): ContextWrapper {
            return LocaleUtils(getLocalizedContext(context, targetLocale))
}
fun getLocalizedContext(context: Context, targetLocale: Locale): Context {
            val newConfig = getLocalizedConfig(context, targetLocale)
            return context.createConfigurationContext(newConfig)
}
 private fun getLocalizedConfig(context: Context, targetLocale: Locale): Configuration {
            val resources: Resources = context.resources
            val config: Configuration = resources.configuration
            config.setLocale(targetLocale)

            when {
                Build.VERSION.SDK_INT >= Build.VERSION_CODES.N -> {
                    val localeList = LocaleList(targetLocale)
                    LocaleList.setDefault(localeList)
                    config.setLocales(localeList)
                }
                else -> {
                    Locale.setDefault(targetLocale)
                }
            }
            return config
}

I also ensured the Activity destination is a new task:

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)

But whenever I get a Push Notification and I tap on that to navigate to the app, the content is not localized. I can verify this by having a different language set by the device system settings. When I kill the app and restart it though, the app takes the custom in-app language from my shared preference as normal.

Also, when I tap the Push Notification, I can see that my BaseActivity is calling attachBaseContext() and is updating the correct in-app locale.

Has anyone encountered this problem before? Any help would be much appreciated.

Thank you so much in advance!

0 Answers
Related