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

问题描述 投票:0回答:2

我正在使用远程视图构建通知。我已经给了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();

帮助赞赏!!!谢谢

android android-8.0-oreo lockscreen
2个回答
0
投票

试试这个:

Notification.Builder.setVisibility(Notification.VISIBILITY_PUBLIC);

0
投票

我发现为我工作的是在您的notificationchannel中,您将LockscreenVisibility更改为此

channel.LockscreenVisibility = NotificationVisibility.Public;

并在您的NotificationCompat.Builder中

.setVisibility(NotificationCompat.VisibilityPublic)

你似乎使用了一个稍微不同的语法,因为我必须大写.SetVisibility 我希望这仍然有帮助。

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

© www.soinside.com 2019 - 2024. All rights reserved.