S_K*_*S_K 3 java twilio spring-boot twilio-api
使用 Twilio notify 发送通知时出现异常。
如果我在不发送 SMS 的情况下发送通知,则在使用相同的 Twilio NotificationCreator bean 发送 SMS 后发送通知时,代码会引发异常,它工作正常。
这是 Twilio 通知的配置
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
@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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
您应该必须从 TwilioConfig.java 文件中删除 bean 创建方法。
@Bean
public NotificationCreator notificationCreator() {
return Notification.creator(serviceId);
}
Run Code Online (Sandbox Code Playgroud)
相反,每次发送通知或短信时,都使用 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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
75 次 |
| 最近记录: |