Android O 不显示前台服务通知

Android O not displaying foregrounded service notification

根据文档,我对应用程序进行了更改,以使用 context.startForegroundService(Intent) 在后台启动服务,然后像以前一样在服务中调用 startForeground

NotificationCompat.Builder notification = new 
NotificationCompat.Builder(getApplicationContext())
            .setSmallIcon(R.mipmap.icon)
            .setContentText("Content")
            .setOngoing(true)
            .setContentInfo("Info")
            .setContentTitle("Title");
startForeground(1, notification.build());

这会在 Android N 设备上正确显示通知,但在 Android O 设备上它不会显示通知,它只会在后台显示新的 " is 运行 ...点击以获取有关电池和数据使用情况的更多详细信息

在 Android 上正确显示通知是否缺少某些内容?

是,使用通道作为 NotificationCompat class 的构造函数参数。

上面的答案对我不起作用。您必须创建频道才能工作,然后传递频道 ID。

NotificationManager notificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

String channelId = "some_channel_id";
CharSequence channelName = "Some Channel";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);
notificationManager.createNotificationChannel(notificationChannel);

然后用相同的cannelId创建一个通知

new NotificationCompat.Builder(this, channelId)