通知重要性始终为前台服务的媒介

时间:2018-09-25 10:57:50

标签: android service android-8.0-oreo

我已经读到,对于前台服务的通知重要性至少应为LOW,但是当我尝试将重要性更改为LOW或HIGH时,它将被忽略,并且通知仍将保持MEDIUM重要性。为什么以及如何解决?

代码段:

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            String id = SYNC_CHANNEL;
            String name = getApplicationContext().getResources().getString(R.string.sync_data);
            String description = getApplicationContext().getResources().getString(R.string.sync_progress);
            int importance = NotificationManager.IMPORTANCE_LOW; // Setting importance

            NotificationChannel channel = new NotificationChannel(id, name, importance);
            channel.setDescription(description);
            channel.enableLights(false);
            channel.enableVibration(false);

            if (notificationManager == null) {
                return;
            }
            notificationManager.createNotificationChannel(channel);

            NotificationCompat.Builder b = new NotificationCompat.Builder(getApplicationContext(), id);
            b.setSmallIcon(R.drawable.ic_refresh)
                    .setContentTitle(getApplicationContext().getResources().getString(R.string.app_name))
                    .setContentText(getApplicationContext().getResources().getString(R.string.sync_data))
                    .setOngoing(true);

            startForeground(1547, b.build());

1 个答案:

答案 0 :(得分:-1)

由于前台服务应该是用户关心的事情,所以它做到了:

 /**
 * Min notification importance: only shows in the shade, below the fold.  This should
 * not be used with {@link Service#startForeground(int, Notification) Service.startForeground}
 * since a foreground service is supposed to be something the user cares about so it does
 * not make semantic sense to mark its notification as minimum importance.  If you do this
 * as of Android version {@link android.os.Build.VERSION_CODES#O}, the system will show
 * a higher-priority notification about your app running in the background.
 */
public static final int IMPORTANCE_MIN = 1;