我创建了一个这样的通知频道:
NotificationChannel channel = new NotificationChannel(CHANNEL_ID_FOOBAR, getContext().getString(R.string.notification_channel_foobar), NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
我为 R.string.notification_channel_foobar 提供了不同的翻译,并且使用创建时使用的语言创建了频道,因此如果我最终更改了设备的语言,那么频道将保留旧语言。有没有办法克服这个问题,或者这是一个限制,即通过设计?
答案 0 :(得分:7)
要将新语言应用于您的通知频道,您需要在应用中收听ACTION_LOCALE_CHANGED广播,然后在接收方中再次调用createNotificationChannel。
重新创建频道会将您的字符串更新为新语言(其他频道功能都不会被修改)。您可以在documentation。
中查看答案 1 :(得分:2)
创建频道后,您无法对其进行任何更改。当我们为频道名称提供“字符串”时,系统将始终将该名称表示给用户。
您可以尝试的一件坏事是删除旧频道并创建另一个具有当前语言名称的频道。
您可以在Issue Tracker
中请求此功能增强功能<强>更新强> - @jmart的回答是正确的。 https://stackoverflow.com/a/46670618/3410197
答案 2 :(得分:0)
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
答案 3 :(得分: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());
}
}