如何解决“不能同时请求直接通知和查询通知”

时间:2019-04-13 04:53:12

标签: java spring-boot twilio twilio-api

使用Twilio通知发送通知时出现异常。

如果使用相同的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());
    }
  }

}

1 个答案:

答案 0 :(得分:2)

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

TwilioConfig.java

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

相反,每次发送Notification或SMS时,请使用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());
    }
  }
}