3

I am getting an exception while sending notification using Twilio notify.

The code throws an exception when I send notification after sending SMS using the same Twilio NotificationCreator bean if I am sending notification without sending SMS it is working fine.

Here is the configuration for Twilio notify

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());
    }
  }

}
Community
  • 1
  • 1
S_K
  • 700
  • 1
  • 8
  • 13

1 Answers1

5

You should have to remove the bean creation method from TwilioConfig.java file.

TwilioConfig.java

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

Instead, use a new object of NotificationCreator bean every time when you send Notification or SMS.

For example :

@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());
    }
  }
}
Community
  • 1
  • 1
Rahul Goti
  • 949
  • 1
  • 7
  • 11
  • Note: Always create a spring singleton bean whose internal properties never modified during the life cycle of the bean. – S_K Apr 17 '19 at 11:08