简体   繁体   English

Twilio API; 短信未发送

[英]Twilio API; SMS not sending

I need help with this class, its not even returning the SIDs of the messages. 我需要有关此类的帮助,它甚至不返回消息的SID。 My application is meant to be ran 24/7 and it sends messages when it first begins, but then it shortly fails to send messages. 我的应用程序本应以24/7运行,并且在它第一次启动时就发送消息,但随后不久就无法发送消息。 Do I need to remake the TwilioRestClient object each time a message is sent? 每次发送消息时,我是否需要重新制作TwilioRestClient对象?

public class Twilio {

public static final String ACCOUNT_SID = "VALID SID";
public static final String ACCOUNT_AUTH = "VALID AUTH";

private static Twilio instance;

public static Twilio getInstance() {
    return instance == null ? (instance = new Twilio()) : instance;
}

private TwilioRestClient client;
private SmsFactory smsFactory;

private Map<String, String> defaultProps = new HashMap<>();

public Twilio() {
    defaultProps.put("From", "VALID TWILIO NUMBER");
    client = new TwilioRestClient(ACCOUNT_SID, ACCOUNT_AUTH);
}

public SmsFactory getSMSFactory() {
    return smsFactory == null ? (smsFactory = client.getAccount().getSmsFactory()) : smsFactory;
}

private Sms buildSMS(String recipient, String body) {
    Sms sms = null;
    defaultProps.put("To", recipient);
    defaultProps.put("Body", body);
    try {
        sms = getSMSFactory().create(defaultProps);
    } catch (TwilioRestException e) { e.printStackTrace(); }
    return sms;
}

public String[] sendSMS(String body, String... recipients) {
    List<String> sids = new ArrayList<>();
    for (String r : recipients)
        sids.add(buildSMS(r, body).getSid());
    return sids.toArray(new String[sids.size()]);
}

} }

You can try this code if it helps. 如果有帮助,您可以尝试使用此代码。 its working fine for me. 它对我来说很好。

public boolean sendSMS(String to, String from, String text){
    try {
        String[] toarr = to.split(",");
        TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN); 
        if(toarr.length > 0){
            for (int i = 0; i < toarr.length; i++) {
               List<NameValuePair> params = new ArrayList<NameValuePair>(); 
               params.add(new BasicNameValuePair("To", toarr[i]));
               params.add(new BasicNameValuePair("From", from)); 
               params.add(new BasicNameValuePair("Body", text));
               MessageFactory messageFactory = client.getAccount().getMessageFactory(); 
               com.twilio.sdk.resource.instance.Message message = messageFactory.create(params); 
            }
        }
   }
    catch (TwilioRestException tex) {
        tex.printStackTrace();
    }
    return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM