gpt4 book ai didi

java - 如何修复 "Direct notifications and query notifications cannot be requested together"

转载 作者:行者123 更新时间:2023-12-01 14:21:17 25 4
gpt4 key购买 nike

我在使用 Twilio notify 发送通知时遇到异常。

当我在使用相同的 Twilio NotificationCreator bean 发送 SMS 后发送通知时,如果我发送通知而不发送 SMS,它工作正常,代码会抛出异常。

这是 Twilio 通知的配置

TwilioConfig.java

@Configuration
public class TwilioConfig {

@Value("${twilio.accountSid}")
private String accountSid;

@Value("${twilio.authToken}")
private String authToken;

@Value("${twilio.serviceId}")
private String serviceId;

@Bean
public TwilioRestClient twilioRestClient() {
return new TwilioRestClient.Builder(accountSid, authToken)
.build();
}

@Bean
public NotificationCreator notificationCreator() {
return Notification.creator(serviceId);
}

}

通知服务.java

@Service
public class NotificationService {

@Autowired
private TwilioRestClient twilioRestClient;

@Autowired
private NotificationCreator notificationCreator;

public void sendPushNotification(String title, String body, List<String> identities) {
try {
Notification notification = notificationCreator
.setTitle(title)
.setBody(body)
.setIdentity(identities)
.create(twilioRestClient);

} catch (TwilioException e) {
log.error("An exception occurred trying to send a notification to {}, exception: {}", identities, e.getMessage());
}
}

public void createAndSendSms(String body, String to) {
try {
List<String> toBindings = Collections.singletonList(
"{\"binding_type\":\"sms\",\"address\":\"" + to + "\"}"
);

Notification notification = notificationCreator
.setBody(body)
.setToBinding(toBindings)
.create(twilioRestClient);

} catch (TwilioException e) {
log.error("An exception occurred trying to send a message to {}, exception: {}", to, e.getMessage());
}
}

}

最佳答案

您应该必须从 TwilioConfig.java 文件中删除 bean 创建方法。

TwilioConfig.java

 @Bean
public NotificationCreator notificationCreator() {
return Notification.creator(serviceId);
}

相反,每次发送通知或短信时都使用一个新的 NotificationCreator bean 对象。

例如:

@Service
public class NotificationService {

@Value("${twilio.serviceId}")
private String serviceId;

public void sendPushNotification(String title, String body, List<String> identities) {
try {

// Notification notification = notificationCreator
Notification notification = Notification.creator(serviceId)
.setTitle(title)
.setBody(body)
.setIdentity(identities)
.create(twilioRestClient);

} catch (TwilioException e) {
log.error("An exception occurred trying to send a notification to {}, exception: {}", identities, e.getMessage());
}
}

public void createAndSendSms(String body, String to) {
try {
List<String> toBindings = Collections.singletonList(
"{\"binding_type\":\"sms\",\"address\":\"" + to + "\"}"
);

// Notification notification = notificationCreator
Notification notification = Notification.creator(serviceId)
.setBody(body)
.setToBinding(toBindings)
.create(twilioRestClient);

} catch (TwilioException e) {
log.error("An exception occurred trying to send a message to {}, exception: {}", to, e.getMessage());
}
}
}

关于java - 如何修复 "Direct notifications and query notifications cannot be requested together",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55662139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com