Browse Source

Fix merge issues

pull/8522/head
ViacheslavKlimov 3 years ago
parent
commit
fa7fcda2c7
  1. 67
      application/src/main/java/org/thingsboard/server/service/notification/channels/MobileNotificationChannel.java

67
application/src/main/java/org/thingsboard/server/service/notification/channels/MobileNotificationChannel.java

@ -17,8 +17,6 @@ package org.thingsboard.server.service.notification.channels;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.google.auth.oauth2.GoogleCredentials; import com.google.auth.oauth2.GoogleCredentials;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.firebase.FirebaseApp; import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions; import com.google.firebase.FirebaseOptions;
import com.google.firebase.messaging.FirebaseMessaging; import com.google.firebase.messaging.FirebaseMessaging;
@ -35,7 +33,6 @@ import org.thingsboard.server.common.data.notification.settings.MobileNotificati
import org.thingsboard.server.common.data.notification.settings.NotificationSettings; import org.thingsboard.server.common.data.notification.settings.NotificationSettings;
import org.thingsboard.server.common.data.notification.template.MobileDeliveryMethodNotificationTemplate; import org.thingsboard.server.common.data.notification.template.MobileDeliveryMethodNotificationTemplate;
import org.thingsboard.server.dao.notification.NotificationSettingsService; import org.thingsboard.server.dao.notification.NotificationSettingsService;
import org.thingsboard.server.service.executors.ExternalCallExecutorService;
import org.thingsboard.server.service.notification.NotificationProcessingContext; import org.thingsboard.server.service.notification.NotificationProcessingContext;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -45,52 +42,48 @@ import java.util.Optional;
@RequiredArgsConstructor @RequiredArgsConstructor
public class MobileNotificationChannel implements NotificationChannel<User, MobileDeliveryMethodNotificationTemplate> { public class MobileNotificationChannel implements NotificationChannel<User, MobileDeliveryMethodNotificationTemplate> {
private final ExternalCallExecutorService executor;
private final NotificationSettingsService notificationSettingsService; private final NotificationSettingsService notificationSettingsService;
@Override @Override
public ListenableFuture<Void> sendNotification(User recipient, MobileDeliveryMethodNotificationTemplate processedTemplate, NotificationProcessingContext ctx) { public void sendNotification(User recipient, MobileDeliveryMethodNotificationTemplate processedTemplate, NotificationProcessingContext ctx) throws Exception {
String fcmToken = Optional.ofNullable(recipient.getAdditionalInfo()) String fcmToken = Optional.ofNullable(recipient.getAdditionalInfo())
.map(info -> info.get("fcmToken")).filter(JsonNode::isTextual).map(JsonNode::asText) .map(info -> info.get("fcmToken")).filter(JsonNode::isTextual).map(JsonNode::asText)
.orElse(null); .orElse(null);
if (StringUtils.isEmpty(fcmToken)) { if (StringUtils.isEmpty(fcmToken)) {
return Futures.immediateFailedFuture(new RuntimeException("User doesn't have the mobile app installed")); throw new RuntimeException("User doesn't have the mobile app installed");
} }
MobileNotificationDeliveryMethodConfig config = ctx.getDeliveryMethodConfig(NotificationDeliveryMethod.MOBILE); MobileNotificationDeliveryMethodConfig config = ctx.getDeliveryMethodConfig(NotificationDeliveryMethod.MOBILE);
return executor.submit(() -> { FirebaseOptions firebaseOptions = FirebaseOptions.builder()
FirebaseOptions firebaseOptions = FirebaseOptions.builder() .setCredentials(GoogleCredentials.fromStream(IOUtils.toInputStream(config.getFirebaseServiceAccountCredentials(), StandardCharsets.UTF_8)))
.setCredentials(GoogleCredentials.fromStream(IOUtils.toInputStream(config.getFirebaseServiceAccountCredentials(), StandardCharsets.UTF_8))) .build();
.build(); String appName = ctx.getTenantId().toString();
String appName = ctx.getTenantId().toString();
FirebaseApp firebaseApp = FirebaseApp.getApps().stream() FirebaseApp firebaseApp = FirebaseApp.getApps().stream()
.filter(app -> app.getName().equals(appName)) .filter(app -> app.getName().equals(appName))
.findFirst().orElseGet(() -> { .findFirst().orElseGet(() -> {
try { try {
return FirebaseApp.initializeApp(firebaseOptions, appName); return FirebaseApp.initializeApp(firebaseOptions, appName);
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
return FirebaseApp.getInstance(appName); return FirebaseApp.getInstance(appName);
} }
}); });
FirebaseMessaging firebaseMessaging; FirebaseMessaging firebaseMessaging;
try { try {
firebaseMessaging = FirebaseMessaging.getInstance(firebaseApp); firebaseMessaging = FirebaseMessaging.getInstance(firebaseApp);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// because of concurrency issues: FirebaseMessaging.getInstance lazily loads FirebaseMessagingService // because of concurrency issues: FirebaseMessaging.getInstance lazily loads FirebaseMessagingService
firebaseMessaging = FirebaseMessaging.getInstance(firebaseApp); firebaseMessaging = FirebaseMessaging.getInstance(firebaseApp);
} }
Message message = Message.builder() Message message = Message.builder()
.setNotification(Notification.builder() .setNotification(Notification.builder()
.setTitle(processedTemplate.getSubject()) .setTitle(processedTemplate.getSubject())
.setBody(processedTemplate.getBody()) .setBody(processedTemplate.getBody())
.build()) .build())
.setToken(fcmToken) .setToken(fcmToken)
.build(); .build();
firebaseMessaging.send(message); firebaseMessaging.send(message);
return null;
});
} }
@Override @Override

Loading…
Cancel
Save