From 07a36498e36039e85dbfd0cae33fe2b987939506 Mon Sep 17 00:00:00 2001 From: JamesChenX Date: Sun, 5 Jan 2025 08:32:55 +0800 Subject: [PATCH] Fix failed to find the push notification service provider to send notifications #1600 --- .../turms/plugin/push/NotificationPusher.java | 12 ++++++++---- .../push/core/PushNotificationManager.java | 19 ++++++++++++++----- .../plugin/push/core/smtp/SmtpSession.java | 2 +- .../plugin/push/property/ApnsProperties.java | 2 +- .../session/service/UserStatusService.java | 2 +- 5 files changed, 25 insertions(+), 12 deletions(-) diff --git a/turms-plugins/turms-plugin-push/src/main/java/im/turms/plugin/push/NotificationPusher.java b/turms-plugins/turms-plugin-push/src/main/java/im/turms/plugin/push/NotificationPusher.java index 1addf4b4b6..28dc0ee8a7 100644 --- a/turms-plugins/turms-plugin-push/src/main/java/im/turms/plugin/push/NotificationPusher.java +++ b/turms-plugins/turms-plugin-push/src/main/java/im/turms/plugin/push/NotificationPusher.java @@ -56,7 +56,8 @@ public class NotificationPusher extends TurmsExtension implements RequestHandler private UserService userService; private UserStatusService userStatusService; - private List deviceTokenFieldNames; + private Set deviceTokenFieldNames; + private Map deviceTokenFieldNameToServiceProvider; @Override protected Mono start() { @@ -65,7 +66,8 @@ protected Mono start() { userService = getContext().getBean(UserService.class); userStatusService = getContext().getBean(UserStatusService.class); - deviceTokenFieldNames = manager.getDeviceTokenFieldNames(); + deviceTokenFieldNameToServiceProvider = manager.getDeviceTokenFieldNameToServiceProvider(); + deviceTokenFieldNames = deviceTokenFieldNameToServiceProvider.keySet(); return Mono.empty(); } @@ -85,7 +87,9 @@ public Mono afterNotify( TurmsRequest request = result.notifications() .getFirst() .notification(); - if (request == null || offlineRecipientIds.isEmpty() || deviceTokenFieldNames.isEmpty()) { + if (request == null + || offlineRecipientIds.isEmpty() + || deviceTokenFieldNameToServiceProvider.isEmpty()) { return handlerResultMono; } // TODO: support other types @@ -149,7 +153,7 @@ private void sendNotification( for (Map.Entry detail : deviceDetails.entrySet()) { String providerStr = detail.getKey(); PushNotificationServiceProvider provider = - PushNotificationServiceProvider.get(providerStr); + deviceTokenFieldNameToServiceProvider.get(providerStr); if (provider == null) { continue; } diff --git a/turms-plugins/turms-plugin-push/src/main/java/im/turms/plugin/push/core/PushNotificationManager.java b/turms-plugins/turms-plugin-push/src/main/java/im/turms/plugin/push/core/PushNotificationManager.java index 427aa1db3e..45277ddd94 100644 --- a/turms-plugins/turms-plugin-push/src/main/java/im/turms/plugin/push/core/PushNotificationManager.java +++ b/turms-plugins/turms-plugin-push/src/main/java/im/turms/plugin/push/core/PushNotificationManager.java @@ -40,6 +40,7 @@ import im.turms.plugin.push.property.TemplateProperties; import im.turms.server.common.access.client.dto.constant.DeviceType; import im.turms.server.common.access.client.dto.request.TurmsRequest; +import im.turms.server.common.infra.collection.CollectionUtil; import im.turms.server.common.infra.exception.FeatureDisabledException; import im.turms.server.common.infra.lang.StringUtil; @@ -54,7 +55,7 @@ public class PushNotificationManager { private final ApnsSender apnsSender; private final FcmSender fcmSender; @Getter - private final List deviceTokenFieldNames; + private final Map deviceTokenFieldNameToServiceProvider; private final String localeFieldName; private final String fallbackLocale; private final boolean isApnsEnabled; @@ -91,14 +92,22 @@ public PushNotificationManager(PushNotificationProperties properties) { ? new FcmSender(templates, fcmProperties) : null; - List names = new ArrayList<>(2); + Map deviceTokenFieldNameToServiceProvider = + CollectionUtil.newMapWithExpectedSize(2); if (isApnsEnabled) { - names.add(apnsSender.getDeviceTokenFieldName()); + deviceTokenFieldNameToServiceProvider.put(apnsSender.getDeviceTokenFieldName(), + PushNotificationServiceProvider.APN); } if (isFcmEnabled) { - names.add(fcmSender.getDeviceTokenFieldName()); + String deviceTokenFieldName = fcmSender.getDeviceTokenFieldName(); + if (deviceTokenFieldNameToServiceProvider.putIfAbsent(deviceTokenFieldName, + PushNotificationServiceProvider.FCM) != null) { + throw new IllegalArgumentException( + "The device token field name of FCM and APNs must be different. Actual: " + + deviceTokenFieldName); + } } - deviceTokenFieldNames = names; + this.deviceTokenFieldNameToServiceProvider = deviceTokenFieldNameToServiceProvider; } public Mono sendNotification( diff --git a/turms-plugins/turms-plugin-push/src/main/java/im/turms/plugin/push/core/smtp/SmtpSession.java b/turms-plugins/turms-plugin-push/src/main/java/im/turms/plugin/push/core/smtp/SmtpSession.java index a33e807f9a..7baabfc710 100644 --- a/turms-plugins/turms-plugin-push/src/main/java/im/turms/plugin/push/core/smtp/SmtpSession.java +++ b/turms-plugins/turms-plugin-push/src/main/java/im/turms/plugin/push/core/smtp/SmtpSession.java @@ -24,9 +24,9 @@ import java.util.Collection; import java.util.List; import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; -import javax.annotation.Nullable; import javax.net.ssl.SSLEngine; import javax.net.ssl.TrustManagerFactory; +import jakarta.annotation.Nullable; import jakarta.validation.constraints.NotNull; import io.netty.buffer.ByteBuf; diff --git a/turms-plugins/turms-plugin-push/src/main/java/im/turms/plugin/push/property/ApnsProperties.java b/turms-plugins/turms-plugin-push/src/main/java/im/turms/plugin/push/property/ApnsProperties.java index 9135d782d9..9da96512e5 100644 --- a/turms-plugins/turms-plugin-push/src/main/java/im/turms/plugin/push/property/ApnsProperties.java +++ b/turms-plugins/turms-plugin-push/src/main/java/im/turms/plugin/push/property/ApnsProperties.java @@ -41,6 +41,6 @@ public class ApnsProperties extends ServiceProviderProperties { private boolean sandboxEnabled; - private String deviceTokenFieldName = "f"; + private String deviceTokenFieldName = "a"; } \ No newline at end of file diff --git a/turms-server-common/src/main/java/im/turms/server/common/domain/session/service/UserStatusService.java b/turms-server-common/src/main/java/im/turms/server/common/domain/session/service/UserStatusService.java index 8d7444a808..0e2c37c5bf 100644 --- a/turms-server-common/src/main/java/im/turms/server/common/domain/session/service/UserStatusService.java +++ b/turms-server-common/src/main/java/im/turms/server/common/domain/session/service/UserStatusService.java @@ -510,7 +510,7 @@ private Mono fetchNodeStatus(String nodeId) { */ public Mono>> fetchDeviceDetails( @NotEmpty Set userIds, - @NotEmpty List fields) { + @NotEmpty Collection fields) { try { Validator.notEmpty(userIds, "userIds"); Validator.notEmpty(fields, "fields");