Уведомление службы переднего плана на экране блокировки - PullRequest
0 голосов
/ 07 марта 2020

Я пытаюсь получить обслуживание переднего плана. моя проблема в том, что мое уведомление не появляется на экране блокировки. Я пытаюсь установить видимость для PUBLI C, но она не работает.

@RequiresApi(api = Build.VERSION_CODES.O)
private String createNotificationChannel() {

    NotificationChannel notificationChannel =
            new NotificationChannel(
                    "channelId",
                    "channelName",
                    NotificationManager.IMPORTANCE_DEFAULT);
    notificationChannel.setDescription("channelDescription");

    notificationChannel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC);

    NotificationManagerCompat service = NotificationManagerCompat.from(getBaseContext());
    service.createNotificationChannel(notificationChannel);

    return "myChannelId";
}

public void startForeground() {
    Intent notificationIntent = new Intent(this, HomeActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    String channelID = "";
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        channelID = createNotificationChannel();
    }

    Notification notification = new NotificationCompat.Builder(this, channelID)
            .setContentTitle(title)
            .setContentText(content)
            .setShowWhen(false)
            .setSmallIcon(R.drawable.ic_notification_small_icon)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setContentIntent(pendingIntent)
            .build();

    startForeground("notificationId", notification);
}

1 Ответ

0 голосов
/ 07 марта 2020
notification.setOngoing(true)

делает уведомление видимым на экране блокировки.

Код

Notification notification = new NotificationCompat.Builder(this, channelID)
.setContentTitle(title)
.setContentText(content)
.setShowWhen(false)
.setSmallIcon(R.drawable.ic_notification_small_icon)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentIntent(pendingIntent)
.setOngoing(true)
.build();

, если это не работает, проверьте настройки устройства .

{ ссылка }

...