简体   繁体   English

Android 通知重要性无法更改

[英]Android notification importance cannot be changed

Code Summary:代码摘要:

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() returns always 3(IMPORTANCE_DEFAULT). manager.getImportance()总是返回 3(IMPORTANCE_DEFAULT)。 How can I change importance?我怎样才能改变重要性?

Once you submit the channel to the NotificationManager, you cannot change the importance level.将频道提交给 NotificationManager 后,您将无法更改重要性级别。 However, the user can change their preferences for your app's channels at any time.但是,用户可以随时更改他们对应用程序频道的偏好。

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

The solution is to guide user to notification settings and allow him to modify your channel.解决方案是引导用户进行通知设置,并允许他修改您的频道。 To open a channel in android settings there is an intent:要在 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();
      }
}

Another solution is to create a new channel and use that channel but user always sees all channels that you have created in Android Settings另一种解决方案是创建一个新频道并使用该频道,但用户始终会看到您在 Android 设置中创建的所有频道

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM