Allow missing push notification keys

This commit is contained in:
Tim
2025-08-04 21:47:59 +08:00
parent 232ce26246
commit 7368fa77ae

View File

@@ -23,14 +23,22 @@ public class PushNotificationService {
private final PushService pushService;
public PushNotificationService(PushSubscriptionRepository subscriptionRepository,
@Value("${app.webpush.public-key}") String publicKey,
@Value("${app.webpush.private-key}") String privateKey) throws GeneralSecurityException {
@Value("${app.webpush.public-key:}") String publicKey,
@Value("${app.webpush.private-key:}") String privateKey) throws GeneralSecurityException {
this.subscriptionRepository = subscriptionRepository;
Security.addProvider(new BouncyCastleProvider());
this.pushService = new PushService(publicKey, privateKey);
if (publicKey != null && !publicKey.isBlank() && privateKey != null && !privateKey.isBlank()) {
Security.addProvider(new BouncyCastleProvider());
this.pushService = new PushService(publicKey, privateKey);
} else {
this.pushService = null;
}
}
public void sendNotification(User user, String payload) {
if (pushService == null) {
log.warn("Push notifications are disabled because VAPID keys are not configured.");
return;
}
List<PushSubscription> subs = subscriptionRepository.findByUser(user);
for (PushSubscription sub : subs) {
try {