|
|
@ -15,54 +15,65 @@ |
|
|
*/ |
|
|
*/ |
|
|
package org.thingsboard.server.service.notification.channels; |
|
|
package org.thingsboard.server.service.notification.channels; |
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode; |
|
|
import com.google.firebase.messaging.FirebaseMessagingException; |
|
|
|
|
|
import com.google.firebase.messaging.MessagingErrorCode; |
|
|
import lombok.RequiredArgsConstructor; |
|
|
import lombok.RequiredArgsConstructor; |
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
|
|
import org.springframework.stereotype.Component; |
|
|
import org.springframework.stereotype.Component; |
|
|
import org.thingsboard.rule.engine.api.notification.FirebaseService; |
|
|
import org.thingsboard.rule.engine.api.notification.FirebaseService; |
|
|
import org.thingsboard.server.common.data.User; |
|
|
import org.thingsboard.server.common.data.User; |
|
|
|
|
|
import org.thingsboard.server.common.data.UserMobileInfo; |
|
|
import org.thingsboard.server.common.data.id.TenantId; |
|
|
import org.thingsboard.server.common.data.id.TenantId; |
|
|
import org.thingsboard.server.common.data.notification.NotificationDeliveryMethod; |
|
|
import org.thingsboard.server.common.data.notification.NotificationDeliveryMethod; |
|
|
import org.thingsboard.server.common.data.notification.settings.MobileNotificationDeliveryMethodConfig; |
|
|
import org.thingsboard.server.common.data.notification.settings.MobileAppNotificationDeliveryMethodConfig; |
|
|
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.MobileAppDeliveryMethodNotificationTemplate; |
|
|
import org.thingsboard.server.dao.notification.NotificationSettingsService; |
|
|
import org.thingsboard.server.dao.notification.NotificationSettingsService; |
|
|
|
|
|
import org.thingsboard.server.dao.user.UserService; |
|
|
import org.thingsboard.server.service.notification.NotificationProcessingContext; |
|
|
import org.thingsboard.server.service.notification.NotificationProcessingContext; |
|
|
|
|
|
|
|
|
import java.util.Optional; |
|
|
import java.util.Optional; |
|
|
|
|
|
|
|
|
@Component |
|
|
@Component |
|
|
@RequiredArgsConstructor |
|
|
@RequiredArgsConstructor |
|
|
public class MobileNotificationChannel implements NotificationChannel<User, MobileDeliveryMethodNotificationTemplate> { |
|
|
public class MobileAppNotificationChannel implements NotificationChannel<User, MobileAppDeliveryMethodNotificationTemplate> { |
|
|
|
|
|
|
|
|
private final FirebaseService firebaseService; |
|
|
private final FirebaseService firebaseService; |
|
|
|
|
|
private final UserService userService; |
|
|
private final NotificationSettingsService notificationSettingsService; |
|
|
private final NotificationSettingsService notificationSettingsService; |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void sendNotification(User recipient, MobileDeliveryMethodNotificationTemplate processedTemplate, NotificationProcessingContext ctx) throws Exception { |
|
|
public void sendNotification(User recipient, MobileAppDeliveryMethodNotificationTemplate processedTemplate, NotificationProcessingContext ctx) throws Exception { |
|
|
String fcmToken = Optional.ofNullable(recipient.getAdditionalInfo()) |
|
|
UserMobileInfo mobileInfo = userService.findMobileInfo(recipient.getTenantId(), recipient.getId()); |
|
|
.map(info -> info.get("fcmToken")).filter(JsonNode::isTextual).map(JsonNode::asText) |
|
|
String fcmToken = Optional.ofNullable(mobileInfo) |
|
|
.orElse(null); |
|
|
.map(UserMobileInfo::getFcmToken) |
|
|
if (StringUtils.isEmpty(fcmToken)) { |
|
|
.orElseThrow(() -> new IllegalArgumentException("User doesn't use the mobile app")); |
|
|
throw new RuntimeException("User doesn't have the mobile app installed"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
MobileNotificationDeliveryMethodConfig config = ctx.getDeliveryMethodConfig(NotificationDeliveryMethod.MOBILE); |
|
|
MobileAppNotificationDeliveryMethodConfig config = ctx.getDeliveryMethodConfig(NotificationDeliveryMethod.MOBILE_APP); |
|
|
firebaseService.sendMessage(ctx.getTenantId(), config.getFirebaseServiceAccountCredentials(), |
|
|
try { |
|
|
fcmToken, processedTemplate.getSubject(), processedTemplate.getBody()); |
|
|
firebaseService.sendMessage(ctx.getTenantId(), config.getFirebaseServiceAccountCredentials(), |
|
|
|
|
|
fcmToken, processedTemplate.getSubject(), processedTemplate.getBody()); |
|
|
|
|
|
} catch (FirebaseMessagingException e) { |
|
|
|
|
|
if (e.getMessagingErrorCode() == MessagingErrorCode.UNREGISTERED) { |
|
|
|
|
|
// the token is no longer valid
|
|
|
|
|
|
mobileInfo.setFcmToken(null); |
|
|
|
|
|
userService.saveMobileInfo(recipient.getTenantId(), recipient.getId(), mobileInfo); |
|
|
|
|
|
throw new IllegalArgumentException("User doesn't use the mobile app"); |
|
|
|
|
|
} |
|
|
|
|
|
throw new RuntimeException("Failed to send message via FCM: " + e.getMessage(), e); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void check(TenantId tenantId) throws Exception { |
|
|
public void check(TenantId tenantId) throws Exception { |
|
|
NotificationSettings settings = notificationSettingsService.findNotificationSettings(tenantId); |
|
|
NotificationSettings settings = notificationSettingsService.findNotificationSettings(tenantId); |
|
|
if (!settings.getDeliveryMethodsConfigs().containsKey(NotificationDeliveryMethod.MOBILE)) { |
|
|
if (!settings.getDeliveryMethodsConfigs().containsKey(NotificationDeliveryMethod.MOBILE_APP)) { |
|
|
throw new RuntimeException("Push-notifications to mobile are not configured"); |
|
|
throw new RuntimeException("Push-notifications to mobile are not configured"); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public NotificationDeliveryMethod getDeliveryMethod() { |
|
|
public NotificationDeliveryMethod getDeliveryMethod() { |
|
|
return NotificationDeliveryMethod.MOBILE; |
|
|
return NotificationDeliveryMethod.MOBILE_APP; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |