0

我的应用程序使用在 6.0 及更低版本上运行良好的前台服务通知。在某些情况下,我使用此答案从状态栏和锁定屏幕中隐藏图标(我的应用程序有两种模式,一种是应用程序不需要永久通知用户的“后台”模式,另一种是“高度警报”模式我为此通知使用最高优先级)。

正如我所说,这在 Android 6.0 及更低版本上运行良好。但是在 Nougat 上,即使我的应用程序针对 API 级别 24(最低 API 级别 17)并使用所有内容的最新版本,此通知仍显示为正常通知。

我已经搜索了可能的解决方案,但由于 7.0 的相对新鲜度,没有太多信息。我注意到,即使在 7.0(其中之一是 AccuBattery)上,某些应用程序确实设法将其通知显示为最低优先级。

我使用以下代码来创建通知:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle(getString(R.string.app_name));
builder.setOngoing(true);
builder.setDefaults(Notification.DEFAULT_ALL);
builder.setSmallIcon(R.drawable.ic_stat_onesignal_default);
builder.setColor(getResources().getColor(R.color.notification_color));

Intent alertIntent = new Intent(this, SplashActivity.class);
alertIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, alertIntent, 0);
builder.setContentIntent(pendingIntent);
switch (mode) {
    case ALERT:
        builder.setContentText("Alert is active");
        builder.setPriority(Notification.PRIORITY_MAX);
        break;
    case BACKGROUND:
    default:
        builder.setContentText("Background service running");
        builder.setPriority(Notification.PRIORITY_MIN);
        break;
}
4

0 回答 0