Android O: notification channel localization

I created a notification channel as follows:

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

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

+8
source share
4 answers

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

( ). .

+7

, . "String" , .

() , , , .

- @jmart . fooobar.com/questions/1019856/...

+2

, . - . , , . ( - .)

createNotificationChannel , , , .

, , , , . , , , . , , . , , .

, , :

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

, , . , :

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

:

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

}

}

0

All Articles