Как обновить уведомление с прямым ответом, не потеряв первое полученное сообщение - PullRequest
0 голосов
/ 21 мая 2019

Как я могу обновить уведомление только для ввода, отправляемого с прямым ответом, но не для того, чтобы скрыть первое сообщение, которое приходит при получении уведомления. Я пытался создать один и тот же метод с разными аргументами. Нужно ли сделать другой способ создания уведомления? Я знаю, что могу передать сообщение и имя пользователя здесь SocketService.setAndUpdateNotificationMessage(context, responseHistory.toArray(history));, но я думаю, что это не очень хорошая реализация.

Created Notification

After Reply, Updated Notification

Переписываем один и тот же метод с разными аргументами

public static void setAndUpdateNotificationMessage(Context context, CharSequence message, CharSequence title) {
        setAndUpdateNotificationMessage(context, null, message, title);
    }

Способ создания и обновления уведомлений.

public static void setAndUpdateNotificationMessage(Context context, CharSequence[] history, CharSequence message, CharSequence title) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_stat_name)
                .setContentTitle(title)
                .setContentText(message)
                .addAction(action)
                .setGroup(GROUP_KEY_WORK_EMAIL)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);

// notificationId is a unique int for each notification that you must define
        notificationManager.notify(1, builder.build());

        if(history != null) {
            builder.setRemoteInputHistory(history);
        }
        notificationManager.notify(1, builder.build());
}

Вызов метода для создания уведомления

setAndUpdateNotificationMessage(getApplicationContext(), message,username);

Вызов метода для обновления уведомлений после действия прямого ответа

if (remoteInput != null) {
            responseHistory.add(0, replyMessage);

        }
        if(!responseHistory.isEmpty()) {
            CharSequence[] history = new CharSequence[responseHistory.size()];
            SocketService.setAndUpdateNotificationMessage(context, responseHistory.toArray(history));
        } else {
            SocketService.setAndUpdateNotificationMessage(context,null, replyMessage, "Me");
        }
...