Skip to content

Commit

Permalink
Fix failed to find the push notification service provider to send not…
Browse files Browse the repository at this point in the history
…ifications #1600
  • Loading branch information
JamesChenX committed Jan 5, 2025
1 parent 7213557 commit 07a3649
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public class NotificationPusher extends TurmsExtension implements RequestHandler
private UserService userService;
private UserStatusService userStatusService;

private List<String> deviceTokenFieldNames;
private Set<String> deviceTokenFieldNames;
private Map<String, PushNotificationServiceProvider> deviceTokenFieldNameToServiceProvider;

@Override
protected Mono<Void> start() {
Expand All @@ -65,7 +66,8 @@ protected Mono<Void> start() {
userService = getContext().getBean(UserService.class);
userStatusService = getContext().getBean(UserStatusService.class);

deviceTokenFieldNames = manager.getDeviceTokenFieldNames();
deviceTokenFieldNameToServiceProvider = manager.getDeviceTokenFieldNameToServiceProvider();
deviceTokenFieldNames = deviceTokenFieldNameToServiceProvider.keySet();
return Mono.empty();
}

Expand All @@ -85,7 +87,9 @@ public Mono<RequestHandlerResult> 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
Expand Down Expand Up @@ -149,7 +153,7 @@ private void sendNotification(
for (Map.Entry<String, String> detail : deviceDetails.entrySet()) {
String providerStr = detail.getKey();
PushNotificationServiceProvider provider =
PushNotificationServiceProvider.get(providerStr);
deviceTokenFieldNameToServiceProvider.get(providerStr);
if (provider == null) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -54,7 +55,7 @@ public class PushNotificationManager {
private final ApnsSender apnsSender;
private final FcmSender fcmSender;
@Getter
private final List<String> deviceTokenFieldNames;
private final Map<String, PushNotificationServiceProvider> deviceTokenFieldNameToServiceProvider;
private final String localeFieldName;
private final String fallbackLocale;
private final boolean isApnsEnabled;
Expand Down Expand Up @@ -91,14 +92,22 @@ public PushNotificationManager(PushNotificationProperties properties) {
? new FcmSender(templates, fcmProperties)
: null;

List<String> names = new ArrayList<>(2);
Map<String, PushNotificationServiceProvider> 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<Void> sendNotification(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ public class ApnsProperties extends ServiceProviderProperties {

private boolean sandboxEnabled;

private String deviceTokenFieldName = "f";
private String deviceTokenFieldName = "a";

}
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ private Mono<NodeStatus> fetchNodeStatus(String nodeId) {
*/
public Mono<Map<Long, Map<String, String>>> fetchDeviceDetails(
@NotEmpty Set<Long> userIds,
@NotEmpty List<String> fields) {
@NotEmpty Collection<String> fields) {
try {
Validator.notEmpty(userIds, "userIds");
Validator.notEmpty(fields, "fields");
Expand Down

0 comments on commit 07a3649

Please sign in to comment.