我已经读到,对于前台服务的通知重要性至少应为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());

最佳答案

由于前台服务应该是用户关心的事情,因此它可以:

 /**
 * 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;

关于android - 通知重要性始终为前台服务的媒介,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52496550/

10-13 04:35