Mark as read - extract buildIntentForAction function

Signed-off-by: Dariusz Olszewski <starypatyk@users.noreply.github.com>
This commit is contained in:
Dariusz Olszewski 2022-07-10 23:20:01 +02:00 committed by Marcel Hibbe (Rebase PR Action)
parent 7243142676
commit c3a31da6a9

View File

@ -417,22 +417,16 @@ public class NotificationWorker extends Worker {
notificationBuilder.setStyle(getStyle(person.build(), style)); notificationBuilder.setStyle(getStyle(person.build(), style));
} }
private void addMarkAsReadAction(NotificationCompat.Builder notificationBuilder, int systemNotificationId) { private PendingIntent buildIntentForAction(Class<?> cls, int systemNotificationId, int messageId) {
String label = context.getResources().getString(R.string.nc_mark_as_read); Intent actualIntent = new Intent(context, cls);
// Build a PendingIntent for the reply action
Intent actualIntent = new Intent(context, MarkAsReadReceiver.class);
// NOTE - systemNotificationId is an internal ID used on the device only. // NOTE - systemNotificationId is an internal ID used on the device only.
// It is NOT the same as the notification ID used in communication with the server. // It is NOT the same as the notification ID used in communication with the server.
actualIntent.putExtra(BundleKeys.INSTANCE.getKEY_INTERNAL_USER_ID(), Objects.requireNonNull(signatureVerification.getUserEntity()).getId());
actualIntent.putExtra(BundleKeys.INSTANCE.getKEY_SYSTEM_NOTIFICATION_ID(), systemNotificationId); actualIntent.putExtra(BundleKeys.INSTANCE.getKEY_SYSTEM_NOTIFICATION_ID(), systemNotificationId);
actualIntent.putExtra(BundleKeys.INSTANCE.getKEY_INTERNAL_USER_ID(),
Objects.requireNonNull(signatureVerification.getUserEntity()).getId());
actualIntent.putExtra(BundleKeys.INSTANCE.getKEY_ROOM_TOKEN(), decryptedPushMessage.getId()); actualIntent.putExtra(BundleKeys.INSTANCE.getKEY_ROOM_TOKEN(), decryptedPushMessage.getId());
if (decryptedPushMessage.getNotificationId() != null) { actualIntent.putExtra(BundleKeys.KEY_MESSAGE_ID, messageId);
// TODO - improve parsing when server returns unexpected objectId
int messageId = Integer.parseInt(decryptedPushMessage.getObjectId().split("/")[1]);
actualIntent.putExtra(BundleKeys.KEY_MESSAGE_ID, messageId);
}
int intentFlag; int intentFlag;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
@ -440,8 +434,18 @@ public class NotificationWorker extends Worker {
} else { } else {
intentFlag = PendingIntent.FLAG_UPDATE_CURRENT; intentFlag = PendingIntent.FLAG_UPDATE_CURRENT;
} }
PendingIntent pendingIntent =
PendingIntent.getBroadcast(context, systemNotificationId, actualIntent, intentFlag); return PendingIntent.getBroadcast(context, systemNotificationId, actualIntent, intentFlag);
}
private void addMarkAsReadAction(NotificationCompat.Builder notificationBuilder, int systemNotificationId) {
String label = context.getResources().getString(R.string.nc_mark_as_read);
// TODO - improve parsing when server returns unexpected objectId
int messageId = Integer.parseInt(decryptedPushMessage.getObjectId().split("/")[1]);
// Build a PendingIntent for the mark as read action
PendingIntent pendingIntent = buildIntentForAction(MarkAsReadReceiver.class, systemNotificationId, messageId);
NotificationCompat.Action action = NotificationCompat.Action action =
new NotificationCompat.Action.Builder(R.drawable.ic_eye, label, pendingIntent) new NotificationCompat.Action.Builder(R.drawable.ic_eye, label, pendingIntent)
@ -461,22 +465,7 @@ public class NotificationWorker extends Worker {
.build(); .build();
// Build a PendingIntent for the reply action // Build a PendingIntent for the reply action
Intent actualIntent = new Intent(context, DirectReplyReceiver.class); PendingIntent replyPendingIntent = buildIntentForAction(DirectReplyReceiver.class, systemNotificationId, 0);
// NOTE - systemNotificationId is an internal ID used on the device only.
// It is NOT the same as the notification ID used in communication with the server.
actualIntent.putExtra(BundleKeys.INSTANCE.getKEY_INTERNAL_USER_ID(), Objects.requireNonNull(signatureVerification.getUserEntity()).getId());
actualIntent.putExtra(BundleKeys.INSTANCE.getKEY_SYSTEM_NOTIFICATION_ID(), systemNotificationId);
actualIntent.putExtra(BundleKeys.INSTANCE.getKEY_ROOM_TOKEN(), decryptedPushMessage.getId());
int intentFlag;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
intentFlag = PendingIntent.FLAG_MUTABLE|PendingIntent.FLAG_UPDATE_CURRENT;
} else {
intentFlag = PendingIntent.FLAG_UPDATE_CURRENT;
}
PendingIntent replyPendingIntent =
PendingIntent.getBroadcast(context, systemNotificationId, actualIntent, intentFlag);
NotificationCompat.Action replyAction = NotificationCompat.Action replyAction =
new NotificationCompat.Action.Builder(R.drawable.ic_reply, replyLabel, replyPendingIntent) new NotificationCompat.Action.Builder(R.drawable.ic_reply, replyLabel, replyPendingIntent)