如何修复 "Direct notifications and query notifications cannot be requested together"
How to fix "Direct notifications and query notifications cannot be requested together"
我在使用 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);
}
}
NotificationService.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());
}
}
}
我在使用 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);
}
}
NotificationService.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());
}
}
}