自定义通知未显示在锁定屏幕oreo上

时间:2018-10-04 16:04:05

标签: android android-8.0-oreo lockscreen

我正在构建具有远程视图的Notification。我给了NotificationCompat.VISIBILITY_PUBLIC。但通知未显示在Oreo的锁定屏幕上。

我的compileSdkVersion和targetSdkVersion是27

 remoteViews = new RemoteViews(getPackageName(), R.layout.player_noti_layout);
 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.setDescription("Notification, Play/pause & Next/Prev");
        channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        notificationmanager.createNotificationChannel(channel);
    }
    builder = new NotificationCompat.Builder(this, "default");
    Notification foregroundNote;
    // Set Icon
    foregroundNote = builder.setSmallIcon(R.drawable.ic_radio)
            .setTicker(getResources().getString(R.string.app_name))
            .setAutoCancel(false).setOngoing(true)
            .setContent(remoteViews)
            .setContentTitle("app name").setContentText("").setWhen(0).setPriority(NotificationCompat.PRIORITY_MAX)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) 
            .build();

帮助表示赞赏!!!谢谢

2 个答案:

答案 0 :(得分:0)

尝试一下:

Notification.Builder.setVisibility(Notification.VISIBILITY_PUBLIC);

答案 1 :(得分:0)

我发现对我有用的是在您的通知频道中,您将LockscreenVisibility更改为此

channel.LockscreenVisibility = NotificationVisibility.Public;

在您的NotificationCompat.Builder中

.setVisibility(NotificationCompat.VisibilityPublic)

您似乎确实使用了稍微不同的语法,因为我必须大写.SetVisibility
希望对您有所帮助。

PS:请记住,您的手机可能不允许锁屏通知。这也发生在代码可以正常工作的地方,但我的手机仍未显示通知。 ;)