Уведомление исчезает при выходе из приложения - PullRequest
0 голосов
/ 25 апреля 2020

Приложение имеет реализацию закрепленных уведомлений. В Android 9 при закрытии приложения это уведомление исчезает, а в Android 5.1 оно, как и должно быть, остается в панели уведомлений. Я не понимаю, почему это происходит. Как сделать так, чтобы уведомление не исчезало при выходе из приложения?

Вот код реализации уведомления:

NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    int ID_NOTIFY = info.getId();

    String CHANNEL_ID = "channel-1";
    String CHANNEL_NAME = "channel-name";

    Intent intent = new Intent(context, MainActivity.class);
    PendingIntent pi = PendingIntent.getActivity(context, 1, intent, 0);

    Intent reminderDeletor = new Intent(context, DeleteNotifyReminder.class);
    reminderDeletor.putExtra("id", info.getId());
    PendingIntent pird = PendingIntent.getBroadcast(context, info.getId(), reminderDeletor, 0);
    Notification.Action actionDelete = new Notification.Action(R.drawable.ic_delete, getString(R.string.delete), pird);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        int importanse = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, importanse);
        Objects.requireNonNull(manager).createNotificationChannel(channel);

        Notification.Builder builderCompat = new Notification.Builder(context, CHANNEL_ID)
                .setContentTitle(getString(R.string.app_name))
                .setContentIntent(pi)
                .setSmallIcon(R.drawable.notify_remind_icon)
                .setContentText(info.getRemind())
                .setOngoing(true)
                .setChannelId(CHANNEL_ID)
                .addAction(actionDelete);

        if (needColor) builderCompat.setColor(color);

        if (info.getRemind().length() > 70) builderCompat.setStyle(new Notification.BigTextStyle().bigText(info.getRemind().substring(0, 70)));
        else builderCompat.setStyle(new Notification.BigTextStyle().bigText(info.getRemind()));

        manager.notify(ID_NOTIFY, builderCompat.build());
    }else {
        Notification.Builder builder = new Notification.Builder(context)
                .setContentTitle(getString(R.string.app_name))
                .setContentIntent(pi)
                .setSmallIcon(R.drawable.notify_remind_icon)
                .setContentText(info.getRemind())
                .setOngoing(true)
                .addAction(R.drawable.ic_delete, getString(R.string.delete), pird);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && needColor) {
            builder.setColor(color);
        }

        String text = info.getRemind();

        if (text.length() > 70) builder.setStyle(new Notification.BigTextStyle().bigText(text.substring(0, 70)));
        else builder.setStyle(new Notification.BigTextStyle().bigText(text));

        Objects.requireNonNull(manager).notify(ID_NOTIFY, builder.build());
    }
...