首页 > 解决方案 > setOngoing(true) 在 Oreo+ 通知中不起作用

问题描述

我正在使用 channel_id 创建一个自定义通知并将其作为前台启动。我正在播放背景音乐。 我的问题是当用户滑动通知时它消失了。我也有 setOngoing(true)。我希望在播放音乐时保留通知。

注意:它只发生在 Oreo+ 中。在较低版本中它工作正常。

public void CustomNotification() {
    remoteViews = new RemoteViews(getPackageName(), R.layout.player_noti_layout);
    notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel("default",
                getString(R.string.player_channel),
                NotificationManager.IMPORTANCE_LOW);
        channel.enableVibration(true);
        channel.enableLights(true);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        channel.setDescription("Notification, Play/pause & Next/Prev");
        notificationmanager.createNotificationChannel(channel);
    }
    builder = new NotificationCompat.Builder(this, "default");
    Notification foregroundNote;

    // Set icon
    foregroundNote = builder.setSmallIcon(R.drawable.ic_radio)
           .setLargeIcon(R.drawable.cool_1)
            // Set ticker message
            .setTicker(getResources().getString(R.string.app_name))
            // Dismiss notification
            .setAutoCancel(false)
            .setOngoing(true)
          .setContent(remoteViews)
            .setContentTitle("title").
                    setContentText("text")
            .build();
    foregroundNote.flags |= Notification.FLAG_ONGOING_EVENT;
    foregroundNote.flags |= Notification.FLAG_NO_CLEAR;


    foregroundNote.contentView = remoteViews;


    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        startForeground(2, foregroundNote);
    } else
        notificationmanager.notify(2, foregroundNote);


}

标签: notificationsandroid-8.1-oreo

解决方案


简而言之,这是由于碎片化和定制化造成的。可能该电话具有某种设置来配置该行为。正如有人有一天所说:有些手机只是-垃圾-。不要浪费时间尝试为每部可能的手机解决问题。


推荐阅读