15

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?

TofferJ
  • 4,216
  • 1
  • 31
  • 43
Alécio Carvalho
  • 12,697
  • 5
  • 63
  • 71

4 Answers4

21

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.

jmart
  • 2,313
  • 15
  • 30
  • 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. – ZoltanF Nov 24 '17 at 12:48
  • 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. – Himanshu Walia Nov 23 '19 at 17:45
2

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
steliosf
  • 2,921
  • 2
  • 29
  • 44
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://stackoverflow.com/a/46670618/3410197

Arnav M.
  • 2,500
  • 1
  • 24
  • 40
  • 1
    Indeed, I was led to the same conclusion. Thanks. – Alécio Carvalho Oct 10 '17 at 16:51
  • @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: https://developer.android.com/guide/topics/ui/notifiers/notifications.html#UpdateChannel – jmart Oct 10 '17 at 21:38
  • 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. – Alécio Carvalho Oct 10 '17 at 21:50
  • 1
    @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. – jmart Oct 10 '17 at 21:54
  • @jmart 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? – Alécio Carvalho Oct 10 '17 at 22:04
  • @AlécioCarvalho "All other fields are ignored for channels that already exist" Link: https://developer.android.com/reference/android/app/NotificationManager.html#createNotificationChannel(android.app.NotificationChannel) – jmart Oct 10 '17 at 22:11
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());

}

}

Stefan Cizmar
  • 51
  • 1
  • 3