Android O不显示前台服务通知

4

根据文档,我已经在应用程序中进行了更改,使用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设备上,它不会显示通知,只会显示新的“正在后台运行……点击以获取有关电池和数据使用情况的更多详细信息”。

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

2个回答

3
上面的答案对我没用。你必须先创建通道才能正常工作,然后传递通道 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);

然后使用相同的通道ID创建一个通知

new NotificationCompat.Builder(this, channelId)

2
是的,可以将通道作为 NotificationCompat 类的构造函数参数使用。

你能详细解释一下你的意思吗? - Ryan
哦,你是指beta支持库中的新构造函数?不幸的是,由于外部限制,我无法使用beta库。我只是为用户编写一个未来可用的库,当他们转移到v26时,我将不得不更新库。 - Ryan
是的,请使用v26.0.0-beta2。 - greywolf82
1
这在O中不起作用,即使我创建了通道,我的通知也不会在O中显示。它在所有其他版本上都可以显示。我甚至确保调用startForegroundService而不是startService。 - Kenny

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接