java - 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();
}
}
另一种解决方案是创建一个新频道并使用该频道,但用户始终会看到您在 Android 设置中创建的所有频道
推荐阅读
- amazon-web-services - AWS Lambda - 使用 .NET Core 构建无服务器 API
- android - 将谷歌地图放入 AppBarLayout
- java - 如何确定一个进程是否在 Java 中正确终止
- sql - 从 SQL 比较中排除隐藏字符
- angular - 如何在部署 nginx 中强制更新 Web 应用程序
- c# - NLog LoggerFactory 中的 TypeInitializationException
- r - R使用函数循环遍历多个子组
- android - 从自定义视图缩放形状坐标以适应不同的屏幕尺寸
- c# - 如何在 UWP 应用中使用 Microsoft Graph 自动获取新电子邮件
- c - 在 ARM Cortex M4 中可以安全地省略 DMB 指令吗