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 上正确显示的内容?

4

2 回答 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);

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

new NotificationCompat.Builder(this, channelId)
于 2017-08-31T14:02:21.813 回答
2

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

于 2017-06-23T14:35:33.567 回答