首页 > 解决方案 > 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)。我怎样才能改变重要性?

标签: javaandroid

解决方案


一旦您将频道提交给 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();
      }
}

另一种解决方案是创建一个新频道并使用该频道,但用户始终会看到您在 Android 设置中创建的所有频道


推荐阅读