Android通知的重要性无法更改



代码摘要:

NotificationCompat.Builder notification;
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
...
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_TITLE, importance);
manager.createNotificationChannel(channel);
notification.setChannelId(CHANNEL_ID);
manager.notify(notificationId, 0, notification.build());

manager.getImportance()总是返回3(IMPORTANCE_DEFAULT(。如何更改重要性?

一旦您将通道提交给NotificationManager,您就无法更改重要性级别但是,用户可以更改您的应用程序频道的首选项

https://developer.android.com/training/notify-user/channels

解决方案是引导用户进行通知设置,并允许他修改您的频道。要在android设置中打开一个频道,有一个意图:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
final Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, getActivity().getPackageName());
intent.putExtra(Settings.EXTRA_CHANNEL_ID, channelID);
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivity(intent);
} else {
Toast.makeText(getActivity(), R.string.not_found, Toast.LENGTH_LONG).show();
}
}

另一个解决方案是创建一个新频道并使用该频道,但用户总是看到你在安卓设置中创建的所有频道

最新更新