Android O: Notification Channel localization

Viewed 3784

I created a Notification Channel like this:

NotificationChannel channel = new NotificationChannel(CHANNEL_ID_FOOBAR, getContext().getString(R.string.notification_channel_foobar), NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);

I provided different translations for R.string.notification_channel_foobar and the channel is created with the language being used at the time of the creation, hence if I eventually change the language of my device, that channel will remain in the old language. Is there a way to overcome this or is this a limitation, i.e. by design?

4 Answers

To apply the new language to your notification channel, you need to listen to the ACTION_LOCALE_CHANGED broadcast in your app and call createNotificationChannel again in your receiver.

Recreating the channels will update your strings to the new language (none of the other channel features will be modified). You can see it in the documentation.

You can simply create the channel every time your main activity launches. Pretty elegant solution without having to use any broadcast receivers. That way the channel will always get the fresh values from your string resources, even when you're adding more languages. (Premature optimization is the root of all evil.)

The createNotificationChannel command will create the channel if it hasn't been created yet, and it will update the channel if it has been already created.

If the channel is already created, then the only thing you can change is the name of the channel and the channel description, nothing else. The importance will be ignored, because the user might have already changed the importance of the channel manually. But even if he hasn't changed that, still the importance won't be updated, and actually that's the purpose of the notification channels. To give freedom to the users to manage their channels, without the developers messing with them when the app is updated.

So in summary, by declaring:

NotificationChannel notificationChannel = new NotificationChannel("channel id", "channel new name", NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(notificationChannel);

in an already created channel, the name of the channel will be updated, but not the importance. If you want to update the channel description as well, you can do that like that:

notificationChannel.setDescription("new description"); //set that before creating the channel

Once you have created a channel, you cannot change anything about it. As we provide "String" for the channel name, system will represent this name to the user always.

One (bad) thing you can try is deleting the old channel and creating another one having name in current language.

You may request this feature enhancement in Issue Tracker

Update- @jmart 's answer is correct. https://stackoverflow.com/a/46670618/3410197

This is my solution:

public class AlarmReceiver extends BroadcastReceiver {

public static String NOTIFICATION_CHANNEL_ID = "notification-id";
public static String NOTIFICATION = "Notification";
public static String DESCRIPTION = "Channel description";

@TargetApi(26)
@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Log.e("RECEIVE", "received2");


    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);


    //PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent pi = PendingIntent.getActivity(context, intent.getIntExtra("NotifID", 1), new Intent(context, CalendarActivity.class),PendingIntent.FLAG_CANCEL_CURRENT);

    // get current locale
    String locale; // for getting locale
    locale = Locale.getDefault().getLanguage();
    if(locale.equalsIgnoreCase("sk")) {
        NOTIFICATION = "Notifikácia";
        DESCRIPTION = "Popis";
    }

        if (VERSION.SDK_INT >= 26) {
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION, NotificationManager.IMPORTANCE_HIGH);
            notificationChannel.setDescription(DESCRIPTION);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.GREEN);

            notificationChannel.setLockscreenVisibility(Notification.DEFAULT_SOUND);
            notificationChannel.setVibrationPattern(new long[]{1000, 500, 500, 200, 200, 200});
            notificationChannel.enableVibration(true);
            manager.createNotificationChannel(notificationChannel);

        }



        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID);
        builder.setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setWhen(System.currentTimeMillis())
            .setContentTitle(intent.getStringExtra("AppName"))
            .setContentText(intent.getStringExtra("lname"))
            .setSmallIcon(R.drawable.ic_launcher)
            ;

        //manager.notify(1,builder.build());
    manager.notify(intent.getIntExtra("NotifID", 1), builder.build());

}

}

Related