我该如何仅针对直接答复发送的输入来更新通知,而不隐藏收到通知时出现的第一条消息。
我尝试用不同的参数创建相同的方法。
我需要采用其他方法来创建通知吗?
我知道我可以在SocketService.setAndUpdateNotificationMessage(context, responseHistory.toArray(history));
处传递消息和用户名,但是我认为这不是很好的实现。
使用不同的参数重写相同的方法
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");
}