Codename One – Using the Nexmo SMS API
The Nexmo platform provides a set of cloud-based communication APIs such as the chat API, the number insights API, the voice API and the SMS API which we’re going to test in this quick tutorial.
We’ll create a sample app to send an sms using two different methods.
Setting the project
We’ll simply create a main ui with a “send sms” button. This should be the code for your Main.java for now:
public class Main { private Form current; private Resources theme; public void init(Object context) { theme = UIManager.initFirstTheme("/theme"); Toolbar.setGlobalToolbar(true); } public void start() { if (current != null) { current.show(); return; } Form hi = new Form("SMS demo"); Button btn = new Button("send sms"); btn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent evt) { // to do ... } }); hi.add(btn); hi.show(); } public void stop() { current = Display.getInstance().getCurrent(); if (current instanceof Dialog) { ((Dialog) current).dispose(); current = Display.getInstance().getCurrent(); } } public void destroy() { } }
Before starting you have to create a Nexmo account. You’ll need the api_key and api_secret later on :
1- Connecting to Nexmo Rest service
First possible way is to connect to a Nexmo rest service using an https connection. You will need to add the values of the parameters (api_key/api_secret/to/from/text) found on your Nexmo dashboard to the url as follows :
btn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent evt) { String myURL = "https://rest.nexmo.com/sms/json?api_key=*****&api_secret=*****&to=*****" + "&from=*****&text=*****"; ConnectionRequest cntRqst = new ConnectionRequest() { @Override protected void readResponse(InputStream in) throws IOException { } @Override protected void postResponse() { Dialog.show("SMS", "sms successfully sent", "OK", null); } }; cntRqst.setUrl(myURL); NetworkManager.getInstance().addToQueue(cntRqst); } });
2- Adding Nexmo Client library to your project
The second method is to add the Nexmo client jar to your project and use it. You simply create an AuthMethod object with the api_key and api_secret codes and a NexmoClient object with that authMethod and call client.getSmsClient().submitMessage(…) as follows:
btn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent evt) { AuthMethod auth = new TokenAuthMethod("*****", "*****"); // (api_key,api_secret) NexmoClient client = new NexmoClient(auth); SmsSubmissionResult[] responses = client.getSmsClient().submitMessage(new TextMessage( "*****", // from "*****", // to "*****")); // text for (SmsSubmissionResult response : responses) { System.out.println(response); } } });
That’s all coders ! You can integrate the Nexmo sms API in two minutes !
You can download the nexmo client and httpClient libraries here.
For more features, and further details check out their documentation and tutorials here.
Recent Comments