Sending SMS using Plivo in Google App Engine

Now-a-days, sending a SMS is part of the notification in all web and mobile products. We uses Plivo to send SMS notification.

Let me share the code that we used for sending SMS in Java Google App Engine project.

  public static final String PLIVO_VERSION = "v1";
  public static final String PLIVO_AUTH_ID = "YOUR_PLIVO_AUTH_ID";
  public static final String PLIVO_AUTH_TOKEN = "YOUR_PLIVO_AUTH_TOKEN";
  public static final String PLIVO_NUMBER = "YOUR_PLIVO_NUMBER";

  public static void sendSMS(String dst, String text) {

    Client client = Client.create();
    // client.addFilter(new LoggingFilter(System.out));
    WebResource webResource = client
        .resource("https://api.plivo.com/v1/Account/" + PLIVO_AUTH_ID
            + "/Message/");
    client.addFilter(new HTTPBasicAuthFilter(PLIVO_AUTH_ID,
        PLIVO_AUTH_TOKEN));

    JSONObject object = new JSONObject();
    object.put("src", PLIVO_NUMBER);
    object.put("dst", dst);
    object.put("text", text);
    object.put("type", "sms");
    ClientResponse clientResponse = webResource
        .type(MediaType.APPLICATION_JSON)
        .header(HTTPChecker.USER_AGENT_NAME,
            HTTPChecker.USER_AGENT_VALUE)
        .post(ClientResponse.class, object.toString());

    int status = clientResponse.getStatus();
    if (status >= 400) {
      System.out.println(clientResponse.getEntity(String.class));
    }
  }

Here we uses Java Rest client jersey to send a request to plivo.

Hope it helps and Have a nice day.

View comments

Sending Mail using MailGun in Google App Engine

Google App engine provides an email service to send email. Unfortunately it is limited to 100 emails per day. So we uses mailgun to send an email.

Let me share the code that we used in our Java project to send an email using mailgun.

  public static final String MAILGUN_API_KEY = "YOUR_MAILGUN_API_KEY";
  public static final String MAILGUN_HOST = "example.com"; // your host name

  public static void sendMail(String to, Set<String> cc, String subject,
      String html) {

    Client client = Client.create();
    client.addFilter(new HTTPBasicAuthFilter("api", MAILGUN_API_KEY));

    WebResource webResource = client.resource("https://api.mailgun.net/v2/"
        + MAILGUN_HOST + "/messages");

    MultivaluedMapImpl formData = new MultivaluedMapImpl();
    formData.add("from", "Example <hello@example.com>"); // your host email address
    formData.add("to", to);
    formData.add("subject", subject);
    formData.add("html", html);

    ClientResponse clientResponse = webResource.type(
        MediaType.APPLICATION_FORM_URLENCODED).post(
        ClientResponse.class, formData);
    int status = clientResponse.getStatus();
    if (status >= 400) {
      System.out.println(clientResponse.getEntity(String.class));
    }
  }

Have a nice day.

View comments

Social Sharing in Ionic application

You may need a social sharing feature inside your Ionic Framework Android and iOS application.

Using the Social Sharing plugin for Apache Cordova, we are able to do the social sharing.

Add ngCordova

First you have to add ngCordova to your Ionic application. If you already have this in your app, you can skip this step.

Install ngCordova

bower install ngCordova --save

Add ng-cordova.js before cordova script inside index.html

//index.html
<script src="lib/ngCordova/dist/ng-cordova.js"></script>
<script src="cordova.js"></script>

Add ngCordova in your app module dependencies

angular.module('myApp', ['ionic', 'ngCordova'])

Add Social Sharing cordova plugin

In order to implement social sharing, add cordova social sharing plugin.

cordova plugin add https://github.com/EddyVerbruggen/SocialSharing-PhoneGap-Plugin.git

Implement Social sharing

Now, go ahead and start creating method to share in your controllers

// share anywhere
$scope.share = function () {
    $cordovaSocialSharing.share('This is my message', 'Subject string', null, 'http://www.mylink.com');
}

// Share via email. Can be used for feedback
$scope.sendFeedback = function () {
    $cordovaSocialSharing
            .shareViaEmail('Some message', 'Some Subject', 'to_address@gmail.com');
}

// Share via SMS. Access multiple numbers in a string like: '0612345678,0687654321'
$scope.sendSMS = function (message, number) {
    $cordovaSocialSharing.shareViaSMS(message, number);
}

For more functionalities, refer the documentation.

Have a nice day.

View comments