Android O: Notification Channel localization
Asked Answered
T

4

16

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?

Tantara answered 20/9, 2017 at 8:55 Comment(1)
You can used SharedPreference for this.Woodnote
P
26

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.

Platen answered 10/10, 2017 at 15:32 Comment(2)
I don't know why you got down-voted, as this is correct. The only thing I would add that you need to use the same channel id when re-creating the channel with a new localised name.Bier
It is important to mention that we do not want to re-create the whole channel i.e delete and create, as it will show the channel as n deleted in notification settings, however we only want to update the channel name and description, using above method which is allowed by the api.Schroth
C
3

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
Christcross answered 1/2, 2018 at 18:29 Comment(0)
N
1

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://mcmap.net/q/723009/-android-o-notification-channel-localization

Nebulous answered 21/9, 2017 at 5:20 Comment(6)
Indeed, I was led to the same conclusion. Thanks.Jarvisjary
@AlécioCarvalho As explained on the official documentation: "You can call createNotificationChannel() and then submit the notification channel again to the notification manager to rename an existing notification channel, or update its description" (I just did it in my app). Link: developer.android.com/guide/topics/ui/notifiers/…Platen
Thanks @jmart, it's clear...the downside is that it will lose the current channel settings that the user had changed, forcing them to reconfigure it. So that is why I was wondering if there would be a better solution that would already deal with the localisation changes.Jarvisjary
@AlécioCarvalho None of the user settings are lost. Calling createNotificationChannel over an already existing channel only modifies the name and description of the channel (this is by design). Only users can modify their settings and only you can modify the name and description.Platen
@Platen is that so? The user changes on that channel won't be affected? humm is there a reference i can confirm that? on the docs it only refers to the renaming and change of description, not about the other settings (e.g. the sound uri, vibration settings, etc) See what i mean?Jarvisjary
@AlécioCarvalho "All other fields are ignored for channels that already exist" Link: developer.android.com/reference/android/app/…Platen
R
0

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());

}

}

Rodin answered 14/5, 2019 at 4:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.